Skip to content

Commit

Permalink
GH-1419: Fix Early Exit in NodeLocator
Browse files Browse the repository at this point in the history
If a node was returned by the REST call and the node was not in the map
of nodes to addreses, the loop exited early.

The incorrect variable was being tested (never null).
  • Loading branch information
garyrussell committed Oct 13, 2022
1 parent 6e3e246 commit 1713452
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Expand Up @@ -398,7 +398,7 @@ default ConnectionFactory locate(String[] adminUris, Map<String, String> nodeToA
String node = (String) queueInfo.get("node");
if (node != null) {
String nodeUri = nodeToAddress.get(node);
if (uri != null) {
if (nodeUri != null) {
close(client);
return factoryFunction.locate(queue, node, nodeUri);
}
Expand Down
@@ -0,0 +1,69 @@
/*
* Copyright 2022 the original author or 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
*
* https://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 org.springframework.amqp.rabbit.connection;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import java.net.URISyntaxException;
import java.util.Map;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import org.springframework.amqp.rabbit.connection.LocalizedQueueConnectionFactory.NodeLocator;
import org.springframework.lang.Nullable;

/**
* @author Gary Russell
* @since 3.0
*
*/
public class NodeLocatorTests {

@Test
@DisplayName("don't exit early when node to address missing")
void missingNode() throws URISyntaxException {

NodeLocator<Object> nodeLocator = spy(new NodeLocator<Object>() {

@Override
public Object createClient(String userName, String password) {
return null;
}

@Override
@Nullable
public Map<String, Object> restCall(Object client, String baseUri, String vhost, String queue) {
if (baseUri.contains("foo")) {
return Map.of("node", "c@d");
}
else {
return Map.of("node", "a@b");
}
}
});
ConnectionFactory factory = nodeLocator.locate(new String[] { "http://foo", "http://bar" },
Map.of("a@b", "baz"), null, "q", null, null, (q, n, u) -> {
return null;
});
verify(nodeLocator, times(2)).restCall(any(), any(), any(), any());
}

}

0 comments on commit 1713452

Please sign in to comment.