Skip to content

Commit a76af23

Browse files
committed
IGNITE-28057 Refactor communication test messages
1 parent 15f2122 commit a76af23

12 files changed

Lines changed: 191 additions & 205 deletions

modules/core/src/test/config/io-manager-benchmark.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@
3434
<list/>
3535
</property>
3636

37+
<property name="pluginProviders">
38+
<list>
39+
<bean class="org.apache.ignite.loadtests.communication.GridTestMessagePluginProvider"/>
40+
</list>
41+
</property>
42+
3743
<!-- Configure load balancing SPI in the way that do not require extra event subscription. -->
3844
<property name="loadBalancingSpi">
3945
<bean class="org.apache.ignite.spi.loadbalancing.roundrobin.RoundRobinLoadBalancingSpi">

modules/core/src/test/java/org/apache/ignite/internal/managers/communication/GridCommunicationSendMessageSelfTest.java

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.ignite.internal.managers.communication;
1919

20-
import java.nio.ByteBuffer;
2120
import java.util.UUID;
2221
import java.util.concurrent.CountDownLatch;
2322
import org.apache.ignite.configuration.IgniteConfiguration;
@@ -28,6 +27,7 @@
2827
import org.apache.ignite.plugin.extensions.communication.MessageFactory;
2928
import org.apache.ignite.plugin.extensions.communication.MessageFactoryProvider;
3029
import org.apache.ignite.plugin.extensions.communication.MessageReader;
30+
import org.apache.ignite.plugin.extensions.communication.MessageSerializer;
3131
import org.apache.ignite.plugin.extensions.communication.MessageWriter;
3232
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
3333
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
@@ -149,21 +149,6 @@ private void doSend(Message msg, final Class<?> msgCls) throws Exception {
149149

150150
/** */
151151
private static class TestMessage implements Message {
152-
/** {@inheritDoc} */
153-
@Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
154-
writer.setBuffer(buf);
155-
156-
if (!writer.writeHeader(directType()))
157-
return false;
158-
159-
return true;
160-
}
161-
162-
/** {@inheritDoc} */
163-
@Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
164-
return true;
165-
}
166-
167152
/** {@inheritDoc} */
168153
@Override public short directType() {
169154
return DIRECT_TYPE;
@@ -173,23 +158,28 @@ private static class TestMessage implements Message {
173158
/** */
174159
private static class TestOverByteIdMessage implements Message {
175160
/** {@inheritDoc} */
176-
@Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
177-
writer.setBuffer(buf);
178-
179-
if (!writer.writeHeader(directType()))
180-
return false;
181-
182-
return true;
161+
@Override public short directType() {
162+
return DIRECT_TYPE_OVER_BYTE;
183163
}
164+
}
184165

166+
/** Serializer that writes only the message header. */
167+
private static class HeaderOnlyMessageSerializer<M extends Message> implements MessageSerializer<M> {
185168
/** {@inheritDoc} */
186-
@Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
169+
@Override public boolean writeTo(M msg, MessageWriter writer) {
170+
if (!writer.isHeaderWritten()) {
171+
if (!writer.writeHeader(msg.directType()))
172+
return false;
173+
174+
writer.onHeaderWritten();
175+
}
176+
187177
return true;
188178
}
189179

190180
/** {@inheritDoc} */
191-
@Override public short directType() {
192-
return DIRECT_TYPE_OVER_BYTE;
181+
@Override public boolean readFrom(M msg, MessageReader reader) {
182+
return true;
193183
}
194184
}
195185

@@ -204,8 +194,8 @@ public static class TestPluginProvider extends AbstractTestPluginProvider {
204194
@Override public void initExtensions(PluginContext ctx, ExtensionRegistry registry) {
205195
registry.registerExtension(MessageFactoryProvider.class, new MessageFactoryProvider() {
206196
@Override public void registerAll(MessageFactory factory) {
207-
factory.register(DIRECT_TYPE, TestMessage::new);
208-
factory.register(DIRECT_TYPE_OVER_BYTE, TestOverByteIdMessage::new);
197+
factory.register(DIRECT_TYPE, TestMessage::new, new HeaderOnlyMessageSerializer<>());
198+
factory.register(DIRECT_TYPE_OVER_BYTE, TestOverByteIdMessage::new, new HeaderOnlyMessageSerializer<>());
209199
}
210200
});
211201
}

modules/core/src/test/java/org/apache/ignite/internal/managers/communication/GridIoManagerSelfTest.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.apache.ignite.internal.managers.communication;
1919

20-
import java.nio.ByteBuffer;
2120
import java.util.Collection;
2221
import java.util.UUID;
2322
import java.util.concurrent.Callable;
@@ -29,8 +28,6 @@
2928
import org.apache.ignite.internal.managers.discovery.GridDiscoveryManager;
3029
import org.apache.ignite.internal.util.typedef.F;
3130
import org.apache.ignite.plugin.extensions.communication.Message;
32-
import org.apache.ignite.plugin.extensions.communication.MessageReader;
33-
import org.apache.ignite.plugin.extensions.communication.MessageWriter;
3431
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
3532
import org.apache.ignite.testframework.GridTestNode;
3633
import org.apache.ignite.testframework.GridTestUtils;
@@ -227,17 +224,6 @@ private static class IsEqualCollection implements ArgumentMatcher<Collection<? e
227224

228225
/** */
229226
private static class TestMessage implements Message {
230-
231-
/** {@inheritDoc} */
232-
@Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
233-
return true;
234-
}
235-
236-
/** {@inheritDoc} */
237-
@Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
238-
return true;
239-
}
240-
241227
/** {@inheritDoc} */
242228
@Override public short directType() {
243229
return 0;

modules/core/src/test/java/org/apache/ignite/internal/managers/communication/IgniteMessageFactoryImplTest.java

Lines changed: 8 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@
1717

1818
package org.apache.ignite.internal.managers.communication;
1919

20-
import java.nio.ByteBuffer;
21-
2220
import org.apache.ignite.IgniteException;
2321
import org.apache.ignite.plugin.extensions.communication.Message;
2422
import org.apache.ignite.plugin.extensions.communication.MessageFactory;
2523
import org.apache.ignite.plugin.extensions.communication.MessageFactoryProvider;
26-
import org.apache.ignite.plugin.extensions.communication.MessageReader;
27-
import org.apache.ignite.plugin.extensions.communication.MessageWriter;
2824
import org.junit.Test;
2925

3026
import static org.junit.Assert.assertArrayEquals;
@@ -34,15 +30,6 @@
3430
* Tests for default implementation of {@link MessageFactory} interface.
3531
*/
3632
public class IgniteMessageFactoryImplTest {
37-
/** Test message 1 type. */
38-
private static final short TEST_MSG_1_TYPE = 1;
39-
40-
/** Test message 2 type. */
41-
private static final short TEST_MSG_2_TYPE = 2;
42-
43-
/** Test message 42 type. */
44-
private static final short TEST_MSG_42_TYPE = 42;
45-
4633
/** Unknown message type. */
4734
private static final short UNKNOWN_MSG_TYPE = 0;
4835

@@ -69,18 +56,18 @@ public void testCreate() {
6956

7057
Message msg;
7158

72-
msg = msgFactory.create(TEST_MSG_1_TYPE);
59+
msg = msgFactory.create(TestMessage1.DIRECT_TYPE);
7360
assertTrue(msg instanceof TestMessage1);
7461

75-
msg = msgFactory.create(TEST_MSG_2_TYPE);
62+
msg = msgFactory.create(TestMessage2.DIRECT_TYPE);
7663
assertTrue(msg instanceof TestMessage2);
7764

78-
msg = msgFactory.create(TEST_MSG_42_TYPE);
65+
msg = msgFactory.create(TestMessage42.DIRECT_TYPE);
7966
assertTrue(msg instanceof TestMessage42);
8067

8168
short[] directTypes = msgFactory.registeredDirectTypes();
8269

83-
assertArrayEquals(directTypes, new short[] {TEST_MSG_1_TYPE, TEST_MSG_2_TYPE, TEST_MSG_42_TYPE});
70+
assertArrayEquals(directTypes, new short[] {TestMessage1.DIRECT_TYPE, TestMessage2.DIRECT_TYPE, TestMessage42.DIRECT_TYPE});
8471
}
8572

8673
/**
@@ -116,8 +103,8 @@ public void testRegisterTheSameType() {
116103
private static class TestMessageFactoryPovider implements MessageFactoryProvider {
117104
/** {@inheritDoc} */
118105
@Override public void registerAll(MessageFactory factory) {
119-
factory.register(TEST_MSG_1_TYPE, TestMessage1::new);
120-
factory.register(TEST_MSG_42_TYPE, TestMessage42::new);
106+
factory.register(TestMessage1.DIRECT_TYPE, TestMessage1::new, new TestMessage1Serializer());
107+
factory.register(TestMessage42.DIRECT_TYPE, TestMessage42::new, new TestMessage42Serializer());
121108
}
122109
}
123110

@@ -127,7 +114,7 @@ private static class TestMessageFactoryPovider implements MessageFactoryProvider
127114
private static class TestMessageFactoryPoviderWithTheSameDirectType implements MessageFactoryProvider {
128115
/** {@inheritDoc} */
129116
@Override public void registerAll(MessageFactory factory) {
130-
factory.register(TEST_MSG_1_TYPE, TestMessage1::new);
117+
factory.register(TestMessage1.DIRECT_TYPE, TestMessage1::new, new TestMessage1Serializer());
131118
}
132119
}
133120

@@ -137,64 +124,7 @@ private static class TestMessageFactoryPoviderWithTheSameDirectType implements M
137124
private static class TestMessageFactory implements MessageFactoryProvider {
138125
/** {@inheritDoc} */
139126
@Override public void registerAll(MessageFactory factory) {
140-
factory.register(TEST_MSG_2_TYPE, TestMessage2::new);
141-
}
142-
}
143-
144-
/** Test message. */
145-
private static class TestMessage1 implements Message {
146-
/** {@inheritDoc} */
147-
@Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
148-
return false;
149-
}
150-
151-
/** {@inheritDoc} */
152-
@Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
153-
return false;
154-
}
155-
156-
/** {@inheritDoc} */
157-
@Override public short directType() {
158-
return TEST_MSG_1_TYPE;
159-
}
160-
161-
}
162-
163-
/** Test message. */
164-
private static class TestMessage2 implements Message {
165-
/** {@inheritDoc} */
166-
@Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
167-
return false;
127+
factory.register(TestMessage2.DIRECT_TYPE, TestMessage2::new, new TestMessage2Serializer());
168128
}
169-
170-
/** {@inheritDoc} */
171-
@Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
172-
return false;
173-
}
174-
175-
/** {@inheritDoc} */
176-
@Override public short directType() {
177-
return TEST_MSG_2_TYPE;
178-
}
179-
180-
}
181-
182-
/** Test message. */
183-
private static class TestMessage42 implements Message {
184-
/** {@inheritDoc} */
185-
@Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
186-
return false;
187-
}
188-
189-
/** {@inheritDoc} */
190-
@Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
191-
return false;
192-
}
193-
194-
/** {@inheritDoc} */
195-
@Override public short directType() {
196-
return TEST_MSG_42_TYPE;
197-
}
198-
199129
}
200130
}

modules/core/src/test/java/org/apache/ignite/internal/managers/communication/MessageDirectTypeIdConflictTest.java

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@
1717

1818
package org.apache.ignite.internal.managers.communication;
1919

20-
import java.nio.ByteBuffer;
2120
import java.util.concurrent.Callable;
2221
import org.apache.ignite.IgniteCheckedException;
2322
import org.apache.ignite.configuration.IgniteConfiguration;
2423
import org.apache.ignite.plugin.AbstractTestPluginProvider;
2524
import org.apache.ignite.plugin.ExtensionRegistry;
2625
import org.apache.ignite.plugin.PluginContext;
27-
import org.apache.ignite.plugin.extensions.communication.Message;
2826
import org.apache.ignite.plugin.extensions.communication.MessageFactory;
2927
import org.apache.ignite.plugin.extensions.communication.MessageFactoryProvider;
30-
import org.apache.ignite.plugin.extensions.communication.MessageReader;
31-
import org.apache.ignite.plugin.extensions.communication.MessageWriter;
3228
import org.apache.ignite.spi.communication.tcp.TcpCommunicationSpi;
3329
import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
3430
import org.junit.Test;
@@ -88,28 +84,10 @@ public static class TestPluginProvider extends AbstractTestPluginProvider {
8884
@Override public void initExtensions(PluginContext ctx, ExtensionRegistry registry) {
8985
registry.registerExtension(MessageFactoryProvider.class, new MessageFactoryProvider() {
9086
@Override public void registerAll(MessageFactory factory) {
91-
factory.register(MSG_DIRECT_TYPE, TestMessage::new);
87+
factory.register(MSG_DIRECT_TYPE, MessageDirectTypeIdConflictTestMessage::new,
88+
new MessageDirectTypeIdConflictTestMessageSerializer());
9289
}
9390
});
9491
}
9592
}
96-
97-
/** Test message with already registered direct type. */
98-
private static class TestMessage implements Message {
99-
/** {@inheritDoc} */
100-
@Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) {
101-
return false;
102-
}
103-
104-
/** {@inheritDoc} */
105-
@Override public boolean readFrom(ByteBuffer buf, MessageReader reader) {
106-
return false;
107-
}
108-
109-
/** {@inheritDoc} */
110-
@Override public short directType() {
111-
return MSG_DIRECT_TYPE;
112-
}
113-
114-
}
11593
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.internal.managers.communication;
19+
20+
import org.apache.ignite.internal.Order;
21+
import org.apache.ignite.plugin.extensions.communication.Message;
22+
23+
/** Test message for {@link IgniteMessageFactoryImplTest}. */
24+
public class TestMessage1 implements Message {
25+
/** Direct type. */
26+
public static final short DIRECT_TYPE = 1;
27+
28+
/** Marker field to enable code-generated serializer. */
29+
@Order(0)
30+
byte marker;
31+
32+
/** {@inheritDoc} */
33+
@Override public short directType() {
34+
return DIRECT_TYPE;
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.internal.managers.communication;
19+
20+
import org.apache.ignite.internal.Order;
21+
import org.apache.ignite.plugin.extensions.communication.Message;
22+
23+
/** Test message for {@link IgniteMessageFactoryImplTest}. */
24+
public class TestMessage2 implements Message {
25+
/** Direct type. */
26+
public static final short DIRECT_TYPE = 2;
27+
28+
/** Marker field to enable code-generated serializer. */
29+
@Order(0)
30+
byte marker;
31+
32+
/** {@inheritDoc} */
33+
@Override public short directType() {
34+
return DIRECT_TYPE;
35+
}
36+
}

0 commit comments

Comments
 (0)