Skip to content

Commit

Permalink
Stop hashring in test and check for leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrotx committed Sep 12, 2024
1 parent 9030438 commit 354faab
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions common/membership/hashring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"go.uber.org/goleak"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/clock"
Expand Down Expand Up @@ -99,6 +100,7 @@ func Test_ring_compareMembers(t *testing.T) {
}

type hashringTestData struct {
t *testing.T
mockPeerProvider *MockPeerProvider
mockTimeSource clock.MockedTimeSource
hashRing *ring
Expand All @@ -108,6 +110,7 @@ func newHashringTestData(t *testing.T) *hashringTestData {
var td hashringTestData

ctrl := gomock.NewController(t)
td.t = t
td.mockPeerProvider = NewMockPeerProvider(ctrl)
td.mockTimeSource = clock.NewMockedTimeSourceAt(time.Now())

Expand All @@ -122,13 +125,25 @@ func newHashringTestData(t *testing.T) *hashringTestData {
return &td
}

// starts hashring' background work and verifies all the goroutines closed at the end
func (td *hashringTestData) startHashRing() {
td.mockPeerProvider.EXPECT().Stop()

td.t.Cleanup(func() {
td.hashRing.Stop()
goleak.VerifyNone(td.t)
})

td.hashRing.Start()
}

func TestFailedLookupWillAskProvider(t *testing.T) {
td := newHashringTestData(t)

td.mockPeerProvider.EXPECT().Subscribe(gomock.Any(), gomock.Any()).Times(1)
td.mockPeerProvider.EXPECT().GetMembers("test-service").Times(1)

td.hashRing.Start()
td.startHashRing()
_, err := td.hashRing.Lookup("a")

assert.Error(t, err)
Expand All @@ -141,15 +156,14 @@ func TestRefreshUpdatesRingOnlyWhenRingHasChanged(t *testing.T) {
td.mockPeerProvider.EXPECT().GetMembers("test-service").Times(1).Return(randomHostInfo(3), nil)

// Start will also call .refresh()
td.hashRing.Start()
td.startHashRing()
updatedAt := td.hashRing.members.refreshed
td.hashRing.refresh()
refreshed, err := td.hashRing.refresh()

assert.NoError(t, err)
assert.False(t, refreshed)
assert.Equal(t, updatedAt, td.hashRing.members.refreshed)

}

func TestRefreshWillNotifySubscribers(t *testing.T) {
Expand All @@ -168,7 +182,7 @@ func TestRefreshWillNotifySubscribers(t *testing.T) {
HostsRemoved: []string{"c"},
}

td.hashRing.Start()
td.startHashRing()

var changeCh = make(chan *ChangedEvent, 2)
// Check if multiple subscribers will get notified
Expand Down Expand Up @@ -206,7 +220,7 @@ func TestSubscribersAreNotifiedPeriodically(t *testing.T) {
})
td.mockPeerProvider.EXPECT().WhoAmI().AnyTimes()

td.hashRing.Start()
td.startHashRing()

var changeCh = make(chan *ChangedEvent, 1)
assert.NoError(t, td.hashRing.Subscribe("subscriber1", changeCh))
Expand Down Expand Up @@ -301,7 +315,7 @@ func TestLookupAndRefreshRaceCondition(t *testing.T) {
return randomHostInfo(5), nil
})

td.hashRing.Start()
td.startHashRing()
wg.Add(2)
go func() {
for i := 0; i < 50; i++ {
Expand All @@ -323,7 +337,6 @@ func TestLookupAndRefreshRaceCondition(t *testing.T) {
}

func TestEmitHashringView(t *testing.T) {

tests := map[string]struct {
hosts []HostInfo
lookuperr error
Expand Down Expand Up @@ -362,7 +375,6 @@ func TestEmitHashringView(t *testing.T) {
}

for testName, testInput := range tests {

t.Run(testName, func(t *testing.T) {
td := newHashringTestData(t)

Expand Down

0 comments on commit 354faab

Please sign in to comment.