Skip to content

Commit

Permalink
Merge pull request #1688 from mysteriumnetwork/tk/improve-tests
Browse files Browse the repository at this point in the history
Improve test stability
  • Loading branch information
vkuznecovas authored Feb 12, 2020
2 parents 109ddb2 + 5d70be8 commit 8a83a4d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 45 deletions.
10 changes: 5 additions & 5 deletions core/discovery/brokerdiscovery/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ func Test_Subscriber_StartSyncsNewProposals(t *testing.T) {
"proposal": {"provider_id": "0x1"}
}`)

assert.Eventually(t, proposalCountEquals(repo, 1), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, proposalCountEquals(repo, 1), 2*time.Second, 1*time.Millisecond)
assert.Exactly(t, []market.ServiceProposal{proposalFirst}, repo.storage.Proposals())

proposalRegister(connection, `{
"proposal": {"provider_id": "0x2"}
}`)

assert.Eventually(t, proposalCountEquals(repo, 2), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, proposalCountEquals(repo, 2), 2*time.Second, 1*time.Millisecond)
assert.Exactly(t, []market.ServiceProposal{proposalFirst, proposalSecond}, repo.storage.Proposals())
}

Expand All @@ -78,7 +78,7 @@ func Test_Subscriber_StartSyncsIdleProposals(t *testing.T) {
proposalRegister(connection, `{
"proposal": {"provider_id": "0x1"}
}`)
assert.Eventually(t, proposalCountEquals(repo, 0), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, proposalCountEquals(repo, 0), 2*time.Second, 1*time.Millisecond)
}

func Test_Subscriber_StartSyncsHealthyProposals(t *testing.T) {
Expand All @@ -98,7 +98,7 @@ func Test_Subscriber_StartSyncsHealthyProposals(t *testing.T) {
"proposal": {"provider_id": "0x1"}
}`)

assert.Eventually(t, proposalCountEquals(repo, 1), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, proposalCountEquals(repo, 1), 2*time.Second, 1*time.Millisecond)
assert.Exactly(t, []market.ServiceProposal{proposalFirst}, repo.storage.Proposals())
}

Expand All @@ -116,7 +116,7 @@ func Test_Subscriber_StartSyncsStoppedProposals(t *testing.T) {
"proposal": {"provider_id": "0x1"}
}`)

assert.Eventually(t, proposalCountEquals(repo, 1), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, proposalCountEquals(repo, 1), 2*time.Second, 1*time.Millisecond)
assert.Exactly(t, []market.ServiceProposal{proposalSecond}, repo.storage.Proposals())
}

Expand Down
6 changes: 3 additions & 3 deletions core/promise/methods/noop/promise_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestPromiseProcessor_Start_SendsBalanceMessages(t *testing.T) {
defer processor.Stop()

assert.NoError(t, err)
assert.Eventually(t, balanceStateMatches(processor, balanceNotifying), 1*time.Second, 1*time.Millisecond)
assert.Eventually(t, balanceStateMatches(processor, balanceNotifying), 2*time.Second, 10*time.Millisecond)

lastMessage, err := dialog.waitSendMessage()
assert.NoError(t, err)
Expand All @@ -68,11 +68,11 @@ func TestPromiseProcessor_Stop_StopsBalanceMessages(t *testing.T) {
}
err := processor.Start(proposal)
assert.NoError(t, err)
assert.Eventually(t, balanceStateMatches(processor, balanceNotifying), 1*time.Second, 1*time.Millisecond)
assert.Eventually(t, balanceStateMatches(processor, balanceNotifying), 2*time.Second, 10*time.Millisecond)

err = processor.Stop()
assert.NoError(t, err)
assert.Eventually(t, balanceStateMatches(processor, balanceStopped), 1*time.Second, 1*time.Millisecond)
assert.Eventually(t, balanceStateMatches(processor, balanceStopped), 2*time.Second, 10*time.Millisecond)
}

func balanceStateMatches(processor *PromiseProcessor, expected balanceState) func() bool {
Expand Down
63 changes: 33 additions & 30 deletions core/state/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,24 @@ import (
)

type debounceTester struct {
timesCalled int
lock sync.Mutex
numInteractions int
lock sync.Mutex
}

type interactionCounter interface {
interactions() int
}

func (dt *debounceTester) do(interface{}) {
dt.lock.Lock()
dt.timesCalled++
dt.numInteractions++
dt.lock.Unlock()
}

func (dt *debounceTester) get() int {
func (dt *debounceTester) interactions() int {
dt.lock.Lock()
defer dt.lock.Unlock()
return dt.timesCalled
return dt.numInteractions
}

func Test_Debounce_CallsOnceInInterval(t *testing.T) {
Expand All @@ -56,9 +60,7 @@ func Test_Debounce_CallsOnceInInterval(t *testing.T) {
for i := 1; i < 10; i++ {
f(struct{}{})
}

time.Sleep(duration * 2)
assert.Equal(t, 1, dt.get())
assert.Eventually(t, interacted(dt, 1), 2*time.Second, 10*time.Millisecond)
}

var mockNATStatus = nat.Status{
Expand All @@ -67,22 +69,22 @@ var mockNATStatus = nat.Status{
}

type natStatusProviderMock struct {
statusToReturn nat.Status
timesCalled int
lock sync.Mutex
statusToReturn nat.Status
numInteractions int
lock sync.Mutex
}

func (nspm *natStatusProviderMock) Status() nat.Status {
nspm.lock.Lock()
defer nspm.lock.Unlock()
nspm.timesCalled++
nspm.numInteractions++
return nspm.statusToReturn
}

func (nspm *natStatusProviderMock) getTimesCalled() int {
func (nspm *natStatusProviderMock) interactions() int {
nspm.lock.Lock()
defer nspm.lock.Unlock()
return nspm.timesCalled
return nspm.numInteractions
}

func (nspm *natStatusProviderMock) ConsumeNATEvent(event natEvent.Event) {}
Expand All @@ -102,40 +104,40 @@ func (mp *mockPublisher) Publish(topic string, data interface{}) {

type serviceListerMock struct {
lock sync.Mutex
timesCalled int
numInteractions int
servicesToReturn map[service.ID]*service.Instance
}

func (slm *serviceListerMock) getTimesCalled() int {
func (slm *serviceListerMock) interactions() int {
slm.lock.Lock()
defer slm.lock.Unlock()
return slm.timesCalled
return slm.numInteractions
}

func (slm *serviceListerMock) List() map[service.ID]*service.Instance {
slm.lock.Lock()
defer slm.lock.Unlock()
slm.timesCalled++
slm.numInteractions++
return slm.servicesToReturn
}

type serviceSessionStorageMock struct {
timesCalled int
numInteractions int
sessionsToReturn []session.Session
lock sync.Mutex
}

func (sssm *serviceSessionStorageMock) GetAll() []session.Session {
sssm.lock.Lock()
defer sssm.lock.Unlock()
sssm.timesCalled++
sssm.numInteractions++
return sssm.sessionsToReturn
}

func (sssm *serviceSessionStorageMock) getTimesCalled() int {
func (sssm *serviceSessionStorageMock) interactions() int {
sssm.lock.Lock()
defer sssm.lock.Unlock()
return sssm.timesCalled
return sssm.numInteractions
}

func Test_ConsumesNATEvents(t *testing.T) {
Expand All @@ -158,8 +160,7 @@ func Test_ConsumesNATEvents(t *testing.T) {
})
}

time.Sleep(duration * 3)
assert.Equal(t, 1, natProvider.getTimesCalled())
assert.Eventually(t, interacted(natProvider, 1), 2*time.Second, 10*time.Millisecond)

assert.Equal(t, natProvider.statusToReturn.Error.Error(), keeper.GetState().NATStatus.Error)
assert.Equal(t, natProvider.statusToReturn.Status, keeper.GetState().NATStatus.Status)
Expand Down Expand Up @@ -187,8 +188,7 @@ func Test_ConsumesSessionEvents(t *testing.T) {
keeper.ConsumeSessionStateEvent(sessionEvent.Payload{})
}

time.Sleep(duration * 3)
assert.Equal(t, 1, sessionStorage.getTimesCalled())
assert.Eventually(t, interacted(sessionStorage, 1), 2*time.Second, 10*time.Millisecond)

assert.Equal(t, string(expected.ID), keeper.GetState().Sessions[0].ID)
assert.Equal(t, expected.ConsumerID.Address, keeper.GetState().Sessions[0].ConsumerID)
Expand Down Expand Up @@ -252,10 +252,7 @@ func Test_ConsumesServiceEvents(t *testing.T) {
keeper.ConsumeServiceStateEvent(service.EventPayload{})
}

select {
case <-time.After(duration * 3):
assert.Equal(t, 1, sl.getTimesCalled())
}
assert.Eventually(t, interacted(sl, 1), 2*time.Second, 10*time.Millisecond)

actual := keeper.GetState().Services[0]
assert.Equal(t, string(id), actual.ID)
Expand Down Expand Up @@ -362,3 +359,9 @@ func Test_incrementConnectionCount(t *testing.T) {
assert.Equal(t, 1, s.ConnectionStatistics.Successful)
assert.Equal(t, 1, s.ConnectionStatistics.Attempted)
}

func interacted(c interactionCounter, times int) func() bool {
return func() bool {
return c.interactions() == times
}
}
10 changes: 5 additions & 5 deletions session/event_based_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestEventBasedStorage_PublishesEventsOnCreate(t *testing.T) {

sessionStore.Add(session)

assert.Eventually(t, lastEventMatches(mp, session.ID, sessionEvent.Created), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, lastEventMatches(mp, session.ID, sessionEvent.Created), 2*time.Second, 10*time.Millisecond)
}

func TestEventBasedStorage_PublishesEventsOnDelete(t *testing.T) {
Expand All @@ -45,7 +45,7 @@ func TestEventBasedStorage_PublishesEventsOnDelete(t *testing.T) {

sessionStore.Remove(session.ID)

assert.Eventually(t, lastEventMatches(mp, session.ID, sessionEvent.Removed), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, lastEventMatches(mp, session.ID, sessionEvent.Removed), 2*time.Second, 10*time.Millisecond)
}

func TestEventBasedStorage_PublishesEventsOnDataTransferUpdate(t *testing.T) {
Expand All @@ -58,7 +58,7 @@ func TestEventBasedStorage_PublishesEventsOnDataTransferUpdate(t *testing.T) {

sessionStore.UpdateDataTransfer(session.ID, 1, 2)

assert.Eventually(t, lastEventMatches(mp, session.ID, sessionEvent.Updated), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, lastEventMatches(mp, session.ID, sessionEvent.Updated), 2*time.Second, 10*time.Millisecond)
}

func TestNewEventBasedStorage_HandlesAppEventTokensEarned(t *testing.T) {
Expand All @@ -82,7 +82,7 @@ func TestNewEventBasedStorage_HandlesAppEventTokensEarned(t *testing.T) {
storedSession, ok = sessionStore.Find(session.ID)
assert.True(t, ok)
assert.EqualValues(t, 500, storedSession.TokensEarned)
assert.Eventually(t, lastEventMatches(mp, session.ID, sessionEvent.Updated), 1*time.Second, 5*time.Millisecond)
assert.Eventually(t, lastEventMatches(mp, session.ID, sessionEvent.Updated), 2*time.Second, 10*time.Millisecond)
}

func TestEventBasedStorage_PublishesEventsOnRemoveForService(t *testing.T) {
Expand All @@ -95,7 +95,7 @@ func TestEventBasedStorage_PublishesEventsOnRemoveForService(t *testing.T) {

sessionStore.RemoveForService("whatever")

assert.Eventually(t, lastEventMatches(mp, "", sessionEvent.Removed), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, lastEventMatches(mp, "", sessionEvent.Removed), 2*time.Second, 10*time.Millisecond)
}

func lastEventMatches(mp *mockPublisher, id ID, action sessionEvent.Action) func() bool {
Expand Down
2 changes: 1 addition & 1 deletion session/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,5 @@ func TestManager_AcknowledgeSession_PublishesEvent(t *testing.T) {
err = manager.Acknowledge(consumerID, string(sessionInstance.ID))
assert.Nil(t, err)

assert.Eventually(t, lastEventMatches(mp, sessionInstance.ID, sessionEvent.Acknowledged), 100*time.Millisecond, 1*time.Millisecond)
assert.Eventually(t, lastEventMatches(mp, sessionInstance.ID, sessionEvent.Acknowledged), 2*time.Second, 10*time.Millisecond)
}
2 changes: 1 addition & 1 deletion session/pingpong/invoice_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ func TestInvoiceTracker_TestInvoiceTracker_handleAccountantError_settles(t *test
assert.Eventually(t, func() bool {
p, a := ms.getCalledWith()
return provider.Address == p.Address && accountant.Address == a.Address
}, time.Millisecond*100, time.Millisecond)
}, 2*time.Second, 10*time.Millisecond)
}

func TestInvoiceTracker_handleAccountantError(t *testing.T) {
Expand Down

0 comments on commit 8a83a4d

Please sign in to comment.