-
Notifications
You must be signed in to change notification settings - Fork 3
/
server_test.go
101 lines (78 loc) · 2.69 KB
/
server_test.go
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
package main
import (
"context"
"errors"
"os/signal"
"syscall"
"testing"
"time"
"github.com/chinmina/chinmina-bridge/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
type MockServer struct {
mock.Mock
}
func (m *MockServer) ListenAndServe() error {
args := m.Called()
return args.Error(0)
}
func (m *MockServer) Shutdown(ctx context.Context) error {
args := m.Called(ctx)
return args.Error(0)
}
func (m *MockServer) RegisterOnShutdown(f func()) {
m.Called(f)
}
func TestServeHTTP_StartupError(t *testing.T) {
// deregister signal handlers afterwards just in case they're left around
defer signal.Reset()
expectedErr := errors.New("startup error")
mockServer := MockServer{}
mockServer.On("ListenAndServe").Return(expectedErr)
mockServer.On("Shutdown", mock.Anything).Return(nil)
serverCfg := config.ServerConfig{Port: -1, ShutdownTimeoutSeconds: 25}
err := serveHTTP(serverCfg, &mockServer)
require.Error(t, err)
assert.Equal(t, expectedErr, err)
mockServer.AssertExpectations(t)
}
func TestServeHTTP_ShutdownSignal(t *testing.T) {
// deregister signal handlers afterwards just in case they're left around
defer signal.Reset()
// set up the server to run for a period of time if an expected signal
// interrupt isn't received
expectedErr := errors.New("startup should be interrupted before this error is returned")
mockServer := MockServer{}
mockServer.On("ListenAndServe").Return(expectedErr).WaitUntil(time.After(5 * time.Second))
mockServer.On("Shutdown", mock.Anything).Return(nil)
// send termination signal after the mock server has had enough time to start
startupTimer, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()
go func() {
<-startupTimer.Done()
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
}()
serverCfg := config.ServerConfig{Port: -1, ShutdownTimeoutSeconds: 25}
err := serveHTTP(serverCfg, &mockServer)
require.NoError(t, err)
mockServer.AssertExpectations(t)
}
func TestServeHTTP_GracefulShutdown(t *testing.T) {
// deregister signal handlers afterwards just in case they're left around
defer signal.Reset()
var actualError error
mockServer := MockServer{}
mockServer.On("ListenAndServe").Return(nil)
mockServer.On("Shutdown", mock.Anything).Run(func(args mock.Arguments) {
ctx := args.Get(0).(context.Context)
<-ctx.Done()
actualError = ctx.Err()
}).Return(errors.New("ignore this"))
serverCfg := config.ServerConfig{Port: -1, ShutdownTimeoutSeconds: 1}
_ = serveHTTP(serverCfg, &mockServer)
require.Error(t, actualError)
assert.ErrorContains(t, actualError, "context deadline exceeded")
mockServer.AssertExpectations(t)
}