-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue: #6774
- Loading branch information
Showing
2 changed files
with
81 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
core/src/test/java/org/apache/seata/core/rpc/netty/NettyClientBootstrapTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.apache.seata.core.rpc.netty; | ||
|
||
import io.netty.channel.EventLoopGroup; | ||
import io.netty.util.concurrent.DefaultEventExecutorGroup; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class NettyClientBootstrapTest { | ||
|
||
@Mock | ||
private NettyClientConfig nettyClientConfig; | ||
private DefaultEventExecutorGroup eventExecutorGroup; | ||
|
||
@BeforeEach | ||
void init() { | ||
eventExecutorGroup = new DefaultEventExecutorGroup(1); | ||
} | ||
|
||
@Test | ||
void testSharedEventLoopGroupEnabled() { | ||
TmNettyRemotingClient tmNettyRemotingClient = TmNettyRemotingClient.getInstance(); | ||
RmNettyRemotingClient rmNettyRemotingClient = RmNettyRemotingClient.getInstance(); | ||
|
||
tmNettyRemotingClient.init(); | ||
rmNettyRemotingClient.init(); | ||
|
||
NettyClientBootstrap tmBootstrap = getNettyClientBootstrap(tmNettyRemotingClient); | ||
NettyClientBootstrap rmBootstrap = getNettyClientBootstrap(rmNettyRemotingClient); | ||
|
||
EventLoopGroup tmEventLoopGroupWorker = getEventLoopGroupWorker(tmBootstrap); | ||
EventLoopGroup rmEventLoopGroupWorker = getEventLoopGroupWorker(rmBootstrap); | ||
|
||
Assertions.assertEquals(tmEventLoopGroupWorker, rmEventLoopGroupWorker); | ||
} | ||
|
||
@Test | ||
void testSharedEventLoopGroupDisabled() { | ||
when(nettyClientConfig.getEnableClientSharedEventLoop()).thenReturn(false); | ||
NettyClientBootstrap tmNettyClientBootstrap = new NettyClientBootstrap(nettyClientConfig, eventExecutorGroup, NettyPoolKey.TransactionRole.TMROLE); | ||
EventLoopGroup tmEventLoopGroupWorker = getEventLoopGroupWorker(tmNettyClientBootstrap); | ||
|
||
NettyClientBootstrap rmNettyClientBootstrap = new NettyClientBootstrap(nettyClientConfig, eventExecutorGroup, NettyPoolKey.TransactionRole.RMROLE); | ||
EventLoopGroup rmEventLoopGroupWorker = getEventLoopGroupWorker(rmNettyClientBootstrap); | ||
|
||
Assertions.assertNotEquals(tmEventLoopGroupWorker, rmEventLoopGroupWorker); | ||
} | ||
|
||
private NettyClientBootstrap getNettyClientBootstrap(AbstractNettyRemotingClient remotingClient) { | ||
try { | ||
java.lang.reflect.Field field = AbstractNettyRemotingClient.class.getDeclaredField("clientBootstrap"); | ||
field.setAccessible(true); | ||
return (NettyClientBootstrap) field.get(remotingClient); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private EventLoopGroup getEventLoopGroupWorker(NettyClientBootstrap bootstrap) { | ||
try { | ||
java.lang.reflect.Field field = NettyClientBootstrap.class.getDeclaredField("eventLoopGroupWorker"); | ||
field.setAccessible(true); | ||
return (EventLoopGroup) field.get(bootstrap); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |