Skip to content

Commit e29d43b

Browse files
committedApr 19, 2018
[FAB-9087] Sync conn decrement in deliverclient test
The connection number in the client_test is tracked in many tests to ensure that no connection leak occurs, and is compared to 0. The connection number is incremented via an implementation of a gRPC load balancer which is passed to the connection creation, and is decremented via the implementation of Close() of that load balancer. Sadly, the Close() method in the load balancer is called from a different goroutine (the gRPC connection closes the "done channel" of the balancer) and thus we need to: 1) Synchronize between the decrement and the read of the connNum value 2) Ensure the read of the connNum value takes place after the connNum was decremented, while it is decremented from a different goroutine. For that I just added a waitgroup. Change-Id: I64f3e33c3a61c53675b3a31abf42a2be2cd4798d Signed-off-by: yacovm <yacovm@il.ibm.com>
1 parent 5d23c48 commit e29d43b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed
 

‎core/deliverservice/client_test.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import (
2626
"google.golang.org/grpc"
2727
)
2828

29-
var connNumber = 0
29+
var (
30+
connNumber = 0
31+
connWG sync.WaitGroup
32+
)
3033

3134
func newConnection() *grpc.ClientConn {
3235
// The balancer is in order to check connection leaks.
@@ -40,6 +43,7 @@ type balancer struct {
4043
}
4144

4245
func (*balancer) Start(target string, config grpc.BalancerConfig) error {
46+
connWG.Add(1)
4347
connNumber++
4448
return nil
4549
}
@@ -58,6 +62,7 @@ func (*balancer) Notify() <-chan []grpc.Address {
5862

5963
func (*balancer) Close() error {
6064
connNumber--
65+
connWG.Done()
6166
return nil
6267
}
6368

@@ -154,6 +159,7 @@ func (cp *connProducer) DisableEndpoint(endpoint string) {
154159
func TestOrderingServiceConnFailure(t *testing.T) {
155160
testOrderingServiceConnFailure(t, blockDelivererConsumerWithRecv)
156161
testOrderingServiceConnFailure(t, blockDelivererConsumerWithSend)
162+
connWG.Wait()
157163
assert.Equal(t, 0, connNumber)
158164
}
159165

@@ -191,6 +197,7 @@ func testOrderingServiceConnFailure(t *testing.T, bdc blocksDelivererConsumer) {
191197
func TestOrderingServiceStreamFailure(t *testing.T) {
192198
testOrderingServiceStreamFailure(t, blockDelivererConsumerWithRecv)
193199
testOrderingServiceStreamFailure(t, blockDelivererConsumerWithSend)
200+
connWG.Wait()
194201
assert.Equal(t, 0, connNumber)
195202
}
196203

@@ -227,6 +234,7 @@ func testOrderingServiceStreamFailure(t *testing.T, bdc blocksDelivererConsumer)
227234
func TestOrderingServiceSetupFailure(t *testing.T) {
228235
testOrderingServiceSetupFailure(t, blockDelivererConsumerWithRecv)
229236
testOrderingServiceSetupFailure(t, blockDelivererConsumerWithSend)
237+
connWG.Wait()
230238
assert.Equal(t, 0, connNumber)
231239
}
232240

@@ -261,6 +269,7 @@ func testOrderingServiceSetupFailure(t *testing.T, bdc blocksDelivererConsumer)
261269
func TestOrderingServiceFirstOperationFailure(t *testing.T) {
262270
testOrderingServiceFirstOperationFailure(t, blockDelivererConsumerWithRecv)
263271
testOrderingServiceFirstOperationFailure(t, blockDelivererConsumerWithSend)
272+
connWG.Wait()
264273
assert.Equal(t, 0, connNumber)
265274
}
266275

@@ -298,6 +307,7 @@ func testOrderingServiceFirstOperationFailure(t *testing.T, bdc blocksDelivererC
298307
func TestOrderingServiceCrashAndRecover(t *testing.T) {
299308
testOrderingServiceCrashAndRecover(t, blockDelivererConsumerWithRecv)
300309
testOrderingServiceCrashAndRecover(t, blockDelivererConsumerWithSend)
310+
connWG.Wait()
301311
assert.Equal(t, 0, connNumber)
302312
}
303313

@@ -339,6 +349,7 @@ func testOrderingServiceCrashAndRecover(t *testing.T, bdc blocksDelivererConsume
339349
func TestOrderingServicePermanentCrash(t *testing.T) {
340350
testOrderingServicePermanentCrash(t, blockDelivererConsumerWithRecv)
341351
testOrderingServicePermanentCrash(t, blockDelivererConsumerWithSend)
352+
connWG.Wait()
342353
assert.Equal(t, 0, connNumber)
343354
}
344355

@@ -377,6 +388,7 @@ func testOrderingServicePermanentCrash(t *testing.T, bdc blocksDelivererConsumer
377388
func TestLimitedConnAttempts(t *testing.T) {
378389
testLimitedConnAttempts(t, blockDelivererConsumerWithRecv)
379390
testLimitedConnAttempts(t, blockDelivererConsumerWithSend)
391+
connWG.Wait()
380392
assert.Equal(t, 0, connNumber)
381393
}
382394

@@ -405,11 +417,13 @@ func testLimitedConnAttempts(t *testing.T, bdc blocksDelivererConsumer) {
405417

406418
func TestLimitedTotalConnTimeRcv(t *testing.T) {
407419
testLimitedTotalConnTime(t, blockDelivererConsumerWithRecv)
420+
connWG.Wait()
408421
assert.Equal(t, 0, connNumber)
409422
}
410423

411424
func TestLimitedTotalConnTimeSnd(t *testing.T) {
412425
testLimitedTotalConnTime(t, blockDelivererConsumerWithSend)
426+
connWG.Wait()
413427
assert.Equal(t, 0, connNumber)
414428
}
415429

@@ -441,6 +455,7 @@ func testLimitedTotalConnTime(t *testing.T, bdc blocksDelivererConsumer) {
441455
func TestGreenPath(t *testing.T) {
442456
testGreenPath(t, blockDelivererConsumerWithRecv)
443457
testGreenPath(t, blockDelivererConsumerWithSend)
458+
connWG.Wait()
444459
assert.Equal(t, 0, connNumber)
445460
}
446461

@@ -507,6 +522,7 @@ func TestCloseWhileRecv(t *testing.T) {
507522
func TestCloseWhileSleep(t *testing.T) {
508523
testCloseWhileSleep(t, blockDelivererConsumerWithRecv)
509524
testCloseWhileSleep(t, blockDelivererConsumerWithSend)
525+
connWG.Wait()
510526
assert.Equal(t, 0, connNumber)
511527
}
512528

0 commit comments

Comments
 (0)
Please sign in to comment.