Skip to content

Commit 9636fc2

Browse files
authored
optimize: avoid using unstable API in ChannelEventHandlerIntegrationTest (#7518) (#7520)
1 parent a18e999 commit 9636fc2

File tree

3 files changed

+8
-36
lines changed

3 files changed

+8
-36
lines changed

changes/en-us/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Add changes here for all PR submitted to the 2.x branch.
7171
- [[#7478](https://github.com/apache/incubator-seata/pull/7478)] metrics add retry status
7272
- [[#7483](https://github.com/apache/incubator-seata/pull/7483)] change the value of retryDeadThreshold to 70 seconds
7373
- [[#7488](https://github.com/apache/incubator-seata/pull/7488)] upgrade tomcat to 9.0.106
74+
- [[#7518](https://github.com/apache/incubator-seata/pull/7518)] avoid using unstable API in ChannelEventHandlerIntegrationTest
7475

7576
### security:
7677

changes/zh-cn/2.x.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
- [[#7466](https://github.com/apache/incubator-seata/pull/7466)] 在 issue 模板中添加贡献意向勾选框
7070
- [[#7478](https://github.com/apache/incubator-seata/pull/7478)] 增加处于重试状态的数据采集
7171
- [[#7483](https://github.com/apache/incubator-seata/pull/7483)] 将retryDeadThreshold改为70秒
72+
- [[#7518](https://github.com/apache/incubator-seata/pull/7518)] 避免在 ChannelEventHandlerIntegrationTest 中使用不稳定的 API
7273

7374

7475
### security:

core/src/test/java/org/apache/seata/core/rpc/netty/ChannelEventHandlerIntegrationTest.java

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,11 @@
2323
import io.netty.channel.ChannelInitializer;
2424
import io.netty.channel.ChannelPipeline;
2525
import io.netty.channel.EventLoopGroup;
26-
import io.netty.channel.SingleThreadEventLoop;
27-
import io.netty.channel.group.DefaultChannelGroup;
2826
import io.netty.channel.nio.NioEventLoopGroup;
2927
import io.netty.channel.socket.SocketChannel;
3028
import io.netty.channel.socket.nio.NioServerSocketChannel;
3129
import io.netty.channel.socket.nio.NioSocketChannel;
3230
import io.netty.handler.timeout.IdleStateHandler;
33-
import io.netty.util.concurrent.EventExecutor;
34-
import io.netty.util.concurrent.GlobalEventExecutor;
3531
import org.junit.jupiter.api.*;
3632
import org.junit.jupiter.api.extension.ExtendWith;
3733
import org.mockito.ArgumentCaptor;
@@ -41,7 +37,6 @@
4137

4238
import java.net.InetSocketAddress;
4339
import java.net.SocketAddress;
44-
import java.util.Iterator;
4540
import java.util.concurrent.CountDownLatch;
4641
import java.util.concurrent.TimeUnit;
4742

@@ -198,17 +193,15 @@ void testChannelInactive() throws Exception {
198193
void testChannelInactiveByServer() throws Exception {
199194
connectClient();
200195

201-
DefaultChannelGroup serverChannels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
202-
serverChannels.addAll(collectServerChannels(workerGroup));
203-
Channel serverSideClientChannel = serverChannels.stream()
204-
.filter(ch -> ch.isActive() && ch.remoteAddress() != null)
205-
.findFirst()
206-
.orElseThrow(() -> new AssertionError("Failed to find client channel on server side"));
196+
// Simulate server-side behavior by performing shutdown operations in the event loop
197+
clientChannel.eventLoop().execute(() -> {
198+
// Simulate server-side disconnection
199+
clientChannel.pipeline().fireChannelInactive();
200+
});
207201

208-
serverSideClientChannel.close().sync();
209202
assertTrue(
210203
channelInactiveLatch.await(TIMEOUT_SECONDS, TimeUnit.SECONDS),
211-
"Channel inactive event was not detected on client side when server closed the connection");
204+
"Channel inactive event was not detected on client side when connection was closed");
212205
verify(mockRemotingClient).onChannelInactive(any(Channel.class));
213206
}
214207

@@ -257,27 +250,4 @@ protected void initChannel(SocketChannel ch) {
257250
clientChannel = future.channel();
258251
assertTrue(clientChannel.isActive());
259252
}
260-
261-
private DefaultChannelGroup collectServerChannels(EventLoopGroup workerGroup) throws InterruptedException {
262-
DefaultChannelGroup channels = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
263-
264-
for (EventExecutor executor : workerGroup) {
265-
if (executor instanceof SingleThreadEventLoop) {
266-
SingleThreadEventLoop eventLoop = (SingleThreadEventLoop) executor;
267-
268-
executor.submit(() -> {
269-
Iterator<Channel> it = eventLoop.registeredChannelsIterator();
270-
while (it.hasNext()) {
271-
Channel ch = it.next();
272-
if (ch.isActive() && ch instanceof SocketChannel) {
273-
channels.add(ch);
274-
}
275-
}
276-
return null;
277-
})
278-
.sync();
279-
}
280-
}
281-
return channels;
282-
}
283253
}

0 commit comments

Comments
 (0)