Debian Codegraph @

stream_test.go

raw symbols SCIP
1 package observer
2
3 import (
4 "fmt"
5 "testing"
6 "time"
7 )
8
9 func TestStreamInitialValue(t *testing.T) {
10 state := newState(10)
11 stream := &stream{state: state}
12 if val := stream.Value(); val != 10 {
13 t.Fatalf("Expecting 10 but got %#v\n", val)
14 }
15 }
16
17 func TestStreamUpdate(t *testing.T) {
18 state1 := newState(10)
19 state2 := state1.update(15)
20 stream := &stream{state: state1}
21 if val := stream.Value(); val != 10 {
22 t.Fatalf("Expecting 10 but got %#v\n", val)
23 }
24 state2.update(15)
25 if val := stream.Value(); val != 10 {
26 t.Fatalf("Expecting 10 but got %#v\n", val)
27 }
28 }
29
30 func TestStreamNextValue(t *testing.T) {
31 state1 := newState(10)
32 stream := &stream{state: state1}
33 state2 := state1.update(15)
34 if val := stream.Next(); val != 15 {
35 t.Fatalf("Expecting 15 but got %#v\n", val)
36 }
37 state2.update(20)
38 if val := stream.Next(); val != 20 {
39 t.Fatalf("Expecting 20 but got %#v\n", val)
40 }
41 }
42
43 func TestStreamDetectsChanges(t *testing.T) {
44 state := newState(10)
45 stream := &stream{state: state}
46 select {
47 case <-stream.Changes():
48 t.Fatalf("Expecting no changes\n")
49 default:
50 }
51 go func() {
52 time.Sleep(1 * time.Second)
53 state.update(15)
54 }()
55 select {
56 case <-stream.Changes():
57 case <-time.After(2 * time.Second):
58 t.Fatalf("Expecting changes\n")
59 }
60 select {
61 case <-stream.Changes():
62 default:
63 t.Fatalf("Expecting changes\n")
64 }
65 if val := stream.Next(); val != 15 {
66 t.Fatalf("Expecting 15 but got %#v\n", val)
67 }
68 select {
69 case <-stream.Changes():
70 t.Fatalf("Expecting no changes\n")
71 default:
72 }
73 }
74
75 func TestStreamHasChanges(t *testing.T) {
76 state := newState(10)
77 stream := &stream{state: state}
78 if stream.HasNext() {
79 t.Fatalf("Expecting no changes\n")
80 }
81 state.update(15)
82 if !stream.HasNext() {
83 t.Fatalf("Expecting changes\n")
84 }
85 if val := stream.Next(); val != 15 {
86 t.Fatalf("Expecting 15 but got %#v\n", val)
87 }
88 }
89
90 func TestStreamWaitsNext(t *testing.T) {
91 state := newState(10)
92 stream := &stream{state: state}
93 for i := 15; i <= 100; i++ {
94 state = state.update(i)
95 if val := stream.WaitNext(); val != i {
96 t.Fatalf("Expecting %#v but got %#v\n", i, val)
97 }
98 }
99 if stream.HasNext() {
100 t.Fatalf("Expecting no changes\n")
101 }
102 }
103
104 func TestStreamClone(t *testing.T) {
105 state := newState(10)
106 stream1 := &stream{state: state}
107 stream2 := stream1.Clone()
108 if stream2.HasNext() {
109 t.Fatalf("Expecting no changes\n")
110 }
111 if val := stream2.Value(); val != 10 {
112 t.Fatalf("Expecting 10 but got %#v\n", val)
113 }
114 state.update(15)
115 if !stream1.HasNext() {
116 t.Fatalf("Expecting changes\n")
117 }
118 if !stream2.HasNext() {
119 t.Fatalf("Expecting changes\n")
120 }
121 stream1.Next()
122 if val := stream1.Value(); val != 15 {
123 t.Fatalf("Expecting 15 but got %#v\n", val)
124 }
125 if val := stream2.Value(); val != 10 {
126 t.Fatalf("Expecting 10 but got %#v\n", val)
127 }
128 stream2.Next()
129 if val := stream2.Value(); val != 15 {
130 t.Fatalf("Expecting 15 but got %#v\n", val)
131 }
132 if stream1.HasNext() {
133 t.Fatalf("Expecting no changes\n")
134 }
135 if stream2.HasNext() {
136 t.Fatalf("Expecting no changes\n")
137 }
138 }
139
140 func TestStreamConcurrencyWithClones(t *testing.T) {
141 initial := 1000
142 final := 2000
143 prop := NewProperty(initial)
144 stream := prop.Observe()
145 var cherrs []chan error
146 for i := 0; i < 1000; i++ {
147 cherr := make(chan error, 1)
148 cherrs = append(cherrs, cherr)
149 go testStreamRead(stream.Clone(), initial, final, cherr)
150 }
151 done := make(chan bool)
152 go func(prop Property, initial, final int, done chan bool) {
153 defer close(done)
154 for i := initial + 1; i <= final; i++ {
155 prop.Update(i)
156 }
157 }(prop, initial, final, done)
158 for _, cherr := range cherrs {
159 if err := <-cherr; err != nil {
160 t.Fatal(err)
161 }
162 }
163 <-done
164 }
165
166 func testStreamRead(s Stream, initial, final int, err chan error) {
167 val := s.Value().(int)
168 if val != initial {
169 err <- fmt.Errorf("Expecting %#v but got %#v\n", initial, val)
170 return
171 }
172 for i := initial + 1; i <= final; i++ {
173 prevVal := val
174 val = s.WaitNext().(int)
175 expected := prevVal + 1
176 if val != expected {
177 err <- fmt.Errorf("Expecting %#v but got %#v\n", expected, val)
178 return
179 }
180 }
181 close(err)
182 }