View Javadoc
1   /*
2    * Copyright 2026 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * Verifies that {@link io.netty.channel.IoHandler#run(io.netty.channel.IoHandlerContext)}
32   * returns only the count of real I/O events, excluding internal events such as wakeups and timer expirations.
33   * <p>
34   * Subclasses provide the {@link IoHandlerFactory} for each transport. The contract tested here
35   * must hold regardless of the transport implementation.
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              // Block until the scheduled task fires and is picked up by runAllTasks.
96              // The handler may return slightly before the task deadline due to timer precision,
97              // so we loop until the task actually executes.
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 }