-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext4_test.go
98 lines (82 loc) · 2 KB
/
context4_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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package context_test
import (
"fmt"
"testing"
"time"
context "github.com/mcfly722/context"
)
type childNode5 struct {
sequenceChecker sequenceChecker
}
type rootNode5 struct {
sequenceChecker sequenceChecker
}
func (node *childNode5) Go(current context.Context) {
loop:
for {
select {
case _, isOpened := <-current.Context():
if !isOpened {
break loop
}
default:
{
}
}
}
node.sequenceChecker.NotifyWithText(7, "childNode disposing for 300ms\n")
time.Sleep(300 * time.Millisecond)
node.sequenceChecker.NotifyWithText(8, "childNode finished\n")
}
func (node *rootNode5) Go(current context.Context) {
loop:
for {
select {
case _, isOpened := <-current.Context():
if !isOpened {
break loop
}
default:
{
}
}
}
node.sequenceChecker.NotifyWithText(9, "rootNode finished\n")
}
func Test_FailCreateContextFromRootNode(t *testing.T) {
sequenceChecker := newSequenceChecker()
rootNode := &rootNode5{
sequenceChecker: sequenceChecker,
}
childNode := &childNode5{
sequenceChecker: sequenceChecker,
}
sequenceChecker.NotifyWithText(1, "creating new root context\n")
rootContext := context.NewRootContext(rootNode)
sequenceChecker.NotifyWithText(2, "creating child context\n")
_, err := rootContext.NewContextFor(childNode)
if err != nil {
t.Fatal(err)
}
go func() {
time.Sleep(100 * time.Millisecond)
sequenceChecker.NotifyWithText(4, "Closing root context\n")
rootContext.Close()
sequenceChecker.NotifyWithText(5, "trying to create new context from closed root context...\n")
newChildNode := &childNode5{}
_, err := rootContext.NewContextFor(newChildNode)
if err != nil {
_, ok := err.(*context.ClosingIsInProcessForFreezeError)
if ok {
fmt.Printf("* - successfully catched error: %v\n", err)
} else {
panic("uncatched error")
}
} else {
panic("uncatched error")
}
}()
sequenceChecker.NotifyWithText(3, "Wait\n")
rootContext.Wait()
fmt.Printf("test finished with correct sequence = %v\n", sequenceChecker.ToString())
}