-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
58 lines (46 loc) · 1.29 KB
/
server_test.go
File metadata and controls
58 lines (46 loc) · 1.29 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
package web
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNewServer(t *testing.T) {
svr := NewServer(Options{})
assert.NotNil(t, svr)
assert.Equal(t, ":8080", svr.Addr())
assert.Equal(t, false, svr.options.IsTls())
assert.Nil(t, svr.options.TlsConfig())
}
func TestServer_Run(t *testing.T) {
// Create a server with a random port to avoid conflicts
svr := NewServer(Options{Addr: ":0"})
// Run server in a goroutine
errCh := make(chan error, 1)
go func() {
errCh <- svr.Run()
}()
// Give server time to start
time.Sleep(100 * time.Millisecond)
// Server should be running, try to shutdown
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := svr.Shutdown(ctx)
assert.NoError(t, err)
// Wait for Run to return
select {
case err := <-errCh:
// Run returns "http: Server closed" when shutdown gracefully
assert.ErrorContains(t, err, "Server closed")
case <-time.After(2 * time.Second):
t.Fatal("server didn't stop in time")
}
}
func TestServer_Shutdown(t *testing.T) {
svr := NewServer(Options{Addr: ":0"})
// Shutdown on a non-running server should not error
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
err := svr.Shutdown(ctx)
assert.NoError(t, err)
}