forked from Mirantis/dhcp-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
81 lines (58 loc) · 1.86 KB
/
Copy pathmain_test.go
File metadata and controls
81 lines (58 loc) · 1.86 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The dhcp-relay Authors
//go:build linux
package main
import (
"os"
"sync"
"sync/atomic"
"testing"
"time"
)
// TestWaitForHandlersCompletes drains a burst of quick handlers within the grace period.
func TestWaitForHandlersCompletes(t *testing.T) {
var wg sync.WaitGroup
var inFlight atomic.Int64
const n = 50
for range n {
wg.Go(func() {
inFlight.Add(1)
defer inFlight.Add(-1)
time.Sleep(5 * time.Millisecond)
})
}
forceQuit := make(chan os.Signal, 1)
if got := waitForHandlers(&wg, forceQuit, shutdownGracePeriod); got != drainCompleted {
t.Errorf("waitForHandlers = %v, want drainCompleted", got)
}
if got := inFlight.Load(); got != 0 {
t.Errorf("inFlight = %d after drain, want 0", got)
}
}
// TestWaitForHandlersGracePeriodExpiry returns drainTimedOut when a handler outlives the grace period.
func TestWaitForHandlersGracePeriodExpiry(t *testing.T) {
var wg sync.WaitGroup
handlerDone := make(chan struct{})
wg.Go(func() { <-handlerDone })
forceQuit := make(chan os.Signal, 1)
if got := waitForHandlers(&wg, forceQuit, 20*time.Millisecond); got != drainTimedOut {
t.Errorf("waitForHandlers = %v, want drainTimedOut", got)
}
// Release the handler so the test goroutine does not leak.
close(handlerDone)
wg.Wait()
}
// TestWaitForHandlersForced returns drainForced when a second signal arrives before the handlers drain.
func TestWaitForHandlersForced(t *testing.T) {
var wg sync.WaitGroup
handlerDone := make(chan struct{})
wg.Go(func() { <-handlerDone })
forceQuit := make(chan os.Signal, 1)
forceQuit <- os.Interrupt
if got := waitForHandlers(&wg, forceQuit, time.Hour); got != drainForced {
t.Errorf("waitForHandlers = %v, want drainForced", got)
}
// Release the handler so the test goroutine does not leak.
close(handlerDone)
wg.Wait()
}