-
Notifications
You must be signed in to change notification settings - Fork 537
/
MessageReaderWriterTest.java
128 lines (107 loc) · 4.31 KB
/
MessageReaderWriterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2016-2020 chronicle.software
*
* https://chronicle.software
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.chronicle.queue;
import net.openhft.chronicle.bytes.MethodReader;
import net.openhft.chronicle.core.annotation.RequiredForClient;
import net.openhft.chronicle.core.pool.ClassAliasPool;
import net.openhft.chronicle.core.util.ObjectUtils;
import net.openhft.chronicle.queue.impl.single.SingleChronicleQueueBuilder;
import net.openhft.chronicle.wire.SelfDescribingMarshallable;
import org.jetbrains.annotations.NotNull;
import org.junit.Test;
import java.io.File;
import java.util.Arrays;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@RequiredForClient
public class MessageReaderWriterTest extends QueueTestCommon {
@Test
public void testWriteWhileReading() {
ClassAliasPool.CLASS_ALIASES.addAlias(Message1.class);
ClassAliasPool.CLASS_ALIASES.addAlias(Message2.class);
File path1 = getTmpDir();
File path2 = getTmpDir();
try (ChronicleQueue queue1 = SingleChronicleQueueBuilder
.binary(path1)
.testBlockSize()
.build();
ChronicleQueue queue2 = SingleChronicleQueueBuilder
.binary(path2)
.testBlockSize()
.build()) {
MethodReader reader2 = queue1.createTailer().methodReader(printAll(MessageListener.class));
MessageListener writer2 = queue2.methodWriter(MessageListener.class);
MessageListener processor = new MessageProcessor(writer2);
MethodReader reader1 = queue1.createTailer().methodReader(processor);
MessageListener writer1 = queue1.methodWriter(MessageListener.class);
for (int i = 0; i < 3; i++) {
// write a message
writer1.method1(new Message1("hello"));
writer1.method2(new Message2(234));
// read those messages
assertTrue(reader1.readOne());
assertTrue(reader1.readOne());
assertFalse(reader1.readOne());
// read the produced messages
assertTrue(reader2.readOne());
assertTrue(reader2.readOne());
assertFalse(reader2.readOne());
}
// System.out.println(queue1.dump());
}
}
private static <T> T printAll(@NotNull Class<T> tClass, Class<?>... additional) throws IllegalArgumentException {
return ObjectUtils.onMethodCall((method, args) -> {
@NotNull String argsStr = args == null ? "()" : Arrays.toString(args);
System.out.println(method.getName() + " " + argsStr);
return ObjectUtils.defaultValue(method.getReturnType());
}, tClass, additional);
}
interface MessageListener {
void method1(Message1 message);
void method2(Message2 message);
}
static class Message1 extends SelfDescribingMarshallable {
String text;
public Message1(String text) {
this.text = text;
}
}
static class Message2 extends SelfDescribingMarshallable {
long number;
public Message2(long number) {
this.number = number;
}
}
static class MessageProcessor implements MessageListener {
private final MessageListener writer2;
public MessageProcessor(MessageListener writer2) {
this.writer2 = writer2;
}
@Override
public void method1(@NotNull Message1 message) {
message.text += "-processed";
writer2.method1(message);
}
@Override
public void method2(@NotNull Message2 message) {
message.number += 1000;
writer2.method2(message);
}
}
}