use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.
the class InboundDecoderTest method testPartialReadWithReuseBuffer.
/**
* Tests that an {@link InboundDecoder} can handle a {@link ByteBuf} where reader index is not {@code 0} at the start of the {@link
* InboundDecoder#decode}.
*
* @throws Exception If failed.
*/
@Test
public void testPartialReadWithReuseBuffer() throws Exception {
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
var channel = new EmbeddedChannel();
Mockito.doReturn(channel).when(ctx).channel();
var serializationService = new SerializationService(registry, mock(UserObjectSerializationContext.class));
var perSessionSerializationService = new PerSessionSerializationService(serializationService);
final var decoder = new InboundDecoder(perSessionSerializationService);
final var list = new ArrayList<>();
var writer = new DirectMessageWriter(perSessionSerializationService, ConnectionManager.DIRECT_PROTOCOL_VERSION);
var msg = new TestMessagesFactory().testMessage().msg("abcdefghijklmn").build();
MessageSerializer<NetworkMessage> serializer = registry.createSerializer(msg.groupType(), msg.messageType());
ByteBuffer nioBuffer = ByteBuffer.allocate(10_000);
writer.setBuffer(nioBuffer);
// Write message to the ByteBuffer.
boolean fullyWritten = serializer.writeMessage(msg, writer);
assertTrue(fullyWritten);
nioBuffer.flip();
ByteBuf buffer = allocator.buffer();
// Write first 3 bytes of a message.
for (int i = 0; i < 3; i++) {
buffer.writeByte(nioBuffer.get());
}
decoder.decode(ctx, buffer, list);
// At this point a header and a first byte of a message have been decoded.
assertEquals(0, list.size());
// Write next 3 bytes of a message.
for (int i = 0; i < 3; i++) {
buffer.writeByte(nioBuffer.get());
}
// Reader index of a buffer is not zero and it must be handled correctly by the InboundDecoder.
decoder.decode(ctx, buffer, list);
// Check if reader index has been tracked correctly.
assertEquals(6, buffer.readerIndex());
assertEquals(0, list.size());
buffer.writeBytes(nioBuffer);
decoder.decode(ctx, buffer, list);
assertEquals(1, list.size());
TestMessage actualMessage = (TestMessage) list.get(0);
assertEquals(msg, actualMessage);
}
use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.
the class ItScaleCubeNetworkMessagingTest method testSendMessageToSelf.
/**
* Sends a message from a node to itself and verifies that it gets delivered successfully.
*
* @throws Exception in case of errors.
*/
@Test
public void testSendMessageToSelf(TestInfo testInfo) throws Exception {
testCluster = new Cluster(1, testInfo);
testCluster.startAwait();
ClusterService member = testCluster.members.get(0);
ClusterNode self = member.topologyService().localMember();
class Data {
private final TestMessage message;
private final NetworkAddress sender;
private final Long correlationId;
private Data(TestMessage message, NetworkAddress sender, Long correlationId) {
this.message = message;
this.sender = sender;
this.correlationId = correlationId;
}
}
var dataFuture = new CompletableFuture<Data>();
member.messagingService().addMessageHandler(TestMessageTypes.class, (message, senderAddr, correlationId) -> dataFuture.complete(new Data((TestMessage) message, senderAddr, correlationId)));
var requestMessage = messageFactory.testMessage().msg("request").build();
member.messagingService().send(self, requestMessage);
Data actualData = dataFuture.get(3, TimeUnit.SECONDS);
assertThat(actualData.message.msg(), is(requestMessage.msg()));
assertThat(actualData.sender.consistentId(), is(self.name()));
assertNull(actualData.correlationId);
}
use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.
the class ItConnectionManagerTest method testCanReconnectAfterFail.
/**
* Tests that after a channel was closed, a new channel is opened upon a request.
*
* @throws Exception If failed.
*/
@Test
public void testCanReconnectAfterFail() throws Exception {
String msgText = "test";
int port1 = 4000;
int port2 = 4001;
ConnectionManager manager1 = startManager(port1).get1();
IgniteBiTuple<ConnectionManager, NettyBootstrapFactory> manager2 = startManager(port2);
NettySender sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
TestMessage testMessage = messageFactory.testMessage().msg(msgText).build();
manager2.get1().stop();
manager2.get2().stop();
final NettySender finalSender = sender;
assertThrows(ClosedChannelException.class, () -> {
try {
finalSender.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
} catch (Exception e) {
throw e.getCause();
}
});
manager2 = startManager(port2);
var fut = new CompletableFuture<NetworkMessage>();
manager2.get1().addListener((obj) -> fut.complete(obj.message()));
sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
sender.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
NetworkMessage receivedMessage = fut.get(3, TimeUnit.SECONDS);
assertEquals(msgText, ((TestMessage) receivedMessage).msg());
}
use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.
the class ItConnectionManagerTest method testReuseIncomingConnection.
/**
* Tests that incoming connection is reused for sending messages.
*
* @throws Exception If failed.
*/
@Test
public void testReuseIncomingConnection() throws Exception {
final String msgText = "test";
TestMessage testMessage = messageFactory.testMessage().msg("test").build();
int port1 = 4000;
int port2 = 4001;
ConnectionManager manager1 = startManager(port1).get1();
ConnectionManager manager2 = startManager(port2).get1();
var fut = new CompletableFuture<NetworkMessage>();
manager1.addListener((obj) -> fut.complete(obj.message()));
NettySender senderFrom1to2 = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
// Ensure a handshake has finished on both sides by sending a message.
// TODO: IGNITE-14085 When the recovery protocol is implemented replace this with simple
// CompletableFuture#get called on the send future.
var messageReceivedOn2 = new CompletableFuture<Void>();
// If the message is received, that means that the handshake was successfully performed.
manager2.addListener((message) -> messageReceivedOn2.complete(null));
senderFrom1to2.send(new OutNetworkObject(testMessage, Collections.emptyList()));
messageReceivedOn2.get(3, TimeUnit.SECONDS);
NettySender senderFrom2to1 = manager2.channel(manager1.consistentId(), new InetSocketAddress(port1)).get(3, TimeUnit.SECONDS);
InetSocketAddress clientLocalAddress = (InetSocketAddress) senderFrom1to2.channel().localAddress();
InetSocketAddress clientRemoteAddress = (InetSocketAddress) senderFrom2to1.channel().remoteAddress();
assertEquals(clientLocalAddress, clientRemoteAddress);
senderFrom2to1.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
NetworkMessage receivedMessage = fut.get(3, TimeUnit.SECONDS);
assertEquals(msgText, ((TestMessage) receivedMessage).msg());
}
use of org.apache.ignite.network.TestMessage in project ignite-3 by apache.
the class ItConnectionManagerTest method testSentSuccessfully.
/**
* Tests that a message is sent successfully using the ConnectionManager.
*
* @throws Exception If failed.
*/
@Test
public void testSentSuccessfully() throws Exception {
String msgText = "test";
int port1 = 4000;
int port2 = 4001;
ConnectionManager manager1 = startManager(port1).get1();
ConnectionManager manager2 = startManager(port2).get1();
var fut = new CompletableFuture<NetworkMessage>();
manager2.addListener((obj) -> fut.complete(obj.message()));
NettySender sender = manager1.channel(null, new InetSocketAddress(port2)).get(3, TimeUnit.SECONDS);
TestMessage testMessage = messageFactory.testMessage().msg(msgText).build();
sender.send(new OutNetworkObject(testMessage, Collections.emptyList())).get(3, TimeUnit.SECONDS);
NetworkMessage receivedMessage = fut.get(3, TimeUnit.SECONDS);
assertEquals(msgText, ((TestMessage) receivedMessage).msg());
}
Aggregations