-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathasync_subject_test.go
77 lines (64 loc) · 2.18 KB
/
async_subject_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
package co_test
import (
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
"go.tempura.ink/co"
)
func TestAsyncSubject(t *testing.T) {
convey.Convey("given a sequential int", t, func() {
expected := []int{1, 4, 5, 6, 7, 2, 2, 3, 4, 5, 12, 4, 2, 3, 43, 127, 37598, 34, 34, 123, 123}
subject := co.NewAsyncSubject[int]()
go func() {
time.Sleep(time.Second)
for i, val := range expected {
subject.Next(val)
time.Sleep(time.Millisecond * (100 + time.Duration(i)*10))
}
subject.Complete()
}()
convey.Convey("expect resolved list to be identical with given values", func() {
actual, pre, intervals := []int{}, time.Now(), []time.Duration{}
for data := range subject.Iter() {
actual = append(actual, data)
intervals = append(intervals, time.Since(pre))
pre = time.Now()
}
convey.So(actual, convey.ShouldResemble, expected)
intervals = intervals[1:]
convey.Printf("We have a list of interval %+v\n", intervals)
for i := range intervals {
convey.So(intervals[i], convey.ShouldBeGreaterThan, time.Millisecond*(100+time.Duration(i)*10))
}
})
})
}
func TestAsyncSubjectWait1Sec(t *testing.T) {
convey.Convey("given a sequential int", t, func() {
source := []int{1, 4, 5, 6, 7, 2, 2, 3, 4, 5, 12, 4, 2, 3, 43, 127, 37598, 34, 34, 123, 123}
subject := co.NewAsyncSubject[int]()
go func() {
for i, val := range source {
subject.Next(val)
time.Sleep(time.Millisecond * (100 + time.Duration(i)*10))
}
subject.Complete()
}()
convey.Convey("expect resolved list to be identical with given values", func() {
time.Sleep(time.Second)
expected := []int{3, 4, 5, 12, 4, 2, 3, 43, 127, 37598, 34, 34, 123, 123}
actual, pre, intervals := []int{}, time.Now(), []time.Duration{}
for data := range subject.Iter() {
actual = append(actual, data)
intervals = append(intervals, time.Since(pre))
pre = time.Now()
}
convey.So(actual, convey.ShouldResemble, expected)
intervals = intervals[2:]
convey.Printf("We have a list of interval %+v\n", intervals)
for i := range intervals {
convey.So(intervals[i], convey.ShouldBeGreaterThan, time.Millisecond*(100+time.Duration(i)*10))
}
})
})
}