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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
/*
* Copyright 2009 Phil Burk, Mobileer Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jsyn.engine;
import com.jsyn.io.AudioFifo;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.fail;
public class TestFifo {
@Test
public void testBasic() {
Thread watchdog = startWatchdog(600);
AudioFifo fifo = new AudioFifo();
fifo.setReadWaitEnabled(false);
fifo.allocate(8);
assertEquals(0, fifo.available(), "start empty");
assertEquals(Double.NaN, fifo.read(), "read back Nan when empty");
fifo.write(1.0);
assertEquals(1, fifo.available(), "added one value");
assertEquals(1.0, fifo.read(), "read back same value");
assertEquals(0, fifo.available(), "back to empty");
for (int i = 0; i < fifo.size(); i++) {
assertEquals(i, fifo.available(), "adding data");
fifo.write(100.0 + i);
}
for (int i = 0; i < fifo.size(); i++) {
assertEquals(fifo.size() - i, fifo.available(), "removing data");
assertEquals(100.0 + i, fifo.read(), "reading back data");
}
watchdog.interrupt();
}
@Test
public void testReadAvailable() {
Thread watchdog = startWatchdog(600);
AudioFifo fifo = new AudioFifo();
fifo.setReadWaitEnabled(false);
fifo.allocate(8);
final double x1 = 0.234;
fifo.write(x1);
final double x2 = 0.750;
fifo.write(x2);
assertEquals(2, fifo.available(), "added two values");
double[] buffer = new double[6];
int numRead = fifo.read(buffer, 2, buffer.length - 2);
assertEquals(2, numRead, "should have read two values");
assertEquals(x1, buffer[2], "read x1");
assertEquals(x2, buffer[3], "read x2");
watchdog.interrupt();
}
/**
* Wrap around several times to test masking.
*/
@Test
public void testWrapping() {
final int chunk = 5;
AudioFifo fifo = new AudioFifo();
fifo.allocate(8);
double value = 1000.0;
for (int i = 0; i < (fifo.size() * chunk); i++) {
value = checkFifoChunk(fifo, value, chunk);
}
}
private double checkFifoChunk(AudioFifo fifo, double value, int chunk) {
for (int i = 0; i < chunk; i++) {
assertEquals(i, fifo.available(), "adding data");
fifo.write(value + i);
}
for (int i = 0; i < chunk; i++) {
assertEquals(chunk - i, fifo.available(), "removing data");
assertEquals(value + i, fifo.read(), "reading back data");
}
return value + chunk;
}
@Test
public void testBadSize() {
try {
AudioFifo fifo = new AudioFifo();
fifo.allocate(20); // not power of 2
fail("should not get here");
} catch (IllegalArgumentException ignored) {
return;
}
fail("should have caught size exception");
}
@Test
public void testSingleReadWait() {
final int chunk = 5;
final AudioFifo fifo = new AudioFifo();
fifo.allocate(8);
fifo.setWriteWaitEnabled(false);
fifo.setReadWaitEnabled(true);
final double value = 50.0;
// Schedule a delayed write in another thread.
new Thread(() -> {
try {
Thread.sleep(200);
for (int i = 0; i < chunk; i++) {
fifo.write(value + i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
Thread watchdog = startWatchdog(500);
for (int i = 0; i < chunk; i++) {
assertEquals(value + i, fifo.read(), "reading back data");
}
watchdog.interrupt();
}
private Thread startWatchdog(final int msec) {
Thread watchdog = new Thread(() -> {
try {
Thread.sleep(msec);
fail("test must still be waiting");
} catch (InterruptedException ignored) {
}
});
watchdog.start();
return watchdog;
}
@Test
public void testSingleWriteWait() {
final int chunk = 13;
final AudioFifo fifo = new AudioFifo();
fifo.allocate(8);
fifo.setWriteWaitEnabled(true);
fifo.setReadWaitEnabled(true);
final double value = 50.0;
// Schedule a delayed read in another thread.
Thread readThread = new Thread(() -> {
try {
Thread.sleep(200);
for (int i = 0; i < chunk; i++) {
// LOGGER.debug( "testSingleWriteWait: try to read" );
double got = fifo.read();
assertEquals(value + i, got, "adding data");
// LOGGER.debug( "testSingleWriteWait: read " + got );
}
} catch (InterruptedException e) {
e.printStackTrace();
}
});
readThread.start();
Thread watchdog = startWatchdog(500);
// Try to write more than will fit so we will hang.
for (int i = 0; i < chunk; i++) {
fifo.write(value + i);
}
watchdog.interrupt();
try {
readThread.join(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
assertFalse(readThread.isAlive(), "readThread should be done.");
}
@Test
public void testBlockReadWait() {
final int chunk = 50;
final AudioFifo fifo = new AudioFifo();
fifo.allocate(8);
fifo.setWriteWaitEnabled(false);
fifo.setReadWaitEnabled(true);
final double value = 300.0;
double[] readBuffer = new double[chunk];
// Schedule delayed writes in another thread.
new Thread(() -> {
int numWritten = 0;
double[] writeBuffer = new double[4];
try {
while (numWritten < chunk) {
Thread.sleep(30);
for (int i = 0; i < writeBuffer.length; i++) {
writeBuffer[i] = value + numWritten;
numWritten += 1;
}
fifo.write(writeBuffer);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
Thread watchdog = startWatchdog(600);
fifo.read(readBuffer);
for (int i = 0; i < chunk; i++) {
assertEquals(value + i, readBuffer[i], "reading back data");
}
watchdog.interrupt();
}
@Test
public void testBlockReadAndWriteWaitStress() {
final int chunk = 3000000;
final AudioFifo fifo = new AudioFifo();
fifo.allocate(8);
fifo.setWriteWaitEnabled(true);
fifo.setReadWaitEnabled(true);
final double value = 50.0;
// Schedule a delayed write in another thread.
new Thread(() -> {
try {
Thread.sleep(200);
for (int i = 0; i < chunk; i++) {
fifo.write(value + i);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
// TODO Watchdog is apparently not working.
// I set the watchdog to be very short and it did not trigger.
Thread watchdog = startWatchdog(10 * 1000);
for (int i = 0; i < chunk; i++) {
assertEquals(value + i, fifo.read(), "reading back data");
}
watchdog.interrupt();
}
}
|