Skip to content

Commit 58c7831

Browse files
authored
test: make three timing-dependent tests deterministic (#525)
* test: hold the deferred sink changes until the tests have looked The thread making the native calls deferred from a callback could make them before the test checked that they had not been made yet. * test: end the runtime close only after close() has timed out Both waits took 100 ms: a close() that woke up late found closing ended and reported no timeout. * test: keep the frame handler busy until the decoder got ahead A handler sleeping 200 ms per frame fell behind only where the encoder sent more than about five frames a second, which slow runners did not.
1 parent 902c370 commit 58c7831

3 files changed

Lines changed: 147 additions & 52 deletions

File tree

‎webrtc/src/test/java/dev/onvoid/webrtc/client/MediaTests.java‎

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -375,47 +375,59 @@ void slowFrameHandlerGetsNewest() throws Exception {
375375
Peer[] peers = pair();
376376
CustomVideo video = rtc.media().customVideo("video");
377377
AtomicInteger handled = new AtomicInteger();
378-
CompletableFuture<Void> enough = new CompletableFuture<>();
378+
CompletableFuture<Void> busy = new CompletableFuture<>();
379+
CompletableFuture<Void> resumed = new CompletableFuture<>();
380+
CountDownLatch done = new CountDownLatch(1);
379381

380382
peers[1].onTrack(track -> track.asVideo().onFrame(frame -> {
381-
try {
382-
// Much slower than the 30 frames per second sent.
383-
Thread.sleep(200);
384-
}
385-
catch (InterruptedException e) {
386-
Thread.currentThread().interrupt();
383+
int count = handled.incrementAndGet();
384+
385+
if (count == 3) {
386+
// Busy until the test lets it go, however fast frames come.
387+
busy.complete(null);
388+
389+
try {
390+
done.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
391+
}
392+
catch (InterruptedException e) {
393+
Thread.currentThread().interrupt();
394+
}
387395
}
388-
if (handled.incrementAndGet() == 3) {
389-
enough.complete(null);
396+
else if (count > 3) {
397+
resumed.complete(null);
390398
}
391399
}));
392400
peers[0].addTrack(video);
393401

394402
feedVideo(video, 160, 120);
395403

396-
await(enough);
404+
await(busy);
397405

398-
// The decoder keeps going while the handler is busy: within the same
399-
// time it gets well ahead of the handler, at whatever rate the encoder
400-
// has ramped up to on this machine.
406+
// The decoder keeps going while the handler is busy, at whatever rate
407+
// the encoder has ramped up to on this machine, and the handler gets
408+
// none of those frames meanwhile.
401409
long decodedBefore = framesDecoded(await(peers[1].stats()));
402-
int handledBefore = handled.get();
403410
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(TIMEOUT_SECONDS);
404411
long decodedMeanwhile;
405-
int handledMeanwhile;
406412

407-
do {
408-
Thread.sleep(100);
409-
decodedMeanwhile = framesDecoded(await(peers[1].stats())) - decodedBefore;
410-
handledMeanwhile = handled.get() - handledBefore;
411-
}
412-
while ((decodedMeanwhile < 10 || decodedMeanwhile < handledMeanwhile + 5) && System.nanoTime() < deadline);
413+
try {
414+
do {
415+
Thread.sleep(100);
416+
decodedMeanwhile = framesDecoded(await(peers[1].stats())) - decodedBefore;
417+
}
418+
while (decodedMeanwhile < 10 && System.nanoTime() < deadline);
419+
420+
long decoded = decodedMeanwhile;
413421

414-
long decoded = decodedMeanwhile;
415-
int handledFrames = handledMeanwhile;
422+
assertTrue(decoded >= 10, () -> "Meanwhile decoded " + decoded + " frames");
423+
assertEquals(3, handled.get());
424+
}
425+
finally {
426+
done.countDown();
427+
}
416428

417-
assertTrue(decoded >= 10 && decoded >= handledFrames + 5,
418-
() -> "Meanwhile decoded " + decoded + " frames and handled " + handledFrames);
429+
// Then gets the newest frame, and the ones after it.
430+
await(resumed);
419431
}
420432

421433
private static long framesDecoded(PeerStats stats) {

‎webrtc/src/test/java/dev/onvoid/webrtc/client/RuntimeCloseTests.java‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.time.Duration;
2727
import java.util.concurrent.CompletableFuture;
28+
import java.util.concurrent.CountDownLatch;
2829
import java.util.concurrent.ExecutionException;
2930
import java.util.concurrent.RejectedExecutionException;
3031
import java.util.concurrent.TimeUnit;
@@ -153,9 +154,24 @@ void closeWaitsForTasksQueuedBehindTeardown() throws Exception {
153154
void closeTimeoutReported() throws Exception {
154155
// The peer's teardown never runs, so neither close finishes.
155156
ManualExecutor control = new ManualExecutor();
157+
CountDownLatch closeReturned = new CountDownLatch(1);
156158
WebRtc runtime = WebRtc.builder()
157159
.audio(AudioMode.HEADLESS)
158-
.errorHandler(errors::add)
160+
.errorHandler(error -> {
161+
errors.add(error);
162+
163+
// Closing ends only after close() has returned. Otherwise a
164+
// close() that wakes up late finds it ended and has no
165+
// timeout to report.
166+
if (error.getMessage().contains("left undisposed")) {
167+
try {
168+
closeReturned.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
169+
}
170+
catch (InterruptedException e) {
171+
Thread.currentThread().interrupt();
172+
}
173+
}
174+
})
159175
.controlPool(control)
160176
.closeTimeout(Duration.ofMillis(100))
161177
.build();
@@ -164,7 +180,12 @@ void closeTimeoutReported() throws Exception {
164180
await(created);
165181

166182
try {
167-
runtime.close();
183+
try {
184+
runtime.close();
185+
}
186+
finally {
187+
closeReturned.countDown();
188+
}
168189

169190
// The runtime cannot tell whether WebRTC still uses the factory,
170191
// so it leaves it. That outlasts close(), whose wait ran out first.

‎webrtc/src/test/java/dev/onvoid/webrtc/internal/SinkRegistryTests.java‎

Lines changed: 87 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -169,19 +169,21 @@ void removeInCallbackIsDeferred() throws Exception {
169169
return add().add(s, r);
170170
});
171171

172-
assertTrue(registration.get().enter());
172+
try (HeldChanges held = holdDeferredChanges()) {
173+
assertTrue(registration.get().enter());
173174

174-
try {
175-
// WebRTC holds its lock here, so the native removal must wait.
176-
registry.remove(sink);
175+
try {
176+
// WebRTC holds its lock here, so the native removal must wait.
177+
registry.remove(sink);
177178

178-
assertEquals(List.of("add 1"), calls);
179+
assertEquals(List.of("add 1"), calls);
179180

180-
// Removed for the Java side at once.
181-
assertFalse(registration.get().enter());
182-
}
183-
finally {
184-
registration.get().exit();
181+
// Removed for the Java side at once.
182+
assertFalse(registration.get().enter());
183+
}
184+
finally {
185+
registration.get().exit();
186+
}
185187
}
186188

187189
SinkRegistry.awaitDeferred();
@@ -194,16 +196,18 @@ void addInCallbackIsDeferred() throws Exception {
194196
SinkRegistry.Registration callback = callback();
195197
Object sink = new Object();
196198

197-
assertTrue(callback.enter());
199+
try (HeldChanges held = holdDeferredChanges()) {
200+
assertTrue(callback.enter());
198201

199-
try {
200-
registry.add(sink, add());
201-
registry.add(sink, add());
202+
try {
203+
registry.add(sink, add());
204+
registry.add(sink, add());
202205

203-
assertEquals(List.of("add 1"), calls);
204-
}
205-
finally {
206-
callback.exit();
206+
assertEquals(List.of("add 1"), calls);
207+
}
208+
finally {
209+
callback.exit();
210+
}
207211
}
208212

209213
SinkRegistry.awaitDeferred();
@@ -216,14 +220,16 @@ void addAndRemoveInCallback() throws Exception {
216220
SinkRegistry.Registration callback = callback();
217221
Object sink = new Object();
218222

219-
assertTrue(callback.enter());
223+
try (HeldChanges held = holdDeferredChanges()) {
224+
assertTrue(callback.enter());
220225

221-
try {
222-
registry.add(sink, add());
223-
registry.remove(sink);
224-
}
225-
finally {
226-
callback.exit();
226+
try {
227+
registry.add(sink, add());
228+
registry.remove(sink);
229+
}
230+
finally {
231+
callback.exit();
232+
}
227233
}
228234

229235
SinkRegistry.awaitDeferred();
@@ -294,6 +300,47 @@ private SinkRegistry.Registration callback() {
294300
return registration.get();
295301
}
296302

303+
/**
304+
* Holds the thread that makes the native calls deferred from callbacks,
305+
* so that those a test defers wait until it closes the returned hold:
306+
* otherwise that thread may make them before the test looks.
307+
*/
308+
private static HeldChanges holdDeferredChanges() throws InterruptedException {
309+
HeldChanges held = new HeldChanges();
310+
AtomicReference<SinkRegistry.Registration> callback = new AtomicReference<>();
311+
312+
new SinkRegistry<>(handle -> { }).add(new Object(), (s, r) -> {
313+
callback.set(r);
314+
return 1;
315+
});
316+
317+
assertTrue(callback.get().enter());
318+
319+
try {
320+
// Deferred, and made first: the deferred calls are made one after
321+
// another, so the ones deferred later wait for this one.
322+
new SinkRegistry<>(handle -> { }).add(new Object(), (s, r) -> {
323+
held.entered.countDown();
324+
325+
try {
326+
assertTrue(held.released.await(5, TimeUnit.SECONDS));
327+
}
328+
catch (InterruptedException e) {
329+
throw new IllegalStateException(e);
330+
}
331+
332+
return 1;
333+
});
334+
}
335+
finally {
336+
callback.get().exit();
337+
}
338+
339+
assertTrue(held.entered.await(5, TimeUnit.SECONDS));
340+
341+
return held;
342+
}
343+
297344
private SinkRegistry.NativeAdd<Object> add() {
298345
return (sink, registration) -> {
299346
long handle = nextHandle.incrementAndGet();
@@ -304,6 +351,21 @@ private SinkRegistry.NativeAdd<Object> add() {
304351

305352

306353

354+
/** The thread of the deferred native calls, held until closed. */
355+
private static final class HeldChanges implements AutoCloseable {
356+
357+
private final CountDownLatch entered = new CountDownLatch(1);
358+
359+
private final CountDownLatch released = new CountDownLatch(1);
360+
361+
362+
@Override
363+
public void close() {
364+
released.countDown();
365+
}
366+
367+
}
368+
307369
/** A native add that waits, once it has been entered, until the test lets it return. */
308370
private final class BlockedAdd implements SinkRegistry.NativeAdd<Object> {
309371

0 commit comments

Comments
 (0)