forked from kamilsk/semaphore
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext_test.go
More file actions
37 lines (32 loc) · 779 Bytes
/
context_test.go
File metadata and controls
37 lines (32 loc) · 779 Bytes
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
package semaphore_test
import (
"context"
"testing"
"time"
. "github.com/kamilsk/semaphore/v5"
)
func TestWithContext(t *testing.T) {
tests := []struct {
name string
deadline func() <-chan struct{}
expected time.Duration
}{
{"normal case", func() <-chan struct{} {
ch := make(chan struct{})
go func() { time.AfterFunc(10*time.Millisecond, func() { close(ch) }) }()
return ch
}, 10 * time.Millisecond},
{"nil channel", func() <-chan struct{} { return nil }, 0},
}
for _, test := range tests {
tc := test
t.Run(test.name, func(t *testing.T) {
start := time.Now()
<-WithContext(context.TODO(), tc.deadline()).Done()
end := time.Now()
if !end.After(start.Add(tc.expected)) {
t.Errorf("an unexpected deadline")
}
})
}
}