-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_test.go
More file actions
90 lines (75 loc) · 1.35 KB
/
static_test.go
File metadata and controls
90 lines (75 loc) · 1.35 KB
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
package demux_test
import (
"sync"
"testing"
"time"
"github.com/floatdrop/demux"
)
func TestStatic_RoutesToCorrectChannels(t *testing.T) {
in := make(chan int)
outA := make(chan int, 2)
outB := make(chan int, 2)
channels := map[string]chan<- int{
"A": outA,
"B": outB,
}
keyFunc := func(i int) string {
if i%2 == 0 {
return "A"
}
return "B"
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
demux.Static(in, keyFunc, channels)
}()
// Send data
go func() {
in <- 1
in <- 2
in <- 3
in <- 4
close(in)
}()
wg.Wait()
// Collect output
var aValues, bValues []int
done := make(chan struct{})
go func() {
for v := range outA {
aValues = append(aValues, v)
}
for v := range outB {
bValues = append(bValues, v)
}
close(done)
}()
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Test timed out")
}
// Validate
expectedA := []int{2, 4}
expectedB := []int{1, 3}
if !equal(aValues, expectedA) {
t.Errorf("expected A channel values %v, got %v", expectedA, aValues)
}
if !equal(bValues, expectedB) {
t.Errorf("expected B channel values %v, got %v", expectedB, bValues)
}
}
// Helper for slice comparison
func equal[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}