@@ -21,6 +21,7 @@ import (
21
21
"context"
22
22
"crypto/tls"
23
23
"fmt"
24
+ "log"
24
25
"net/http"
25
26
"net/http/httptest"
26
27
"os"
@@ -1123,3 +1124,117 @@ func TestServiceUrlTLSWithTLSTransportWithBasicAuth(t *testing.T) {
1123
1124
func TestWebServiceUrlTLSWithTLSTransportWithBasicAuth (t * testing.T ) {
1124
1125
testTLSTransportWithBasicAuth (t , webServiceURLTLS )
1125
1126
}
1127
+
1128
+ func TestConfigureConnectionMaxIdleTime (t * testing.T ) {
1129
+ _ , err := NewClient (ClientOptions {
1130
+ URL : serviceURL ,
1131
+ ConnectionMaxIdleTime : 1 * time .Second ,
1132
+ })
1133
+
1134
+ assert .Error (t , err , "Should be failed when the connectionMaxIdleTime is less than minConnMaxIdleTime" )
1135
+
1136
+ cli , err := NewClient (ClientOptions {
1137
+ URL : serviceURL ,
1138
+ ConnectionMaxIdleTime : - 1 , // Disabled
1139
+ })
1140
+
1141
+ assert .Nil (t , err )
1142
+ cli .Close ()
1143
+
1144
+ cli , err = NewClient (ClientOptions {
1145
+ URL : serviceURL ,
1146
+ ConnectionMaxIdleTime : 60 * time .Second ,
1147
+ })
1148
+
1149
+ assert .Nil (t , err )
1150
+ cli .Close ()
1151
+ }
1152
+
1153
+ func testSendAndReceive (t * testing.T , producer Producer , consumer Consumer ) {
1154
+ // send 10 messages
1155
+ for i := 0 ; i < 10 ; i ++ {
1156
+ if _ , err := producer .Send (context .Background (), & ProducerMessage {
1157
+ Payload : []byte (fmt .Sprintf ("hello-%d" , i )),
1158
+ }); err != nil {
1159
+ log .Fatal (err )
1160
+ }
1161
+ }
1162
+
1163
+ // receive 10 messages
1164
+ for i := 0 ; i < 10 ; i ++ {
1165
+ msg , err := consumer .Receive (context .Background ())
1166
+ if err != nil {
1167
+ log .Fatal (err )
1168
+ }
1169
+
1170
+ expectMsg := fmt .Sprintf ("hello-%d" , i )
1171
+ assert .Equal (t , []byte (expectMsg ), msg .Payload ())
1172
+ // ack message
1173
+ err = consumer .Ack (msg )
1174
+ if err != nil {
1175
+ return
1176
+ }
1177
+ }
1178
+ }
1179
+
1180
+ func TestAutoCloseIdleConnection (t * testing.T ) {
1181
+ cli , err := NewClient (ClientOptions {
1182
+ URL : serviceURL ,
1183
+ ConnectionMaxIdleTime : - 1 , // Disable auto release connections first, we will enable it manually later
1184
+ })
1185
+
1186
+ assert .Nil (t , err )
1187
+
1188
+ topic := "TestAutoCloseIdleConnection"
1189
+
1190
+ // create consumer
1191
+ consumer1 , err := cli .Subscribe (ConsumerOptions {
1192
+ Topic : topic ,
1193
+ SubscriptionName : "my-sub" ,
1194
+ })
1195
+ assert .Nil (t , err )
1196
+
1197
+ // create producer
1198
+ producer1 , err := cli .CreateProducer (ProducerOptions {
1199
+ Topic : topic ,
1200
+ DisableBatching : false ,
1201
+ })
1202
+ assert .Nil (t , err )
1203
+
1204
+ testSendAndReceive (t , producer1 , consumer1 )
1205
+
1206
+ pool := cli .(* client ).cnxPool
1207
+
1208
+ producer1 .Close ()
1209
+ consumer1 .Close ()
1210
+
1211
+ assert .NotEqual (t , 0 , internal .GetConnectionsCount (& pool ))
1212
+
1213
+ internal .StartCleanConnectionsTask (& pool , 2 * time .Second ) // Enable auto idle connections release manually
1214
+
1215
+ time .Sleep (6 * time .Second ) // Need to wait at least 3 * ConnectionMaxIdleTime
1216
+
1217
+ assert .Equal (t , 0 , internal .GetConnectionsCount (& pool ))
1218
+
1219
+ // create consumer
1220
+ consumer2 , err := cli .Subscribe (ConsumerOptions {
1221
+ Topic : topic ,
1222
+ SubscriptionName : "my-sub" ,
1223
+ })
1224
+ assert .Nil (t , err )
1225
+
1226
+ // create producer
1227
+ producer2 , err := cli .CreateProducer (ProducerOptions {
1228
+ Topic : topic ,
1229
+ DisableBatching : false ,
1230
+ })
1231
+ assert .Nil (t , err )
1232
+
1233
+ // Ensure the client still works
1234
+ testSendAndReceive (t , producer2 , consumer2 )
1235
+
1236
+ producer2 .Close ()
1237
+ consumer2 .Close ()
1238
+
1239
+ cli .Close ()
1240
+ }
0 commit comments