1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.testsuite.transport;
17
18 import io.netty.channel.IoHandlerFactory;
19 import io.netty.channel.ManualIoEventLoop;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.Timeout;
22
23 import java.util.concurrent.CountDownLatch;
24 import java.util.concurrent.TimeUnit;
25 import java.util.concurrent.atomic.AtomicBoolean;
26
27 import static org.junit.jupiter.api.Assertions.assertEquals;
28 import static org.junit.jupiter.api.Assertions.assertTrue;
29
30
31
32
33
34
35
36
37 public abstract class AbstractIoHandlerEventCountTest {
38
39 protected abstract IoHandlerFactory newIoHandlerFactory();
40
41 @Test
42 @Timeout(5)
43 public void testTimerExpirationDoesNotCountAsIoEvent() throws Exception {
44 ManualIoEventLoop loop = new ManualIoEventLoop(Thread.currentThread(), newIoHandlerFactory());
45 try {
46 int result = loop.run(TimeUnit.MILLISECONDS.toNanos(100), -1);
47 assertEquals(0, result, "timer expiration should not be counted as an I/O event");
48 } finally {
49 loop.shutdown();
50 while (!loop.isTerminated()) {
51 loop.runNow();
52 }
53 }
54 }
55
56 @Test
57 @Timeout(5)
58 public void testWakeupDoesNotCountAsIoEvent() throws Exception {
59 ManualIoEventLoop loop = new ManualIoEventLoop(Thread.currentThread(), newIoHandlerFactory());
60 try {
61 CountDownLatch aboutToBlock = new CountDownLatch(1);
62 Thread waker = new Thread(() -> {
63 try {
64 aboutToBlock.await();
65 Thread.sleep(300);
66 } catch (InterruptedException e) {
67 Thread.currentThread().interrupt();
68 return;
69 }
70 loop.wakeup();
71 });
72 waker.start();
73
74 aboutToBlock.countDown();
75 int result = loop.run(0, -1);
76 assertEquals(0, result, "wakeup should not be counted as an I/O event");
77
78 waker.join();
79 } finally {
80 loop.shutdown();
81 while (!loop.isTerminated()) {
82 loop.runNow();
83 }
84 }
85 }
86
87 @Test
88 @Timeout(5)
89 public void testScheduledTaskDoesNotInflateIoCount() throws Exception {
90 ManualIoEventLoop loop = new ManualIoEventLoop(Thread.currentThread(), newIoHandlerFactory());
91 try {
92 AtomicBoolean taskRan = new AtomicBoolean();
93 loop.schedule(() -> taskRan.set(true), 100, TimeUnit.MILLISECONDS);
94
95
96
97
98 int result = 0;
99 while (!taskRan.get()) {
100 result += loop.run(0);
101 }
102 assertEquals(1, result,
103 "only the scheduled task should be counted, not internal timer events");
104 } finally {
105 loop.shutdown();
106 while (!loop.isTerminated()) {
107 loop.runNow();
108 }
109 }
110 }
111 }