Add rabbit binder test support
This commit is contained in:
1
pom.xml
1
pom.xml
@@ -27,6 +27,7 @@
|
||||
<modules>
|
||||
<module>spring-cloud-stream-binder-rabbit</module>
|
||||
<module>spring-cloud-starter-stream-rabbit</module>
|
||||
<module>spring-cloud-stream-binder-rabbit-test-support</module>
|
||||
</modules>
|
||||
<profiles>
|
||||
<profile>
|
||||
|
||||
32
spring-cloud-stream-binder-rabbit-test-support/pom.xml
Normal file
32
spring-cloud-stream-binder-rabbit-test-support/pom.xml
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-binder-rabbit-parent</artifactId>
|
||||
<version>1.1.0.BUILD-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>spring-cloud-stream-binder-rabbit-test-support</artifactId>
|
||||
<description>Rabbit related test classes</description>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-test-support-internal</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,166 @@
|
||||
/*
|
||||
* Copyright 2015 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
|
||||
*
|
||||
* http://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.cloud.stream.binder.test.junit.rabbit;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import javax.net.ServerSocketFactory;
|
||||
import javax.net.SocketFactory;
|
||||
|
||||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
|
||||
import org.springframework.cloud.stream.test.junit.AbstractExternalResourceTestSupport;
|
||||
|
||||
/**
|
||||
* JUnit {@link org.junit.Rule} that detects the fact that RabbitMQ is available on localhost.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Gary Russell
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class RabbitTestSupport extends AbstractExternalResourceTestSupport<CachingConnectionFactory> {
|
||||
|
||||
private final boolean management;
|
||||
|
||||
public RabbitTestSupport() {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public RabbitTestSupport(boolean management) {
|
||||
super("RABBIT");
|
||||
this.management = management;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void obtainResource() throws Exception {
|
||||
resource = new CachingConnectionFactory("localhost");
|
||||
resource.createConnection().close();
|
||||
if (management) {
|
||||
Socket socket = SocketFactory.getDefault().createSocket("localhost", 15672);
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cleanupResource() throws Exception {
|
||||
resource.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test class to allow testing deferred entity declarations when RabbitMQ is down.
|
||||
*
|
||||
*/
|
||||
public static class RabbitProxy {
|
||||
|
||||
private final int port;
|
||||
|
||||
private final ExecutorService serverExec = Executors.newSingleThreadExecutor();
|
||||
|
||||
private final ExecutorService socketExec = Executors.newCachedThreadPool();
|
||||
|
||||
private volatile ServerSocket serverSocket;
|
||||
|
||||
public RabbitProxy() throws IOException {
|
||||
ServerSocket serverSocket = ServerSocketFactory.getDefault().createServerSocket(0);
|
||||
this.port = serverSocket.getLocalPort();
|
||||
serverSocket.close();
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return this.port;
|
||||
}
|
||||
|
||||
public void start() throws IOException {
|
||||
this.serverSocket = ServerSocketFactory.getDefault().createServerSocket(this.port);
|
||||
this.serverExec.execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (true) {
|
||||
final Socket socket = serverSocket.accept();
|
||||
socketExec.execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
final Socket rabbitSocket = SocketFactory.getDefault().createSocket("localhost",
|
||||
5672);
|
||||
socketExec.execute(new Runnable() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
InputStream is = rabbitSocket.getInputStream();
|
||||
OutputStream os = socket.getOutputStream();
|
||||
int c;
|
||||
while ((c = is.read()) >= 0) {
|
||||
os.write(c);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
try {
|
||||
socket.close();
|
||||
rabbitSocket.close();
|
||||
}
|
||||
catch (IOException e1) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
InputStream is = socket.getInputStream();
|
||||
OutputStream os = rabbitSocket.getOutputStream();
|
||||
int c;
|
||||
while ((c = is.read()) >= 0) {
|
||||
os.write(c);
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
try {
|
||||
socket.close();
|
||||
}
|
||||
catch (IOException e1) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (IOException e) {
|
||||
try {
|
||||
serverSocket.close();
|
||||
}
|
||||
catch (IOException e1) {
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void stop() throws IOException {
|
||||
this.serverSocket.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -47,6 +47,12 @@
|
||||
<artifactId>spring-cloud-stream-test-support-internal</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-stream-binder-rabbit-test-support</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-amqp</artifactId>
|
||||
|
||||
@@ -49,7 +49,7 @@ import org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy;
|
||||
import org.springframework.cloud.stream.binder.PartitionSelectorStrategy;
|
||||
import org.springframework.cloud.stream.binder.PartitionTestSupport;
|
||||
import org.springframework.cloud.stream.binder.Spy;
|
||||
import org.springframework.cloud.stream.test.junit.rabbit.RabbitTestSupport;
|
||||
import org.springframework.cloud.stream.binder.test.junit.rabbit.RabbitTestSupport;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.expression.spel.standard.SpelExpression;
|
||||
import org.springframework.integration.channel.DirectChannel;
|
||||
|
||||
Reference in New Issue
Block a user