-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext5_test.go
91 lines (74 loc) · 2.01 KB
/
context5_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
package context_test
import (
"fmt"
"sync"
"testing"
"time"
context "github.com/mcfly722/context"
)
type node5 struct {
name string
sequenceChecker sequenceChecker
sequenceStep int
ready sync.Mutex
}
func (node *node5) getName() string {
node.ready.Lock()
defer node.ready.Unlock()
return node.name
}
func (node *node5) 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.NotifyWithText(node.sequenceStep, "%v finished\n", node.getName())
}
func mixedLadder(sequenceChecker sequenceChecker, path string, parents map[context.ChildContext]struct{}, width int, height int) {
if height > 0 {
newPath := fmt.Sprintf("%v->%v", path, height)
newContexts := make(map[context.ChildContext]struct{})
for i := 0; i < width; i++ {
newInstance := &node5{
name: fmt.Sprintf("%v", newPath),
sequenceChecker: sequenceChecker,
sequenceStep: height,
}
fmt.Printf("%v configured\n", newInstance.getName())
for parent := range parents {
newContext, _ := parent.NewContextFor(newInstance)
newContexts[newContext] = struct{}{}
}
}
mixedLadder(sequenceChecker, newPath, newContexts, width, height-1)
}
}
func Test_MixedLadder(t *testing.T) {
const ladderHight = 10
sequenceChecker := newSequenceChecker()
rootNode := &node5{
name: "root",
sequenceChecker: sequenceChecker,
sequenceStep: ladderHight + 1,
}
rootContext := context.NewRootContext(rootNode)
rootContextMap := make(map[context.ChildContext]struct{})
rootContextMap[rootContext] = struct{}{}
mixedLadder(sequenceChecker, "root", rootContextMap, 3, ladderHight)
go func() {
time.Sleep(100 * time.Millisecond)
sequenceChecker.NotifyWithText(0, "Close\n")
rootContext.Close()
}()
rootContext.Wait()
fmt.Printf("finished with correct sequence = %v\n", sequenceChecker.ToString())
}