Skip to content

Commit

Permalink
[#7] .Update added test for race condition
Browse files Browse the repository at this point in the history
Calling IsConnected() and using the pool at the same time
  • Loading branch information
ThomasObenaus committed Feb 12, 2020
1 parent 7872c06 commit d9b1faa
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package gremtune

import (
"fmt"
"math/rand"
"sync"
"testing"
"time"

Expand All @@ -13,6 +15,53 @@ import (
"github.com/stretchr/testify/require"
)

func TestIsConnectedRace(t *testing.T) {
// This test shall detect data races when
// checking the connection state of the pool
// and using the pool at the same time

// GIVEN
logger := zerolog.Nop()
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedQueryExecutor := mock_interfaces.NewMockQueryExecutor(mockCtrl)
clientFactory := func() (interfaces.QueryExecutor, error) {
return mockedQueryExecutor, nil
}
pool, err := NewPool(clientFactory, 2, time.Second*30, logger)
require.NoError(t, err)
require.NotNil(t, pool)

mockedQueryExecutor.EXPECT().LastError().Return(nil).AnyTimes()
mockedQueryExecutor.EXPECT().IsConnected().Return(true).AnyTimes()
mockedQueryExecutor.EXPECT().Close().Return(nil).AnyTimes()

ticker := time.NewTicker(time.Millisecond * 100)
go func() {
for range ticker.C {
pool.IsConnected()
}
}()

numConnectionsToAcquire := 100
wg := sync.WaitGroup{}
wg.Add(numConnectionsToAcquire)
for i := 0; i < numConnectionsToAcquire; i++ {
go func() {
defer wg.Done()
pc, err := pool.Get()
require.NoError(t, err)
require.NotNil(t, pc)
pc.Close()
millies := rand.Intn(200)
time.Sleep(time.Millisecond * time.Duration(millies))
}()
}
wg.Wait()
ticker.Stop()
pool.Close()
}

func TestIsConnected(t *testing.T) {
// GIVEN
logger := zerolog.Nop()
Expand Down

0 comments on commit d9b1faa

Please sign in to comment.