Skip to content

Commit

Permalink
Fixed AmazonSQSExtension and SQS related test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
reta committed Jan 4, 2024
1 parent c3b61f0 commit 243f579
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
23 changes: 11 additions & 12 deletions aws-junit/src/main/java/zipkin2/junit/aws/AmazonSQSExtension.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 The OpenZipkin Authors
* Copyright 2016-2024 The OpenZipkin Authors
*
* 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
Expand Down Expand Up @@ -30,8 +30,7 @@
import org.elasticmq.StrictSQSLimits$;
import org.elasticmq.rest.sqs.SQSRestServer;
import org.elasticmq.rest.sqs.SQSRestServerBuilder;
import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import zipkin2.Span;
Expand All @@ -40,7 +39,7 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonList;

public class AmazonSQSExtension implements BeforeEachCallback, BeforeAllCallback, AfterAllCallback {
public class AmazonSQSExtension implements BeforeEachCallback, AfterEachCallback {
SQSRestServer server;
int serverPort;
AmazonSQS client;
Expand All @@ -49,7 +48,7 @@ public class AmazonSQSExtension implements BeforeEachCallback, BeforeAllCallback
public AmazonSQSExtension() {
}

@Override public void beforeAll(ExtensionContext extensionContext) {
@Override public void beforeEach(ExtensionContext extensionContext) {
if (server == null) {
server =
SQSRestServerBuilder.withDynamicPort().withSQSLimits(StrictSQSLimits$.MODULE$).start();
Expand All @@ -64,28 +63,28 @@ public AmazonSQSExtension() {
.build();
queueUrl = client.createQueue("zipkin").getQueueUrl();
}

if (client != null && queueUrl != null) {
client.purgeQueue(new PurgeQueueRequest(queueUrl));
}
}

@Override public void afterAll(ExtensionContext extensionContext) {
@Override public void afterEach(ExtensionContext extensionContext) {
if (client != null) {
client.shutdown();
client = null;
}

if (server == null) {
server.stopAndWait();
server = null;
}
}

public String queueUrl() {
return queueUrl;
}

@Override public void beforeEach(ExtensionContext extensionContext) {
if (client != null && queueUrl != null) {
client.purgeQueue(new PurgeQueueRequest(queueUrl));
}
}

public int queueCount() {
String count = client.getQueueAttributes(queueUrl, singletonList("ApproximateNumberOfMessages"))
.getAttributes()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 The OpenZipkin Authors
* Copyright 2016-2024 The OpenZipkin Authors
*
* 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
Expand All @@ -16,6 +16,7 @@
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
Expand All @@ -24,7 +25,6 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.RegisterExtension;
import zipkin2.Span;
import zipkin2.TestObjects;
Expand All @@ -40,9 +40,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class ITSQSCollector {
@RegisterExtension static AmazonSQSExtension sqs = new AmazonSQSExtension();
@RegisterExtension AmazonSQSExtension sqs = new AmazonSQSExtension();

List<Span> spans =
asList( // No unicode or data that doesn't translate between json formats
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 The OpenZipkin Authors
* Copyright 2016-2024 The OpenZipkin Authors
*
* 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
Expand All @@ -18,6 +18,8 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
Expand All @@ -38,20 +40,25 @@
import static zipkin2.TestObjects.CLIENT_SPAN;

class SQSAsyncSenderTest {
@RegisterExtension static AmazonSQSExtension sqs = new AmazonSQSExtension();
@RegisterExtension AmazonSQSExtension sqs = new AmazonSQSExtension();

private SqsClient sqsClient;
private SQSSender sender;

SqsClient sqsClient = SqsClient.builder()
@BeforeEach public void setup() {
sqsClient = SqsClient.builder()
.httpClient(UrlConnectionHttpClient.create())
.region(Region.US_EAST_1)
.endpointOverride(URI.create(sqs.queueUrl()))
.credentialsProvider(
StaticCredentialsProvider.create(AwsBasicCredentials.create("x", "x")))
.build();

SQSSender sender = SQSSender.newBuilder()
sender = SQSSender.newBuilder()
.queueUrl(sqs.queueUrl())
.sqsClient(sqsClient)
.build();
}

@Test void sendsSpans() throws Exception {
send(CLIENT_SPAN, CLIENT_SPAN).execute();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 The OpenZipkin Authors
* Copyright 2016-2024 The OpenZipkin Authors
*
* 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
Expand All @@ -18,6 +18,8 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
Expand All @@ -38,20 +40,25 @@
import static zipkin2.TestObjects.CLIENT_SPAN;

class SQSSenderTest {
@RegisterExtension static AmazonSQSExtension sqs = new AmazonSQSExtension();
@RegisterExtension AmazonSQSExtension sqs = new AmazonSQSExtension();

private SqsClient sqsClient;
private SQSSender sender;

SqsClient sqsClient = SqsClient.builder()
@BeforeEach public void setup() {
sqsClient = SqsClient.builder()
.httpClient(UrlConnectionHttpClient.create())
.region(Region.US_EAST_1)
.endpointOverride(URI.create(sqs.queueUrl()))
.credentialsProvider(
StaticCredentialsProvider.create(AwsBasicCredentials.create("x", "x")))
.build();

SQSSender sender = SQSSender.newBuilder()
sender = SQSSender.newBuilder()
.queueUrl(sqs.queueUrl())
.sqsClient(sqsClient)
.build();
}

@Test void sendsSpans() throws Exception {
send(CLIENT_SPAN, CLIENT_SPAN).execute();
Expand Down

0 comments on commit 243f579

Please sign in to comment.