-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext2_test.go
82 lines (62 loc) · 1.35 KB
/
context2_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
package context_test
import (
"fmt"
"math/rand"
"testing"
"time"
context "github.com/mcfly722/context"
)
type node2 struct {
i int
path string
}
func newNode2(path string, i int) *node2 {
return &node2{
path: path,
i: i,
}
}
func (node *node2) Go(current context.Context) {
loop:
for {
select {
case <-time.After(time.Duration(rand.Intn(100)) * time.Microsecond):
current.Close()
case <-time.After(time.Duration(rand.Intn(100)) * time.Microsecond):
break loop
case _, isOpened := <-current.Context():
if !isOpened {
break loop
}
default:
{
}
}
}
}
func (parent *node2) simpleTree2(context context.ChildContext, width int, height int) {
if height > 1 {
for i := 0; i < width; i++ {
newNode := newNode2(fmt.Sprintf("%v->%v", parent.path, i), i)
newContext, err := context.NewContextFor(newNode)
if err == nil {
newNode.simpleTree2(newContext, width, height-1)
}
}
}
}
const race_iterations int = 10000
func Test_Race_RandomSimpleTree3x3(t *testing.T) {
for i := 1; i <= race_iterations; i++ {
if (i % 100) == 0 {
fmt.Printf("Test_Race_RandomSimpleTree3x3 %v/%v\n", i, race_iterations)
}
rootNode := newNode2("", 0)
rootContext := context.NewRootContext(rootNode)
rootNode.simpleTree2(rootContext, 3, 3)
go func() {
rootContext.Close()
}()
rootContext.Wait()
}
}