Skip to content

Commit

Permalink
GH-1419: Fix Local Node Name in Tests
Browse files Browse the repository at this point in the history
Local node not always `rabbit@localhost`; can be `rabbit@realHostName`.
  • Loading branch information
garyrussell committed Oct 14, 2022
1 parent c2c534d commit fc984e0
Showing 1 changed file with 61 additions and 9 deletions.
Expand Up @@ -19,17 +19,29 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Map;
import java.util.UUID;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.amqp.core.AnonymousQueue;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.junit.BrokerRunningSupport;
import org.springframework.amqp.rabbit.junit.RabbitAvailable;
import org.springframework.amqp.rabbit.junit.RabbitAvailableCondition;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.client.ExchangeFilterFunctions;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.UriUtils;


/**
Expand All @@ -43,23 +55,32 @@ public class LocalizedQueueConnectionFactoryIntegrationTests {

private CachingConnectionFactory defaultConnectionFactory;

private RabbitAdmin defaultAdmin;

@BeforeEach
public void setup() {
this.defaultConnectionFactory = new CachingConnectionFactory("localhost");
String[] addresses = new String[] { "localhost:9999", "localhost:5672" };
String[] adminUris = new String[] { "http://localhost:15672", "http://localhost:15672" };
String[] nodes = new String[] { "foo@bar", "rabbit@localhost" };
this.defaultAdmin = new RabbitAdmin(this.defaultConnectionFactory);
BrokerRunningSupport brokerRunning = RabbitAvailableCondition.getBrokerRunning();
String[] addresses = new String[] { "localhost:9999",
brokerRunning.getHostName() + ":" + brokerRunning.getPort() };
String[] adminUris = new String[] { brokerRunning.getAdminUri(), brokerRunning.getAdminUri() };
String[] nodes = new String[] { "foo@bar", findLocalNode() };
String vhost = "/";
String username = "guest";
String password = "guest";
String username = brokerRunning.getAdminUser();
String password = brokerRunning.getAdminPassword();
this.lqcf = new LocalizedQueueConnectionFactory(defaultConnectionFactory, addresses,
adminUris, nodes, vhost, username, password, false, null);
}

@AfterEach
public void tearDown() {
this.lqcf.destroy();
this.defaultConnectionFactory.destroy();
if (this.lqcf != null) {
this.lqcf.destroy();
}
if (this.defaultConnectionFactory != null) {
this.defaultConnectionFactory.destroy();
}
}

@Test
Expand All @@ -68,6 +89,7 @@ public void testConnect() throws Exception {
Queue queue = new Queue(UUID.randomUUID().toString(), false, false, true);
admin.declareQueue(queue);
ConnectionFactory targetConnectionFactory = this.lqcf.getTargetConnectionFactory("[" + queue.getName() + "]");
assertThat(targetConnectionFactory).isNotSameAs(this.defaultConnectionFactory);
RabbitTemplate template = new RabbitTemplate(targetConnectionFactory);
template.convertAndSend("", queue.getName(), "foo");
assertThat(template.receiveAndConvert(queue.getName())).isEqualTo("foo");
Expand All @@ -77,9 +99,11 @@ public void testConnect() throws Exception {
@Test
void findLocal() {
ConnectionFactory defaultCf = mock(ConnectionFactory.class);
BrokerRunningSupport brokerRunning = RabbitAvailableCondition.getBrokerRunning();
LocalizedQueueConnectionFactory lqcf = new LocalizedQueueConnectionFactory(defaultCf,
Map.of("rabbit@localhost", "localhost:5672"), new String[] { "http://localhost:15672" },
"/", "guest", "guest", false, null);
Map.of(findLocalNode(), brokerRunning.getHostName() + ":" + brokerRunning.getPort()),
new String[] { brokerRunning.getAdminUri() },
"/", brokerRunning.getAdminUser(), brokerRunning.getAdminPassword(), false, null);
ConnectionFactory cf = lqcf.getTargetConnectionFactory("[local]");
RabbitAdmin admin = new RabbitAdmin(cf);
assertThat(admin.getQueueProperties("local")).isNotNull();
Expand All @@ -89,4 +113,32 @@ void findLocal() {
lqcf.destroy();
}

private String findLocalNode() {
AnonymousQueue queue = new AnonymousQueue();
this.defaultAdmin.declareQueue(queue);
URI uri;
BrokerRunningSupport brokerRunning = RabbitAvailableCondition.getBrokerRunning();
try {
uri = new URI(brokerRunning.getAdminUri())
.resolve("/api/queues/" + UriUtils.encodePathSegment("/", StandardCharsets.UTF_8) + "/"
+ queue.getName());
}
catch (URISyntaxException ex) {
throw new IllegalStateException(ex);
}
WebClient client = WebClient.builder()
.filter(ExchangeFilterFunctions.basicAuthentication(brokerRunning.getAdminUser(),
brokerRunning.getAdminPassword()))
.build();
Map<String, Object> queueInfo = client.get()
.uri(uri)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(new ParameterizedTypeReference<Map<String, Object>>() {
})
.block(Duration.ofSeconds(10));
this.defaultAdmin.deleteQueue(queue.getName());
return (String) queueInfo.get("node");
}

}

0 comments on commit fc984e0

Please sign in to comment.