【发布时间】:2019-10-12 14:50:52
【问题描述】:
我设置了 AWS 基础设施,因此对 dynamo db 条目的每次更新都会在启用重复数据删除的 SQS FIFO 队列中结束。我也有一个测试覆盖这种情况,我清除队列(队列可以从套装中的其他测试获取更新。为了避免在接收正确消息之前轮询大量消息,我在运行测试之前清除队列) 并更新 Dynamo Db 并在轮询队列时检查是否收到了这些条目。这个测试很不稳定,有时它会失败,因为我发送的所有更新都没有从队列中接收到。
队列只有一个消费者,即我编写的测试。所以不会有另一个消费者在消费这些消息。
我通过 AWS 控制台检查了队列,在测试结束时它是空的,并且由于设置了 TIMEOUT 值而导致测试超时时不包含丢失的消息。
我在 CDK 中的队列配置
public Queue createSqsQueue() {
return new Queue(this, "DynamoDbUpdateSqsQueue", QueueProps.builder()
.withContentBasedDeduplication(true)
.withFifo(true)
.withQueueName("DynamoDbUpdateSqsQueue.fifo")
.withReceiveMessageWaitTime(Duration.seconds(20))
.build());
}
我的接收信息代码
private void assertExpectedDynamoDbUpdatesAreReceived() {
List<String> expectedDynamoDbUpdates = getExpectedDynamoDbUpdates();
List<String> actualDynamoDBUpdates = newArrayList();
boolean allDynamoDbUpdatesReceived = false;
stopWatch.start();
while (!allDynamoDbUpdatesReceived && stopWatch.getTime() < TIMEOUT ) {
List<String> receivedDynamoDbUpdates =
AmazonSQSClientBuilder.standard().receiveMessage(queueUrl).getMessages().stream()
.map(this::processAndDelete)
.collect(Collectors.toList());
actualDynamoDBUpdates.addAll(receivedDynamoDbUpdates);
if(actualDynamoDBUpdates.containsAll(expectedDynamoDbUpdates)){
allDynamoDbUpdatesReceived= true;
}
}
stopWatch.stop();
assert(allDynamoDbUpdatesReceived).isTrue();
}
【问题讨论】:
标签: amazon-web-services amazon-sqs