-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathproperty_test.go
93 lines (87 loc) · 2.04 KB
/
property_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
package observer
import (
"sync"
"testing"
)
func TestPropertyInitialValue(t *testing.T) {
prop := NewProperty(10)
if val := prop.Value(); val != 10 {
t.Fatalf("Expecting 10 but got %#v\n", val)
}
stream := prop.Observe()
for i := 0; i <= 100; i++ {
if val := stream.Value(); val != 10 {
t.Fatalf("Expecting 10 but got %#v\n", val)
}
}
}
func TestPropertyInitialObserve(t *testing.T) {
prop := NewProperty(10)
var prevStream Stream[int]
for i := 0; i <= 100; i++ {
stream := prop.Observe()
if stream == prevStream {
t.Fatalf("Expecting different stream\n")
}
if val := stream.Value(); val != 10 {
t.Fatalf("Expecting 10 but got %#v\n", val)
}
prevStream = stream
}
}
func TestPropertyObserveAfterUpdate(t *testing.T) {
prop := NewProperty(10)
if val := prop.Value(); val != 10 {
t.Fatalf("Expecting 10 but got %#v\n", val)
}
prop.Update(15)
if val := prop.Value(); val != 15 {
t.Fatalf("Expecting 15 but got %#v\n", val)
}
stream := prop.Observe()
if val := stream.Value(); val != 15 {
t.Fatalf("Expecting 15 but got %#v\n", val)
}
}
func TestPropertyMultipleConcurrentReaders(t *testing.T) {
initial := 1000
final := 2000
prop := NewProperty(initial)
var cherrs []chan error
for i := 0; i < 1000; i++ {
cherr := make(chan error, 1)
cherrs = append(cherrs, cherr)
go testStreamRead(prop.Observe(), initial, final, cherr)
}
done := make(chan bool)
go func(prop Property[int], initial, final int, done chan bool) {
defer close(done)
for i := initial + 1; i <= final; i++ {
prop.Update(i)
}
}(prop, initial, final, done)
for _, cherr := range cherrs {
if err := <-cherr; err != nil {
t.Fatal(err)
}
}
<-done
}
func TestPropertyMultipleConcurrentReadersWriters(t *testing.T) {
wg := &sync.WaitGroup{}
writer := func(prop Property[int], times int) {
defer wg.Done()
for i := 0; i <= times; i++ {
val := prop.Value()
prop.Update(val + 1)
prop.Observe()
}
}
prop := NewProperty(0)
times := 1000
for i := 0; i < 1000; i++ {
wg.Add(1)
go writer(prop, times)
}
wg.Wait()
}