-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller_test.go
58 lines (52 loc) · 1.35 KB
/
controller_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
package goproc
import (
"context"
"fmt"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
type testKeyType struct{ int }
var (
hangingAroundKey1 = &testKeyType{}
)
func hangingAround(ctx context.Context) {
fmt.Printf("Start hanging around...\n")
if value := ctx.Value(hangingAroundKey1); value != nil {
fmt.Printf("Found a passed value through context: %+v\n", value)
if value == "panic" {
panic(value)
}
}
<-ctx.Done()
fmt.Printf("Stop hanging around because... %+v!\n", ctx.Err())
}
func TestBackgroundController(t *testing.T) {
Convey("With test controller created", t, func(c C) {
ctrl := NewController(context.Background(), t.Name())
Convey("Test wait exit", func() {
ctrl.WithTimeout(3 * time.Second).
Go(hangingAround).
Wait()
So(ctrl.Die(), ShouldBeTrue)
})
Convey("Test shutdown", func() {
ctrl.Go(hangingAround).Shutdown()
So(ctrl.Die(), ShouldBeTrue)
})
Convey("Test passing value", func() {
ctrl.WithDeadline(time.Now().Add(3*time.Second)).
WithValue(hangingAroundKey1, "Let's play!").
Go(hangingAround).
Wait()
So(ctrl.Die(), ShouldBeTrue)
})
Convey("Test passing value with anonymous key", func() {
ctrl.WithTimeout(3*time.Second).
WithValue(&testKeyType{}, "panic").
Go(hangingAround).
Wait() // should not receive key
So(ctrl.Die(), ShouldBeTrue)
})
})
}