-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroadcaster_test.go
164 lines (150 loc) · 3.17 KB
/
broadcaster_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package parallel
import (
"context"
"reflect"
"testing"
)
type StringMap map[string]string
func (sm StringMap) Clone() any {
if sm == nil {
return nil
}
result := StringMap{}
for k, v := range sm {
result[k] = v
}
return result
}
func cat[T Clonable](ctx context.Context, v T) (T, error) {
return v, nil
}
func appendToSlice[T any](ctx context.Context, sm T, result []T) ([]T, bool, error) {
if result == nil {
return []T{sm}, false, nil
}
return append(result, sm), false, nil
}
func TestBroadcaster1(t *testing.T) {
gen := NewTapFromSlice([]StringMap{
{
"zero": "0",
"one": "1",
"two": "2",
},
{
"one": "1",
"two": "2",
"three": "3",
"four": "4",
},
{
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
},
})
p0 := Compose(NewMapper(1, cat[StringMap]), NewReducer(1, appendToSlice[StringMap]))
p1 := Compose(NewMapper(1, cat[StringMap]), NewReducer(1, appendToSlice[StringMap]))
p2 := Compose(NewMapper(1, cat[StringMap]), NewReducer(1, appendToSlice[StringMap]))
bc := NewBroadcaster(p0, p1, p2)
gen.Join(bc)
gen.Run(context.Background())
expected := []StringMap{
{
"zero": "0",
"one": "1",
"two": "2",
},
{
"one": "1",
"two": "2",
"three": "3",
"four": "4",
},
{
"two": "2",
"three": "3",
"four": "4",
"five": "5",
"six": "6",
},
}
for _, p := range []Producer[Data[[]StringMap]]{p0, p1, p2} {
result := <-p.Out()
if !reflect.DeepEqual(expected, result.Value()) {
t.Errorf("%v expected but got %v", expected, result.Value())
}
}
}
func squareValue[K comparable, V int | int8 | int16 | int32 | int64](ctx context.Context, kv *KeyValue[K, V]) (*KeyValue[K, V], error) {
if kv == nil {
return nil, nil
}
return &KeyValue[K, V]{kv.Key, kv.Value * kv.Value}, nil
}
func makeMap[K comparable, V any](ctx context.Context, kv *KeyValue[K, V], accum map[K]V) (map[K]V, bool, error) {
if kv == nil {
return accum, false, nil
}
if accum == nil {
return map[K]V{kv.Key: kv.Value}, false, nil
}
accum[kv.Key] = kv.Value
return accum, false, nil
}
func TestBroadcaster2(t *testing.T) {
ctx := context.Background()
gen := NewTapFromMap(map[string]int{
"zero": 0,
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
})
pls := []Processor[Data[*KeyValue[string, int]], Data[map[string]int]]{
Compose(NewMapper(1, cat[*KeyValue[string, int]]), NewReducer(1, makeMap[string, int])),
Compose(NewMapper(1, squareValue[string, int]), NewReducer(1, makeMap[string, int])),
}
bc := NewBroadcaster(pls[0], pls[1])
gen.Join(bc)
gen.Run(ctx)
expected := []map[string]int{
{
"zero": 0,
"one": 1,
"two": 2,
"three": 3,
"four": 4,
"five": 5,
"six": 6,
"seven": 7,
"eight": 8,
"nine": 9,
},
{
"zero": 0,
"one": 1,
"two": 4,
"three": 9,
"four": 16,
"five": 25,
"six": 36,
"seven": 49,
"eight": 64,
"nine": 81,
},
}
for i := range pls {
result := <-pls[i].Out()
if !reflect.DeepEqual(expected[i], result.Value()) {
t.Errorf("%v expected but got %v", expected[i], result.Value())
}
}
}