-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream_test.go
63 lines (57 loc) · 1.48 KB
/
stream_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
package krpcgo
import (
"context"
"fmt"
"sync"
"testing"
"time"
"github.com/atburke/krpc-go/lib/utils"
"github.com/stretchr/testify/require"
)
func TestStreamManager(t *testing.T) {
streamCounts := []int{0, 1, 2, 10}
input := []string{"this", "is", "the", "test", "input"}
for _, numStreams := range streamCounts {
numStreams := numStreams
t.Run(fmt.Sprintf("%v stream(s) listening", numStreams), func(t *testing.T) {
sm := newStreamManager(0)
streamData := map[int][]string{}
mu := sync.Mutex{}
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
for i := 0; i < numStreams; i++ {
i := i
stream := sm.newStream()
go func() {
for {
select {
case data := <-stream.C:
mu.Lock()
streamData[i] = append(streamData[i], string(data))
mu.Unlock()
case <-ctx.Done():
return
}
}
}()
}
// HACK: Stream values will likely be dropped if they're sent too
// quickly. Fortunately, in a real stream a) there will be a delay
// between updates anyway and b) it's ok to drop a few values.
for _, s := range input {
time.Sleep(10 * time.Millisecond)
sm.write([]byte(s))
}
require.Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
for _, data := range streamData {
if !utils.SlicesEqual(data, input) {
return false
}
}
return true
}, 1000*time.Millisecond, 100*time.Millisecond, "map contents: %v", streamData)
})
}
}