-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultMessagingServiceTest.java
187 lines (151 loc) · 6.35 KB
/
DefaultMessagingServiceTest.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package org.kryonite.kryomessaging.service;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.kryonite.kryomessaging.service.DefaultMessagingService.DEFAULT_RETRY_COUNT;
import com.rabbitmq.client.BuiltinExchangeType;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kryonite.kryomessaging.service.message.Message;
import org.kryonite.kryomessaging.service.message.MessageCallback;
import org.kryonite.kryomessaging.service.mock.MockActiveMqConnectionFactory;
import org.kryonite.kryomessaging.service.model.Animal;
import org.kryonite.kryomessaging.service.model.Person;
import org.mockito.junit.jupiter.MockitoExtension;
@Slf4j
@ExtendWith(MockitoExtension.class)
class DefaultMessagingServiceTest {
private DefaultMessagingService testee;
@BeforeEach
void setUp() throws IOException, TimeoutException {
testee = new DefaultMessagingService(new MockActiveMqConnectionFactory());
}
@Test
void shouldSendAndReceiveMessage() throws IOException {
// Arrange
String queue = "queue";
String exchange = "exchange";
Person person = new Person("Tom", UUID.randomUUID(), 21, 12345.67);
testee.setupExchange(exchange, BuiltinExchangeType.DIRECT);
testee.bindQueueToExchange(queue, exchange);
List<Message<Person>> receivedMessages = new ArrayList<>();
testee.startConsuming(queue, receivedMessages::add, Person.class);
// Act
testee.sendMessage(Message.create(exchange, person, ""));
await()
.atMost(1, TimeUnit.SECONDS)
.until(() -> receivedMessages.size() >= 1);
// Assert
assertEquals(1, receivedMessages.size());
assertEquals(person, receivedMessages.get(0).getBody());
}
@Test
void shouldSendAndReceiveMessageWithGivenRoutingKey() throws IOException {
// Arrange
String queue1 = "queue1";
String queue2 = "queue2";
String exchange = "exchange";
String routingKey1 = "person";
String routingKey2 = "animal";
Person person = new Person("Tom", UUID.randomUUID(), 21, 12345.67);
Animal animal = new Animal(12);
testee.setupExchange(exchange, BuiltinExchangeType.DIRECT);
testee.bindQueueToExchange(queue1, exchange, routingKey1);
testee.bindQueueToExchange(queue2, exchange, routingKey2);
List<Message<Person>> receivedMessages = new ArrayList<>();
testee.startConsuming(queue1, receivedMessages::add, Person.class);
testee.startConsuming(queue2, message -> log.info("Received animal: " + message.getBody()), Animal.class);
// Act
testee.sendMessage(Message.create(exchange, person, routingKey1));
testee.sendMessage(Message.create(exchange, animal, routingKey2));
await()
.atMost(1, TimeUnit.SECONDS)
.until(() -> receivedMessages.size() >= 1);
// Assert
assertEquals(1, receivedMessages.size());
assertEquals(person, receivedMessages.get(0).getBody());
}
@Test
void shouldNotThrowError_WhenQueueDoesSendAnotherBody() throws IOException {
// Arrange
String queue1 = "queue1";
String queue2 = "queue2";
String exchange = "exchange";
String routingKey1 = "person";
String routingKey2 = "animal";
Person person = new Person("Tom", UUID.randomUUID(), 21, 12345.67);
Animal animal = new Animal(12);
testee.setupExchange(exchange, BuiltinExchangeType.DIRECT);
testee.bindQueueToExchange(queue1, exchange, routingKey1);
testee.bindQueueToExchange(queue2, exchange, routingKey2);
List<Message<Person>> receivedMessages = new ArrayList<>();
testee.startConsuming(queue1, receivedMessages::add, Person.class);
// Act
testee.sendMessage(Message.create(exchange, person, routingKey1));
testee.sendMessage(Message.create(exchange, animal, routingKey1));
await()
.atMost(1, TimeUnit.SECONDS)
.until(() -> receivedMessages.size() >= 2);
// Assert
assertEquals(2, receivedMessages.size());
}
@Test
void shouldResendMessage_WhenExceptionIsThrownOnTheConsumer() throws IOException {
// Arrange
String queue = "queue";
String exchange = "exchange";
Person person = new Person("Tom", UUID.randomUUID(), 21, 12345.67);
testee.setupExchange(exchange, BuiltinExchangeType.DIRECT);
testee.bindQueueToExchange(queue, exchange);
List<Message<Person>> failedMessages = new ArrayList<>();
List<Message<Person>> receivedMessages = new ArrayList<>();
testee.startConsuming(queue, new MessageCallback<>() {
private int counter = 0;
@Override
public void messageReceived(Message<Person> message) {
counter++;
if (counter <= 1) {
failedMessages.add(message);
throw new IllegalArgumentException("Failed to consume message!");
}
receivedMessages.add(message);
}
}, Person.class);
// Act
testee.sendMessage(Message.create(exchange, person));
await()
.atMost(1, TimeUnit.SECONDS)
.until(() -> receivedMessages.size() >= 1 && failedMessages.size() >= 1);
// Assert
assertEquals(1, receivedMessages.size());
assertEquals(1, failedMessages.size());
}
@Test
void shouldDeleteMessageAfterRetries() throws IOException {
// Arrange
String queue = "queue";
String exchange = "exchange";
Person person = new Person("Tom", UUID.randomUUID(), 21, 12345.67);
testee.setupExchange(exchange, BuiltinExchangeType.DIRECT);
testee.bindQueueToExchange(queue, exchange);
List<Message<Person>> failedMessages = new ArrayList<>();
testee.startConsuming(queue, message -> {
failedMessages.add(message);
throw new IllegalArgumentException("Failed to consume message!");
}, Person.class);
// Act
testee.sendMessage(Message.create(exchange, person));
await()
.atMost(1, TimeUnit.SECONDS)
.until(() -> failedMessages.size() >= DEFAULT_RETRY_COUNT);
// Assert
assertEquals(DEFAULT_RETRY_COUNT, failedMessages.size());
}
}