1
1
/*
2
- * Copyright 2019 the original author or authors.
2
+ * Copyright 2019-2020 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.
22
22
import static org .mockito .Mockito .mock ;
23
23
24
24
import java .io .IOException ;
25
+ import java .util .Properties ;
26
+ import java .util .concurrent .CountDownLatch ;
25
27
import java .util .concurrent .ExecutorService ;
28
+ import java .util .concurrent .TimeUnit ;
26
29
import java .util .concurrent .TimeoutException ;
27
30
import java .util .concurrent .atomic .AtomicBoolean ;
31
+ import java .util .stream .IntStream ;
28
32
29
33
import org .junit .jupiter .api .Test ;
30
34
35
+ import org .springframework .amqp .rabbit .connection .CachingConnectionFactory .ConfirmType ;
36
+ import org .springframework .amqp .rabbit .core .RabbitTemplate ;
37
+ import org .springframework .amqp .rabbit .junit .RabbitAvailable ;
38
+ import org .springframework .amqp .rabbit .junit .RabbitAvailableCondition ;
39
+ import org .springframework .core .task .SimpleAsyncTaskExecutor ;
40
+
31
41
import com .rabbitmq .client .Channel ;
32
42
import com .rabbitmq .client .Method ;
33
43
import com .rabbitmq .client .ShutdownListener ;
38
48
* @since 2.1.5
39
49
*
40
50
*/
51
+ @ RabbitAvailable
41
52
public class PublisherCallbackChannelTests {
42
53
43
54
@ Test
44
- public void shutdownWhileCreate () throws IOException , TimeoutException {
55
+ void shutdownWhileCreate () throws IOException , TimeoutException {
45
56
Channel delegate = mock (Channel .class );
46
57
AtomicBoolean npe = new AtomicBoolean ();
47
58
willAnswer (inv -> {
@@ -59,4 +70,72 @@ public void shutdownWhileCreate() throws IOException, TimeoutException {
59
70
channel .close ();
60
71
}
61
72
73
+ @ Test
74
+ void testNotCached () throws Exception {
75
+ CachingConnectionFactory cf = new CachingConnectionFactory (
76
+ RabbitAvailableCondition .getBrokerRunning ().getConnectionFactory ());
77
+ cf .setPublisherConfirmType (ConfirmType .CORRELATED );
78
+ cf .setChannelCacheSize (2 );
79
+ cf .afterPropertiesSet ();
80
+ RabbitTemplate template = new RabbitTemplate (cf );
81
+ CountDownLatch confirmLatch = new CountDownLatch (2 );
82
+ template .setConfirmCallback ((correlationData , ack , cause ) -> {
83
+ try {
84
+ Thread .sleep (50 );
85
+ confirmLatch .countDown ();
86
+ }
87
+ catch (InterruptedException e ) {
88
+ Thread .currentThread ().interrupt ();
89
+ }
90
+ });
91
+ CountDownLatch openedLatch = new CountDownLatch (2 );
92
+ CountDownLatch closeLatch = new CountDownLatch (1 );
93
+ CountDownLatch closedLatch = new CountDownLatch (2 );
94
+ CountDownLatch waitForOtherLatch = new CountDownLatch (1 );
95
+ SimpleAsyncTaskExecutor exec = new SimpleAsyncTaskExecutor ();
96
+ IntStream .range (0 , 2 ).forEach (i -> {
97
+ // this will open 3 or 4 channels
98
+ exec .execute (() -> {
99
+ template .execute (chann -> {
100
+ openedLatch .countDown ();
101
+ template .convertAndSend ("" , "foo" , "msg" , msg -> {
102
+ if (i == 0 ) {
103
+ try {
104
+ waitForOtherLatch .await ();
105
+ }
106
+ catch (InterruptedException e ) {
107
+ Thread .currentThread ().interrupt ();
108
+ }
109
+ }
110
+ else {
111
+ waitForOtherLatch .countDown ();
112
+ }
113
+ return msg ;
114
+ }, new CorrelationData ("" + i ));
115
+ closeLatch .await ();
116
+ return null ;
117
+ });
118
+ closedLatch .countDown ();
119
+ });
120
+ });
121
+ assertThat (openedLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
122
+ Connection conn = cf .createConnection ();
123
+ Channel chan1 = conn .createChannel (false );
124
+ Channel chan2 = conn .createChannel (false );
125
+ chan1 .close ();
126
+ chan2 .close ();
127
+ Properties cacheProperties = cf .getCacheProperties ();
128
+ assertThat (cacheProperties .getProperty ("idleChannelsNotTx" )).isEqualTo ("2" );
129
+ closeLatch .countDown ();
130
+ assertThat (confirmLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
131
+ assertThat (closedLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
132
+ cacheProperties = cf .getCacheProperties ();
133
+ int n = 0 ;
134
+ while (n ++ < 100 && Integer .parseInt (cacheProperties .getProperty ("idleChannelsNotTx" )) < 2 ) {
135
+ Thread .sleep (100 );
136
+ cacheProperties = cf .getCacheProperties ();
137
+ }
138
+ assertThat (cacheProperties .getProperty ("idleChannelsNotTx" )).isEqualTo ("2" );
139
+ }
140
+
62
141
}
0 commit comments