forked from kamilsk/semaphore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchannel_test.go
60 lines (48 loc) · 1.25 KB
/
channel_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
package semaphore_test
import (
"os"
"testing"
"time"
. "github.com/kamilsk/semaphore/v5"
)
func TestMultiplex(t *testing.T) {
sleep := 100 * time.Millisecond
start := time.Now()
<-Multiplex(WithSignal(os.Interrupt), WithTimeout(sleep))
end := time.Now()
if expected, obtained := sleep, end.Sub(start); expected > obtained {
t.Errorf("an unexpected sleep time. expected: %v; obtained: %v", expected, obtained)
}
}
func TestMultiplex_WithoutChannels(t *testing.T) {
<-Multiplex()
}
func TestWithDeadline(t *testing.T) {
tests := []struct {
name string
deadline time.Duration
}{
{"normal case", 10 * time.Millisecond},
{"past deadline", -time.Nanosecond},
}
for _, test := range tests {
start := time.Now()
<-WithDeadline(start.Add(test.deadline))
end := time.Now()
if !end.After(start.Add(test.deadline)) {
t.Errorf("%s: an unexpected deadline", test.name)
}
}
}
func TestWithSignal_NilSignal(t *testing.T) {
<-WithSignal(nil)
}
func TestWithTimeout(t *testing.T) {
sleep := 100 * time.Millisecond
start := time.Now()
<-WithTimeout(sleep)
end := time.Now()
if expected, obtained := sleep, end.Sub(start); expected > obtained {
t.Errorf("an unexpected sleep time. expected: %v; obtained: %v", expected, obtained)
}
}