This repository was archived by the owner on Aug 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmixer_test.go
141 lines (127 loc) · 2.82 KB
/
mixer_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
package mixer_test
import (
"context"
"reflect"
"testing"
"pipelined.dev/mixer"
"pipelined.dev/pipe"
"pipelined.dev/pipe/mock"
"pipelined.dev/signal"
)
func TestMixer(t *testing.T) {
type generator struct {
messages int
value float64
}
tests := []struct {
description string
generators []generator
expected []float64
}{
{
description: "1st run",
generators: []generator{
{
messages: 8,
value: 0.7,
},
{
messages: 6,
value: 0.5,
},
},
expected: []float64{0.6, 0.6, 0.6, 0.6, 0.6, 0.6, 0.7, 0.7},
},
{
description: "2nd run",
generators: []generator{
{
messages: 5,
value: 0.5,
},
{
messages: 4,
value: 0.7,
},
},
expected: []float64{0.6, 0.6, 0.6, 0.6, 0.5},
},
}
const (
// sampleRate = signal.SampleRate(44100)
numChannels = 1
bufferSize = 2
)
for _, test := range tests {
mixer := mixer.New(numChannels)
var lines []pipe.Line
for _, gen := range test.generators {
sourceAllocator := mock.Source{
Channels: numChannels,
Limit: gen.messages,
Value: gen.value,
}
line, _ := pipe.Routing{
Source: sourceAllocator.Source(),
Sink: mixer.Sink(),
}.Line(bufferSize)
lines = append(lines, line)
}
sink := mock.Sink{}
line, _ := pipe.Routing{
Source: mixer.Source(),
Sink: sink.Sink(),
}.Line(bufferSize)
lines = append(lines, line)
err := pipe.New(context.Background(), pipe.WithLines(lines...)).Wait()
assertEqual(t, "error", err, nil)
result := make([]float64, sink.Values.Len())
signal.ReadFloat64(sink.Values, result)
assertEqual(t, "result", result, test.expected)
}
}
func assertEqual(t *testing.T, name string, result, expected interface{}) {
t.Helper()
if !reflect.DeepEqual(expected, result) {
t.Fatalf("%v\nresult: \t%T\t%+v \nexpected: \t%T\t%+v", name, result, result, expected, expected)
}
}
func Test100Lines(t *testing.T) {
run(1, 512, 51200, 100)
}
func BenchmarkLimit(b *testing.B) {
for i := 0; i < b.N; i++ {
run(1, 512, i*2, 10)
}
}
func BenchmarkLines(b *testing.B) {
for i := 0; i < b.N; i++ {
run(1, 512, 51200, i+1)
}
}
func run(numChannels, bufferSize, limit, numLines int) {
mixer := mixer.New(numChannels)
var lines []pipe.Line
valueMultiplier := 1.0 / float64(numLines)
for i := 0; i < numLines; i++ {
sourceAllocator := mock.Source{
Channels: numChannels,
Limit: limit,
Value: float64(i) * valueMultiplier,
}
line, _ := pipe.Routing{
Source: sourceAllocator.Source(),
Sink: mixer.Sink(),
}.Line(bufferSize)
lines = append(lines, line)
}
sink := mock.Sink{
Discard: true,
}
line, _ := pipe.Routing{
Source: mixer.Source(),
Sink: sink.Sink(),
}.Line(bufferSize)
lines = append(lines, line)
pipe.New(context.Background(), pipe.WithLines(lines...)).Wait()
}