Skip to content

Commit

Permalink
bugfix: TestConnectionHandlerUpdateError data race
Browse files Browse the repository at this point in the history
It is better to use atomic counters.

Part of #218
  • Loading branch information
oleg-jukovec committed Jan 16, 2023
1 parent 6a51ef1 commit bda241e
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions connection_pool/connection_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,18 +403,14 @@ func TestConnectionHandlerOpenError(t *testing.T) {
}

type testUpdateErrorHandler struct {
discovered, deactivated int
mutex sync.Mutex
discovered, deactivated uint32
}

func (h *testUpdateErrorHandler) Discovered(conn *tarantool.Connection,
role connection_pool.Role) error {
h.mutex.Lock()
defer h.mutex.Unlock()

h.discovered++
atomic.AddUint32(&h.discovered, 1)

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

func (h *testUpdateErrorHandler) Deactivated(conn *tarantool.Connection,
role connection_pool.Role) error {
h.mutex.Lock()
defer h.mutex.Unlock()

h.deactivated++
atomic.AddUint32(&h.deactivated, 1)
return nil
}

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

require.Nilf(t, err, "failed to get ConnectedNow()")
require.Falsef(t, connected, "should be deactivated")
require.GreaterOrEqualf(t, h.discovered, h.deactivated, "discovered < deactivated")
discovered := atomic.LoadUint32(&h.discovered)
deactivated := atomic.LoadUint32(&h.deactivated)
require.GreaterOrEqualf(t, discovered, deactivated, "discovered < deactivated")
require.Nilf(t, err, "failed to get ConnectedNow()")
}

Expand Down

0 comments on commit bda241e

Please sign in to comment.