-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
InterceptedSharedConnectionTests.java
141 lines (120 loc) · 5.13 KB
/
InterceptedSharedConnectionTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright 2002-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.integration.ip.tcp;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.ConsumerEndpointFactoryBean;
import org.springframework.integration.ip.tcp.connection.AbstractClientConnectionFactory;
import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
import org.springframework.integration.ip.tcp.connection.HelloWorldInterceptor;
import org.springframework.integration.ip.tcp.connection.TcpConnectionOpenEvent;
import org.springframework.integration.ip.tcp.connection.TcpConnectionSupport;
import org.springframework.integration.ip.util.TestingUtilities;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Gary Russell
* @since 2.0
*/
@SpringJUnitConfig
@DirtiesContext
public class InterceptedSharedConnectionTests {
@Autowired
AbstractApplicationContext ctx;
@Autowired
AbstractServerConnectionFactory server;
@Autowired
AbstractClientConnectionFactory client;
@Autowired
AbstractServerConnectionFactory netServer;
@Autowired
AbstractClientConnectionFactory netClient;
@Autowired
Listener listener;
/**
* Tests a loopback. The client-side outbound adapter sends a message over
* a connection from the client connection factory; the server side
* receives the message, puts in on a channel which is the input channel
* for the outbound adapter that's sharing the connections. The response
* comes back to an inbound adapter that is sharing the client's
* connection and we verify we get the echo back as expected.
*
* @throws Exception
*/
@Test
void test1() throws Exception {
TestingUtilities.waitListening(this.server, null);
this.client.setPort(this.server.getPort());
this.ctx.getBeansOfType(ConsumerEndpointFactoryBean.class).values().forEach(c -> c.start());
for (int i = 0; i < 5; i++) {
MessageChannel input = ctx.getBean("input", MessageChannel.class);
input.send(MessageBuilder.withPayload("Test").build());
QueueChannel replies = ctx.getBean("replies", QueueChannel.class);
Message<?> message = replies.receive(10000);
assertThat(message).isNotNull();
assertThat(message.getPayload()).isEqualTo("Test");
}
assertThat(this.listener.clientOpenEvent).isNotNull();
assertThat(this.listener.clientOpenEvent.getConnectionFactoryName()).isEqualTo("client");
assertThat(this.listener.serverOpenEvent).isNotNull();
assertThat(this.listener.serverOpenEvent.getConnectionFactoryName()).isEqualTo("server");
}
@Test
void correctOpenNetEventPublished() throws InterruptedException {
TestingUtilities.waitListening(this.netServer, null);
this.listener.clientOpenEvent = null;
this.listener.serverOpenEvent = null;
this.netClient.setPort(this.netServer.getPort());
this.netClient.start();
TcpConnectionSupport conn = this.netClient.getConnection();
conn.send(new GenericMessage<>("foo"));
conn.close();
assertThat(this.listener.latch.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(this.listener.clientOpenEvent).isNotNull();
assertThat(this.listener.clientOpenEvent.getConnectionFactoryName()).isEqualTo("netClient");
assertThat(this.listener.serverOpenEvent).isNotNull();
assertThat(this.listener.serverOpenEvent.getConnectionFactoryName()).isEqualTo("netServer");
}
public static class Listener implements ApplicationListener<TcpConnectionOpenEvent> {
final CountDownLatch latch = new CountDownLatch(2);
volatile TcpConnectionOpenEvent clientOpenEvent;
volatile TcpConnectionOpenEvent serverOpenEvent;
@Override
public void onApplicationEvent(TcpConnectionOpenEvent event) {
if (event.getSource() instanceof HelloWorldInterceptor) {
if (event.getConnectionFactoryName().startsWith("net")) {
this.latch.countDown();
}
if (event.getConnectionFactoryName().contains("lient")) {
this.clientOpenEvent = event;
}
else {
this.serverOpenEvent = event;
}
}
}
}
}