-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext7_test.go
85 lines (70 loc) · 1.66 KB
/
context7_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
package context_test
import (
"fmt"
"sync"
"testing"
"time"
context "github.com/mcfly722/context"
)
type node7 struct {
name string
sequenceChecker sequenceChecker
sequenceStep int
ready sync.Mutex
}
func (node *node7) getName() string {
node.ready.Lock()
defer node.ready.Unlock()
return node.name
}
func (node *node7) Go(current context.Context) {
fmt.Printf("go: %v started\n", node.getName())
loop:
for {
select {
case _, isOpened := <-current.Context():
if !isOpened {
break loop
}
default:
{
}
}
}
node.sequenceChecker.Notify(node.sequenceStep)
fmt.Printf("%v finished\n", node.getName())
}
func (parent *node7) ladder(context context.ChildContext, height int) {
fmt.Printf("%v configured\n", parent.getName())
if height > 1 {
newNode := &node7{
name: fmt.Sprintf("%v->%v", parent.getName(), height),
sequenceChecker: parent.sequenceChecker,
sequenceStep: height,
}
newContext, err := context.NewContextFor(newNode)
if err == nil {
newNode.ladder(newContext, height-1)
}
}
}
func Test_Ladder(t *testing.T) {
const ladderHight = 20
sequenceChecker := newSequenceChecker()
rootNode := &node7{
name: "root",
sequenceChecker: sequenceChecker,
sequenceStep: ladderHight + 1,
}
rootContext := context.NewRootContext(rootNode)
rootNode.ladder(rootContext, ladderHight)
go func() {
time.Sleep(100 * time.Millisecond)
fmt.Println("Close")
sequenceChecker.Notify(1)
rootContext.Close()
}()
rootContext.Wait()
sequenceChecker.Notify(ladderHight + 2)
fmt.Printf("test done with correct sequence=%v\n", sequenceChecker.ToString())
}