-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows test suite to run against a RabbitMQ container
(cherry picked from commit 425f4d6)
- Loading branch information
1 parent
9e5349f
commit afb1145
Showing
3 changed files
with
60 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
|
||
package reactor.rabbitmq; | ||
|
||
import com.rabbitmq.client.impl.NetworkConnection; | ||
import com.rabbitmq.client.Connection; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
|
@@ -87,23 +87,34 @@ public static Process rabbitmqctl(String command) throws IOException { | |
} | ||
|
||
public static String rabbitmqctlCommand() { | ||
return System.getProperty("rabbitmqctl.bin"); | ||
String rabbitmqCtl = System.getProperty("rabbitmqctl.bin", System.getenv("RABBITMQCTL_BIN")); | ||
if (rabbitmqCtl == null) { | ||
throw new IllegalStateException("Please define the rabbitmqctl.bin system property or " | ||
+ "the RABBITMQCTL_BIN environment variable"); | ||
} | ||
if (rabbitmqCtl.startsWith("DOCKER:")) { | ||
String containerId = rabbitmqCtl.split(":")[1]; | ||
return "docker exec " + containerId + " rabbitmqctl"; | ||
} else { | ||
return rabbitmqCtl; | ||
} | ||
} | ||
|
||
public static void closeConnection(String pid) throws IOException { | ||
private static void closeConnection(String pid) throws IOException { | ||
rabbitmqctl("close_connection '" + pid + "' 'Closed via rabbitmqctl'"); | ||
} | ||
|
||
public static void closeConnection(NetworkConnection c) throws IOException { | ||
public static void closeConnection(Connection c) throws IOException { | ||
Host.ConnectionInfo ci = findConnectionInfoFor(Host.listConnections(), c); | ||
closeConnection(ci.getPid()); | ||
} | ||
|
||
public static List<ConnectionInfo> listConnections() throws IOException { | ||
String output = capture(rabbitmqctl("list_connections -q pid peer_port").getInputStream()); | ||
String output = capture(rabbitmqctl("list_connections -q pid peer_port client_properties") | ||
.getInputStream()); | ||
// output (header line presence depends on broker version): | ||
// pid peer_port | ||
// <[email protected]> 58713 | ||
// <[email protected]> 58713 [{"product","RabbitMQ"},{"... | ||
String[] allLines = output.split("\n"); | ||
|
||
ArrayList<ConnectionInfo> result = new ArrayList<ConnectionInfo>(); | ||
|
@@ -112,18 +123,31 @@ public static List<ConnectionInfo> listConnections() throws IOException { | |
String[] columns = line.split("\t"); | ||
// can be also header line, so ignoring NumberFormatException | ||
try { | ||
result.add(new ConnectionInfo(columns[0], Integer.valueOf(columns[1]))); | ||
Integer.valueOf(columns[1]); // just to ignore header line | ||
result.add(new ConnectionInfo(columns[0], connectionName(columns[2]))); | ||
} catch (NumberFormatException e) { | ||
// OK | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
private static Host.ConnectionInfo findConnectionInfoFor(List<ConnectionInfo> xs, NetworkConnection c) { | ||
private static String connectionName(String clientProperties) { | ||
String beginning = "\"connection_name\",\""; | ||
int begin = clientProperties.indexOf(beginning); | ||
if (begin > 0) { | ||
int start = clientProperties.indexOf(beginning) + beginning.length(); | ||
int end = clientProperties.indexOf("\"", start); | ||
return clientProperties.substring(start, end); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
private static Host.ConnectionInfo findConnectionInfoFor(List<ConnectionInfo> xs, Connection c) { | ||
Host.ConnectionInfo result = null; | ||
for (Host.ConnectionInfo ci : xs) { | ||
if (c.getLocalPort() == ci.getPeerPort()) { | ||
if (c.getClientProvidedName().equals(ci.getName())) { | ||
result = ci; | ||
break; | ||
} | ||
|
@@ -134,19 +158,19 @@ private static Host.ConnectionInfo findConnectionInfoFor(List<ConnectionInfo> xs | |
public static class ConnectionInfo { | ||
|
||
private final String pid; | ||
private final int peerPort; | ||
private final String name; | ||
|
||
public ConnectionInfo(String pid, int peerPort) { | ||
public ConnectionInfo(String pid, String name) { | ||
this.pid = pid; | ||
this.peerPort = peerPort; | ||
this.name = name; | ||
} | ||
|
||
public String getPid() { | ||
return pid; | ||
} | ||
|
||
public int getPeerPort() { | ||
return peerPort; | ||
public String getName() { | ||
return name; | ||
} | ||
} | ||
} |