BAEL-3439 - Add selector.wakeup() section (#10398)
* Add selector.wakeup() section * Update SelectorTest.java * Update core-java-modules/core-java-nio-2/src/test/java/com/baeldung/selector/SelectorTest.java * Update SelectorTest.java * Update and rename SelectorTest.java to SelectorUnitTest.java * Update SelectorUnitTest.java * Update and rename SelectorUnitTest.java to SelectorManualTest.java
This commit is contained in:
parent
7d0f63b213
commit
4ab767bec2
@ -14,5 +14,12 @@
|
|||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../</relativePath>
|
<relativePath>../</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>3.6.1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
</project>
|
</project>
|
@ -0,0 +1,64 @@
|
|||||||
|
package com.baeldung.selector;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.channels.Pipe;
|
||||||
|
import java.nio.channels.SelectableChannel;
|
||||||
|
import java.nio.channels.Selector;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import static java.nio.channels.SelectionKey.OP_READ;
|
||||||
|
|
||||||
|
public class SelectorManualTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenWakeUpCalledOnSelector_thenBlockedThreadReturns() throws IOException, InterruptedException {
|
||||||
|
Pipe pipe = Pipe.open();
|
||||||
|
|
||||||
|
Selector selector = Selector.open();
|
||||||
|
SelectableChannel channel = pipe.source();
|
||||||
|
channel.configureBlocking(false);
|
||||||
|
channel.register(selector, OP_READ);
|
||||||
|
|
||||||
|
List<String> invocationStepsTracker = Collections.synchronizedList(new ArrayList<>());
|
||||||
|
|
||||||
|
CountDownLatch latch = new CountDownLatch(1);
|
||||||
|
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
invocationStepsTracker.add(">> Count down");
|
||||||
|
latch.countDown();
|
||||||
|
try {
|
||||||
|
invocationStepsTracker.add(">> Start select");
|
||||||
|
selector.select();
|
||||||
|
invocationStepsTracker.add(">> End select");
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
invocationStepsTracker.add(">> Start await");
|
||||||
|
thread.start();
|
||||||
|
latch.await();
|
||||||
|
invocationStepsTracker.add(">> End await");
|
||||||
|
|
||||||
|
invocationStepsTracker.add(">> Wakeup thread");
|
||||||
|
|
||||||
|
selector.wakeup();
|
||||||
|
channel.close();
|
||||||
|
|
||||||
|
assertThat(invocationStepsTracker)
|
||||||
|
.containsExactly(
|
||||||
|
">> Start await",
|
||||||
|
">> Count down",
|
||||||
|
">> Start select",
|
||||||
|
">> End await",
|
||||||
|
">> Wakeup thread",
|
||||||
|
">> End select"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user