-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqsPatternTests.java
More file actions
278 lines (230 loc) · 10 KB
/
SqsPatternTests.java
File metadata and controls
278 lines (230 loc) · 10 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2023 Luxant Solutions
// 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 com.luxant.sqs;
import org.junit.Assert;
import org.junit.Test;
import software.amazon.awssdk.services.sqs.model.Message;
import static org.junit.Assert.*;
import java.time.Duration;
import java.util.ArrayList;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.logging.Logger;
public class SqsPatternTests {
Logger logger = Logger.getGlobal();
void shutdownAndWait(ExecutorService executorService) {
try {
executorService.shutdown();
executorService.awaitTermination(20, TimeUnit.SECONDS);
executorService = Executors.newFixedThreadPool(110);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
@Test
public void testQueueOneToOne() {
ExecutorService es = Executors.newFixedThreadPool(110);
SqsConsumer sqsc = new SqsConsumer("simple-queue",
10, Duration.ofSeconds(2),
msg -> { /* noop */ });
SqsProducerBench sqsp = new SqsProducerBench("simple-queue", "message", 100, 10, false);
es.execute(sqsc);
es.execute(sqsp);
shutdownAndWait(es);
assertEquals(sqsc.getReceivedCount(), sqsp.getSentCount());
Utils.deleteQueue(sqsp.getClient(), sqsp.getQueueUrl());
}
@Test
public void testQueueCompetingConsumer() {
ExecutorService es = Executors.newFixedThreadPool(20);
/*
* Start filling a queue with 100 messages and then start consumers to
* compete.
*/
var producer = new SqsProducerBench("test-competing-consumer", "message", 10000, 100, true);
es.execute(producer);
/*
* Setup the consumers. Count is 100 to avoid potential redelivery
* and thus false negatives without large timeouts. Aggregate consumer
* received message count should match # sent, but no one consumer should
* receive all messages.
*/
SqsConsumer[] consumers = new SqsConsumer[10];
for (int i = 0; i < consumers.length; i++) {
consumers[i] = new SqsConsumer("test-competing-consumer", 100, Duration.ofSeconds(5),
msg -> { /* noop */ });
es.execute(consumers[i]);
}
shutdownAndWait(es);
/* check for distribution and that all messages were delivered */
int recvCount = 0;
for (var c : consumers) {
recvCount += c.getReceivedCount();
assertNotEquals(c.getReceivedCount(), producer.getSentCount());
}
assertEquals(recvCount, producer.getSentCount());
Utils.deleteQueue(producer.getClient(), producer.getQueueUrl());
}
@Test
public void testQueueAggregate() {
ExecutorService es = Executors.newFixedThreadPool(110);
/*
* Setup one many publishers to aggregage data and one consumer.
*/
SqsProducerBench[] producers = new SqsProducerBench[10];
for (int i = 0; i < producers.length; i++) {
producers[i] = new SqsProducerBench("test-queue-aggregate", "message", 10000, 10, true);
es.execute(producers[i]);
}
var consumer = new SqsConsumer("test-queue-aggregate", 100, Duration.ofSeconds(5),
msg -> { /* noop */});
es.execute(consumer);
shutdownAndWait(es);
/* check for distribution and that all messages were delivered */
int sendCount = 0;
for (var p : producers) {
sendCount += p.getSentCount();
}
assertEquals(sendCount, consumer.getReceivedCount());
Utils.deleteQueue(consumer.getClient(), consumer.getQueueUrl());
}
private class MyService implements Consumer<Message>, Runnable {
static final public String RESPONSE_BODY = "Here's some help.";
SqsResponder responder;
private int delay;
public MyService(String listenQueue, int workDelay) {
responder = new SqsResponder(listenQueue, this);
delay = workDelay;
}
@Override
public void accept(Message m) {
if (delay > 0) {
Utils.sleep(delay);
}
responder.reply(m, RESPONSE_BODY);
}
@Override
public void run() {
responder.run();
}
public int getReceivedCount() {
return responder.getReceivedCount();
}
}
@Test
public void testRequestReplySerial() {
ExecutorService es = Executors.newFixedThreadPool(10);
// launch the simple service
es.execute(new MyService("service-queue", 0));
/* make a request */
try (SqsRequestor requestor = new SqsRequestor("requestor-serial")) {
for (int i = 0; i < 10; i++) {
assertEquals(MyService.RESPONSE_BODY,
requestor.request("service-queue", "help!", Duration.ofSeconds(10)));
}
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown: " + e.getMessage());
}
es.shutdownNow();
Utils.deleteQueue("service-queue");
}
private long primeAndGetRequestRTT(SqsRequestor requestor, String queue)
throws InterruptedException, ExecutionException, TimeoutException {
final int rttTestCount = 5;
// Prime the requestor to create the response queue created.
requestor.request(queue, "rtt", Duration.ofSeconds(30));
long start = System.currentTimeMillis();
for (int i = 0; i < rttTestCount; i++) {
requestor.request(queue, "rtt", Duration.ofSeconds(30));
}
long stop = System.currentTimeMillis();
return (stop - start) / rttTestCount;
}
@Test
public void testRequestReplyScaling() {
ExecutorService es = Executors.newFixedThreadPool(110);
/* launch our simple service */
MyService services[] = new MyService[20];
for (int i = 0; i < 20; i++) {
services[i] = new MyService("service-queue-scale", 100);
es.execute(services[i]);
}
// Serially, this would take RTT * 100 requests, so approx 10 seconds
// plus wire time. Ensure it's less than that.
try (SqsRequestor requestor = new SqsRequestor("requestor-scaling")) {
long rtt = primeAndGetRequestRTT(requestor, "service-queue-scale");
var requests = new ArrayList<CompletableFuture<String>>(100);
var start = System.currentTimeMillis();
for (int i = 0; i < 100; i++) {
requests.add(requestor.request("service-queue-scale", "help!"));
}
for (CompletableFuture<String> cf : requests) {
var response = cf.get();
assertEquals(MyService.RESPONSE_BODY, response);
}
var stop = System.currentTimeMillis();
// a low bar for scaling, but did it go faster than serially?
long expectedDuration = rtt * requests.size();
long actualDuration = stop - start;
Assert.assertTrue(String.format("Test did not scale. Expected duration = %d ms, actual = %d ms.",
expectedDuration, actualDuration), actualDuration < expectedDuration);
// check for some distribution and that all requests were handled by the service
int handled = 0;
for (int i = 0; i < services.length; i++) {
assertNotEquals(services[i].getReceivedCount(), requests.size());
handled += services[i].getReceivedCount();
}
// some additional for the priming request and RTT testing.
assertTrue(handled > requests.size());
Utils.deleteQueue(requestor.getClient(), requestor.getQueueUrl("service-queue-scale"));
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown: " + e.getMessage());
}
}
@Test
public void testRequestReplyMultipleQueues() {
ExecutorService es = Executors.newFixedThreadPool(10);
/* launch our simple service */
MyService service1 = new MyService("service-queue-1", 0);
MyService service2 = new MyService("service-queue-2", 0);
es.execute(service1);
es.execute(service2);
// Serially, this would take RTT * 100 requests, so approx 10 seconds
// plus wire time. Ensure it's less than that.
try (SqsRequestor requestor = new SqsRequestor("multi-queue-requestor")) {
var requests = new ArrayList<CompletableFuture<String>>(100);
for (int i = 0; i < 10; i++) {
requests.add(requestor.request("service-queue-1", "message-" + i));
requests.add(requestor.request("service-queue-2", "message-" + i));
}
for (CompletableFuture<String> cf : requests) {
cf.get();
}
Assert.assertEquals(service1.getReceivedCount(), requests.size() / 2);
Assert.assertEquals(service2.getReceivedCount(), requests.size() / 2);
} catch (Exception e) {
e.printStackTrace();
fail("Exception thrown: " + e.getMessage());
} finally {
Utils.deleteQueue("service-queue-1");
Utils.deleteQueue("service-queue-2");
}
}
}