-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp_test.go
More file actions
73 lines (61 loc) · 1.41 KB
/
Copy pathapp_test.go
File metadata and controls
73 lines (61 loc) · 1.41 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
package main
import (
"context"
"net"
"testing"
"time"
"github.com/masahide/OmniSSHAgent/pkg/agentlistener"
)
func TestAppShutdownCancelsAgents(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
var app App
app.cancelAgents = cancel
app.wg.Add(1)
go func() {
defer app.wg.Done()
<-ctx.Done()
}()
done := make(chan struct{})
go func() {
app.shutdown(context.Background())
close(done)
}()
select {
case <-done:
case <-time.After(500 * time.Millisecond):
t.Fatalf("shutdown did not finish in time")
}
if err := ctx.Err(); err != context.Canceled {
t.Fatalf("expected context canceled, got %v", err)
}
}
func TestAppShutdownStopsListener(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatalf("net.Listen failed: %v", err)
}
defer listener.Close()
var app App
ctx, cancel := context.WithCancel(context.Background())
app.agentCtx = ctx
app.cancelAgents = cancel
app.wg.Add(1)
go func() {
defer app.wg.Done()
if err := agentlistener.Serve(app.agentCtx, listener, func(ctx context.Context, c net.Conn) {
c.Close()
}); err != nil && err != net.ErrClosed {
t.Fatalf("Serve returned unexpected error: %v", err)
}
}()
done := make(chan struct{})
go func() {
app.shutdown(context.Background())
close(done)
}()
select {
case <-done:
case <-time.After(time.Second):
t.Fatal("shutdown did not finish in time")
}
}