1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2017 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
34
34
35
35
import javax .net .SocketFactory ;
36
36
37
+ import org .junit .Rule ;
37
38
import org .junit .Test ;
38
39
39
40
import org .springframework .context .ApplicationEventPublisher ;
43
44
import org .springframework .integration .ip .tcp .serializer .ByteArrayStxEtxSerializer ;
44
45
import org .springframework .integration .ip .util .SocketTestUtils ;
45
46
import org .springframework .integration .ip .util .TestingUtilities ;
47
+ import org .springframework .integration .test .support .LongRunningIntegrationTest ;
46
48
import org .springframework .messaging .Message ;
47
49
import org .springframework .messaging .support .ErrorMessage ;
48
50
49
51
/**
50
52
* @author Gary Russell
51
53
* @author Artem Bilan
54
+ *
52
55
* @since 2.0
53
56
*/
54
57
public class TcpNioConnectionReadTests {
55
58
59
+ @ Rule
60
+ public LongRunningIntegrationTest longRunningIntegrationTest = new LongRunningIntegrationTest ();
61
+
56
62
private final CountDownLatch latch = new CountDownLatch (1 );
57
63
58
64
private AbstractServerConnectionFactory getConnectionFactory (
@@ -81,15 +87,10 @@ public void testReadLength() throws Exception {
81
87
ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer ();
82
88
final List <Message <?>> responses = new ArrayList <Message <?>>();
83
89
final Semaphore semaphore = new Semaphore (0 );
84
- AbstractServerConnectionFactory scf = getConnectionFactory (serializer , new TcpListener () {
85
-
86
- @ Override
87
- public boolean onMessage (Message <?> message ) {
88
- responses .add (message );
89
- semaphore .release ();
90
- return false ;
91
- }
92
-
90
+ AbstractServerConnectionFactory scf = getConnectionFactory (serializer , message -> {
91
+ responses .add (message );
92
+ semaphore .release ();
93
+ return false ;
93
94
});
94
95
95
96
// Fire up the sender.
@@ -102,34 +103,28 @@ public boolean onMessage(Message<?> message) {
102
103
assertEquals ("Data" , SocketTestUtils .TEST_STRING + SocketTestUtils .TEST_STRING ,
103
104
new String ((byte []) responses .get (0 ).getPayload ()));
104
105
assertEquals ("Data" , SocketTestUtils .TEST_STRING + SocketTestUtils .TEST_STRING ,
105
- new String ((byte []) responses .get (1 ).getPayload ()));
106
+ new String ((byte []) responses .get (1 ).getPayload ()));
106
107
scf .stop ();
107
108
done .countDown ();
108
109
}
109
110
110
111
111
-
112
112
@ SuppressWarnings ("unchecked" )
113
113
@ Test
114
114
public void testFragmented () throws Exception {
115
115
ByteArrayLengthHeaderSerializer serializer = new ByteArrayLengthHeaderSerializer ();
116
116
final List <Message <?>> responses = new ArrayList <Message <?>>();
117
117
final Semaphore semaphore = new Semaphore (0 );
118
- AbstractServerConnectionFactory scf = getConnectionFactory (serializer , new TcpListener () {
119
-
120
- @ Override
121
- public boolean onMessage (Message <?> message ) {
122
- responses .add (message );
123
- try {
124
- Thread .sleep (1000 );
125
- }
126
- catch (InterruptedException e ) {
127
- Thread .currentThread ().interrupt ();
128
- }
129
- semaphore .release ();
130
- return false ;
118
+ AbstractServerConnectionFactory scf = getConnectionFactory (serializer , message -> {
119
+ responses .add (message );
120
+ try {
121
+ Thread .sleep (10 );
131
122
}
132
-
123
+ catch (InterruptedException e ) {
124
+ Thread .currentThread ().interrupt ();
125
+ }
126
+ semaphore .release ();
127
+ return false ;
133
128
});
134
129
135
130
int howMany = 2 ;
@@ -140,7 +135,7 @@ public boolean onMessage(Message<?> message) {
140
135
assertEquals ("Expected" , howMany , responses .size ());
141
136
for (int i = 0 ; i < howMany ; i ++) {
142
137
assertEquals ("Data" , "xx" ,
143
- new String (((Message <byte []>) responses .get (0 )).getPayload ()));
138
+ new String (((Message <byte []>) responses .get (0 )).getPayload ()));
144
139
}
145
140
scf .stop ();
146
141
done .countDown ();
@@ -152,15 +147,10 @@ public void testReadStxEtx() throws Exception {
152
147
ByteArrayStxEtxSerializer serializer = new ByteArrayStxEtxSerializer ();
153
148
final List <Message <?>> responses = new ArrayList <Message <?>>();
154
149
final Semaphore semaphore = new Semaphore (0 );
155
- AbstractServerConnectionFactory scf = getConnectionFactory (serializer , new TcpListener () {
156
-
157
- @ Override
158
- public boolean onMessage (Message <?> message ) {
159
- responses .add (message );
160
- semaphore .release ();
161
- return false ;
162
- }
163
-
150
+ AbstractServerConnectionFactory scf = getConnectionFactory (serializer , message -> {
151
+ responses .add (message );
152
+ semaphore .release ();
153
+ return false ;
164
154
});
165
155
166
156
// Fire up the sender.
@@ -171,9 +161,9 @@ public boolean onMessage(Message<?> message) {
171
161
assertTrue (semaphore .tryAcquire (1 , 10000 , TimeUnit .MILLISECONDS ));
172
162
assertEquals ("Did not receive data" , 2 , responses .size ());
173
163
assertEquals ("Data" , SocketTestUtils .TEST_STRING + SocketTestUtils .TEST_STRING ,
174
- new String (((Message <byte []>) responses .get (0 )).getPayload ()));
164
+ new String (((Message <byte []>) responses .get (0 )).getPayload ()));
175
165
assertEquals ("Data" , SocketTestUtils .TEST_STRING + SocketTestUtils .TEST_STRING ,
176
- new String (((Message <byte []>) responses .get (1 )).getPayload ()));
166
+ new String (((Message <byte []>) responses .get (1 )).getPayload ()));
177
167
scf .stop ();
178
168
done .countDown ();
179
169
}
@@ -184,15 +174,10 @@ public void testReadCrLf() throws Exception {
184
174
ByteArrayCrLfSerializer serializer = new ByteArrayCrLfSerializer ();
185
175
final List <Message <?>> responses = new ArrayList <Message <?>>();
186
176
final Semaphore semaphore = new Semaphore (0 );
187
- AbstractServerConnectionFactory scf = getConnectionFactory (serializer , new TcpListener () {
188
-
189
- @ Override
190
- public boolean onMessage (Message <?> message ) {
191
- responses .add (message );
192
- semaphore .release ();
193
- return false ;
194
- }
195
-
177
+ AbstractServerConnectionFactory scf = getConnectionFactory (serializer , message -> {
178
+ responses .add (message );
179
+ semaphore .release ();
180
+ return false ;
196
181
});
197
182
198
183
// Fire up the sender.
@@ -203,9 +188,9 @@ public boolean onMessage(Message<?> message) {
203
188
assertTrue (semaphore .tryAcquire (1 , 10000 , TimeUnit .MILLISECONDS ));
204
189
assertEquals ("Did not receive data" , 2 , responses .size ());
205
190
assertEquals ("Data" , SocketTestUtils .TEST_STRING + SocketTestUtils .TEST_STRING ,
206
- new String (((Message <byte []>) responses .get (0 )).getPayload ()));
191
+ new String (((Message <byte []>) responses .get (0 )).getPayload ()));
207
192
assertEquals ("Data" , SocketTestUtils .TEST_STRING + SocketTestUtils .TEST_STRING ,
208
- new String (((Message <byte []>) responses .get (1 )).getPayload ()));
193
+ new String (((Message <byte []>) responses .get (1 )).getPayload ()));
209
194
scf .stop ();
210
195
done .countDown ();
211
196
}
@@ -251,7 +236,8 @@ public void removeDeadConnection(TcpConnection connection) {
251
236
assertTrue (errorMessageLetch .await (10 , TimeUnit .SECONDS ));
252
237
253
238
assertThat (errorMessageRef .get ().getMessage (),
254
- containsString ("Message length 2147483647 exceeds max message length: 2048" ));
239
+ anyOf (containsString ("Message length 2147483647 exceeds max message length: 2048" ),
240
+ containsString ("Connection is closed" )));
255
241
256
242
assertTrue (semaphore .tryAcquire (10000 , TimeUnit .MILLISECONDS ));
257
243
assertTrue (removed .size () > 0 );
@@ -510,11 +496,13 @@ private void testClosureMidMessageGuts(AbstractByteArraySerializer serializer, S
510
496
}
511
497
return false ;
512
498
}, new TcpSender () {
499
+
513
500
@ Override
514
501
public void addNewConnection (TcpConnection connection ) {
515
502
added .add (connection );
516
503
semaphore .release ();
517
504
}
505
+
518
506
@ Override
519
507
public void removeDeadConnection (TcpConnection connection ) {
520
508
removed .add (connection );
0 commit comments