-
Notifications
You must be signed in to change notification settings - Fork 8
/
transmission_test.go
69 lines (55 loc) · 1.52 KB
/
transmission_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
package nbd
import (
"context"
"path/filepath"
"testing"
"time"
)
func TestListenAndServeContextCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dir := t.TempDir()
sockFile := filepath.Join(dir, "nbd.sock")
// Start the server
exited := make(chan any)
go func() {
defer close(exited)
err := ListenAndServe(ctx, "unix", sockFile, Export{})
if err != nil {
t.Errorf("ListenAndServe returned an error: %v", err)
}
}()
// Simulate the server working for some time
time.Sleep(100 * time.Millisecond)
// Test cancelling the context
cancel()
select {
case <-time.After(1 * time.Second):
t.Error("Server did not shut down after context was cancelled")
case <-exited:
// The server context should be cancelled, and the server should shut down, within a very short time.
}
}
func TestListenAndServeContextNoCancel(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
dir := t.TempDir()
sockFile := filepath.Join(dir, "nbd.sock")
// Start the server
exited := make(chan any)
go func() {
defer close(exited)
err := ListenAndServe(ctx, "unix", sockFile, Export{})
if err != nil {
t.Errorf("ListenAndServe returned an error: %v", err)
}
}()
// Simulate the server working for some time
time.Sleep(100 * time.Millisecond)
select {
case <-time.After(100 * time.Millisecond):
// No cancel was called, so we are stuck in ListenAndServe
case <-exited:
t.Error("Server did not shut down after context was cancelled")
}
}