-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
106 lines (86 loc) · 2.94 KB
/
server_test.go
File metadata and controls
106 lines (86 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"net/http"
"net/http/httptest"
"os"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func Test_rootHandler(t *testing.T) {
t.Run("POSITIVE-RootPath_ReturnsStatusOK", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/", nil)
rr := httptest.NewRecorder()
rootHandler(rr, req)
assert.Equal(t, http.StatusOK, rr.Code)
assert.Equal(t, http.StatusText(http.StatusOK), rr.Body.String())
})
t.Run("NEGATIVE-NonRootPath_ReturnsStatusNotFound", func(t *testing.T) {
req, _ := http.NewRequest("GET", "/something-else", nil)
rr := httptest.NewRecorder()
rootHandler(rr, req)
assert.Equal(t, http.StatusNotFound, rr.Code)
assert.Equal(t, http.StatusText(http.StatusNotFound)+"\n", rr.Body.String())
})
}
func TestServer_Run_GracefulShutdown(t *testing.T) {
// We need a test DB and processor for NewServer to work without real dependencies
db, cleanupDB := setupTestDatabase(t)
defer cleanupDB()
// We can't use the real processor because it will try to connect to RabbitMQ
// We use a mock/fake one instead.
mockPublisher := NewMockPublisher(t)
mockPublisher.EXPECT().Close().Return(nil).Maybe() // Called on graceful shutdown
processor := &QueueProcessor{
repo: NewRepository(db),
channel: mockPublisher,
api: &FakeAPI{},
}
server, err := NewServer(WithConfig(&Config{}), WithDB(db), WithProcessor(processor))
assert.NoError(t, err)
// Channel to listen for the server.Run error
errCh := make(chan error, 1)
go func() {
// Use a non-standard port to avoid conflicts during tests
errCh <- server.Run(8989)
}()
// Give the server a moment to start up
time.Sleep(200 * time.Millisecond)
// Get the current process and send a shutdown signal
p, err := os.FindProcess(os.Getpid())
assert.NoError(t, err)
err = p.Signal(syscall.SIGTERM)
assert.NoError(t, err)
// Wait for the server to shut down, with a timeout to prevent the test from hanging
select {
case err := <-errCh:
// On graceful shutdown, server.Run should return nil.
assert.NoError(t, err, "server.Run should return nil on graceful shutdown")
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for server to shut down gracefully")
}
}
func TestNewServer_WithConfig(t *testing.T) {
t.Run("POSITIVE-ConfigOptionSetsServerConfig", func(t *testing.T) {
// Create a dummy config
dummyConfig := &Config{
Host: "test-host",
Port: 1234,
}
// Create mock DB and processor
db, cleanupDB := setupTestDatabase(t)
defer cleanupDB()
mockPublisher := NewMockPublisher(t)
processor := &QueueProcessor{
repo: NewRepository(db),
channel: mockPublisher,
api: &FakeAPI{},
}
// Create a new server with the WithConfig option, and also provide mock DB and processor
server, err := NewServer(WithConfig(dummyConfig), WithDB(db), WithProcessor(processor))
assert.NoError(t, err)
// Assert that the server's config is the dummy config
assert.Equal(t, dummyConfig, server.cfg)
})
}