-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathBlockingEventLoopTest.java
More file actions
63 lines (52 loc) · 2.04 KB
/
Copy pathBlockingEventLoopTest.java
File metadata and controls
63 lines (52 loc) · 2.04 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
/*
* Copyright 2013-2025 chronicle.software; SPDX-License-Identifier: Apache-2.0
*/
package net.openhft.chronicle.threads;
import net.openhft.chronicle.core.Jvm;
import net.openhft.chronicle.core.threads.InterruptedRuntimeException;
import org.junit.jupiter.api.Test;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.jupiter.api.Assertions.*;
/**
* Verifies that a handler in a {@link BlockingEventLoop} is interrupted when
* the loop is stopped while the calling thread continues unimpeded.
*/
class BlockingEventLoopTest extends ThreadsTestCommon {
@Test
void handlersAreInterruptedOnStop() throws TimeoutException {
try (final BlockingEventLoop el = new BlockingEventLoop("test-blocking-loop")) {
el.start();
AtomicBoolean wasStoppedSuccessfully = new AtomicBoolean(false);
CyclicBarrier barrier = new CyclicBarrier(2);
el.addHandler(() -> {
waitQuietly(barrier);
while (!Thread.currentThread().isInterrupted()) {
Jvm.pause(10);
}
wasStoppedSuccessfully.set(true);
return false;
});
waitQuietly(barrier);
Jvm.pause(10);
el.stop();
TimingPauser pauser = Pauser.balanced();
while (!wasStoppedSuccessfully.get()) {
pauser.pause(1, TimeUnit.SECONDS);
}
assertTrue(wasStoppedSuccessfully.get());
assertFalse(Thread.currentThread().isInterrupted());
}
}
private void waitQuietly(CyclicBarrier barrier) {
try {
barrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
Thread.currentThread().interrupt();
throw new InterruptedRuntimeException("Interrupted waiting at barrier");
}
}
}