Skip to content

Commit bda241e

Browse files
committed
bugfix: TestConnectionHandlerUpdateError data race
It is better to use atomic counters. Part of #218
1 parent 6a51ef1 commit bda241e

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

connection_pool/connection_pool_test.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,18 +403,14 @@ func TestConnectionHandlerOpenError(t *testing.T) {
403403
}
404404

405405
type testUpdateErrorHandler struct {
406-
discovered, deactivated int
407-
mutex sync.Mutex
406+
discovered, deactivated uint32
408407
}
409408

410409
func (h *testUpdateErrorHandler) Discovered(conn *tarantool.Connection,
411410
role connection_pool.Role) error {
412-
h.mutex.Lock()
413-
defer h.mutex.Unlock()
414-
415-
h.discovered++
411+
atomic.AddUint32(&h.discovered, 1)
416412

417-
if h.deactivated != 0 {
413+
if atomic.LoadUint32(&h.deactivated) != 0 {
418414
// Don't add a connection into a pool again after it was deleted.
419415
return fmt.Errorf("any error")
420416
}
@@ -423,10 +419,7 @@ func (h *testUpdateErrorHandler) Discovered(conn *tarantool.Connection,
423419

424420
func (h *testUpdateErrorHandler) Deactivated(conn *tarantool.Connection,
425421
role connection_pool.Role) error {
426-
h.mutex.Lock()
427-
defer h.mutex.Unlock()
428-
429-
h.deactivated++
422+
atomic.AddUint32(&h.deactivated, 1)
430423
return nil
431424
}
432425

@@ -477,7 +470,9 @@ func TestConnectionHandlerUpdateError(t *testing.T) {
477470

478471
require.Nilf(t, err, "failed to get ConnectedNow()")
479472
require.Falsef(t, connected, "should be deactivated")
480-
require.GreaterOrEqualf(t, h.discovered, h.deactivated, "discovered < deactivated")
473+
discovered := atomic.LoadUint32(&h.discovered)
474+
deactivated := atomic.LoadUint32(&h.deactivated)
475+
require.GreaterOrEqualf(t, discovered, deactivated, "discovered < deactivated")
481476
require.Nilf(t, err, "failed to get ConnectedNow()")
482477
}
483478

0 commit comments

Comments
 (0)