-
Notifications
You must be signed in to change notification settings - Fork 24
/
deadline_test.go
76 lines (57 loc) · 1.37 KB
/
deadline_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
70
71
72
73
74
75
76
package deadline
import (
"errors"
"fmt"
"testing"
"time"
)
func workerTakes5ms(stopper chan error) error {
fmt.Println("i'm doing this job in 5ms")
time.Sleep(5 * time.Millisecond)
return nil
}
func workerTakes20ms(stopper chan error) error {
fmt.Println("i'm doing this job in 20ms,so work will timeout")
time.Sleep(20 * time.Millisecond)
return nil
}
func cancelWork(stopper chan error) error {
fmt.Println("i'm doing this job")
stopper <- errors.New("canceled job") //cancel job
time.Sleep(5 * time.Millisecond)
fmt.Println("job canceled")
return nil
}
func returnsError(stopper chan error) error {
fmt.Println("i'm doing this job but error occurred")
return errors.New("foo")
}
func TestMultiDeadline(t *testing.T) {
dl := New(15*time.Millisecond, "test multi deadline case")
if err := dl.Run(workerTakes5ms); err != nil {
t.Error(err)
}
err := dl.Run(cancelWork)
t.Log("cancelWork error:", err)
if err.Error() != "canceled job" {
t.Error(err)
}
err = dl.Run(workerTakes20ms)
if err != ErrTimedOut {
t.Error(err)
}
}
func TestDeadline(t *testing.T) {
dl := New(1*time.Second, "one time deadline case worker")
done := make(chan error)
err := dl.Run(func(stopper chan error) error {
fmt.Println("i am doing something here")
time.Sleep(time.Second * 2)
close(done)
return nil
})
if err != ErrTimedOut {
t.Error(err)
}
<-done
}