Skip to content

Commit

Permalink
Make test not need to stop timers
Browse files Browse the repository at this point in the history
  • Loading branch information
gammazero committed Sep 11, 2024
1 parent 899c842 commit 039e8cd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 41 deletions.
45 changes: 12 additions & 33 deletions assigner/core/assigner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ func TestAssignerAll(t *testing.T) {

var assignNum int
var assigns []int
timeout := time.NewTimer(3 * time.Second)
open := true
for open {
select {
Expand All @@ -137,11 +136,10 @@ func TestAssignerAll(t *testing.T) {
}
t.Log("Publisher", peer2IDStr, "assigned to indexer", assignNum)
assigns = append(assigns, assignNum)
case <-timeout.C:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for assignment")
}
}
timeout.Stop()
require.Equal(t, 1, len(assigns))
require.Equal(t, 0, assigns[0])

Expand All @@ -162,7 +160,6 @@ func TestAssignerAll(t *testing.T) {
require.NoError(t, err)

assigns = assigns[:0]
timeout.Reset(3 * time.Second)
open = true
for open {
select {
Expand All @@ -172,11 +169,10 @@ func TestAssignerAll(t *testing.T) {
}
assigns = append(assigns, assignNum)
t.Log("Publisher", peer3IDStr, "assigned to indexer", assignNum)
case <-timeout.C:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for assignment")
}
}
timeout.Stop()
sort.Ints(assigns)
require.Equal(t, []int{0, 1}, assigns)

Expand Down Expand Up @@ -253,7 +249,6 @@ func TestAssignerOne(t *testing.T) {

var assignNum int
var assigns []int
timeout := time.NewTimer(3 * time.Second)
open := true
for open {
select {
Expand All @@ -263,11 +258,10 @@ func TestAssignerOne(t *testing.T) {
}
t.Log("Publisher", peer2IDStr, "assigned to indexer", assignNum)
assigns = append(assigns, assignNum)
case <-timeout.C:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for assignment")
}
}
timeout.Stop()
require.Equal(t, 1, len(assigns))
require.Equal(t, 0, assigns[0])

Expand All @@ -286,7 +280,6 @@ func TestAssignerOne(t *testing.T) {
require.NoError(t, err)

assigns = assigns[:0]
timeout.Reset(3 * time.Second)
open = true
for open {
select {
Expand All @@ -296,11 +289,10 @@ func TestAssignerOne(t *testing.T) {
}
assigns = append(assigns, assignNum)
t.Log("Publisher", peer3IDStr, "assigned to indexer", assignNum)
case <-timeout.C:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for assignment")
}
}
timeout.Stop()
require.Equal(t, 1, len(assigns))
require.Equal(t, 1, assigns[0])

Expand Down Expand Up @@ -409,7 +401,6 @@ func TestAssignerPreferred(t *testing.T) {

var assignNum int
var assigns []int
timeout := time.NewTimer(3 * time.Second)
open := true
for open {
select {
Expand All @@ -419,11 +410,10 @@ func TestAssignerPreferred(t *testing.T) {
}
t.Log("Publisher", peer2IDStr, "assigned to indexer", assignNum)
assigns = append(assigns, assignNum)
case <-timeout.C:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for assignment")
}
}
timeout.Stop()
require.Equal(t, 1, len(assigns))
require.Equal(t, 1, assigns[0], "expected assignment to indexer 1")

Expand All @@ -442,7 +432,6 @@ func TestAssignerPreferred(t *testing.T) {
require.NoError(t, err)

assigns = assigns[:0]
timeout.Reset(3 * time.Second)
open = true
for open {
select {
Expand All @@ -452,11 +441,10 @@ func TestAssignerPreferred(t *testing.T) {
}
assigns = append(assigns, assignNum)
t.Log("Publisher", peer3IDStr, "assigned to indexer", assignNum)
case <-timeout.C:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for assignment")
}
}
timeout.Stop()
require.Equal(t, 1, len(assigns))
require.Equal(t, 1, assigns[0], "expected assignment to indexer 1")

Expand Down Expand Up @@ -551,11 +539,10 @@ func TestPoolIndexerOffline(t *testing.T) {
err = assigner.Announce(ctx, adCid, addrInfo)
require.NoError(t, err)

timeout := time.NewTimer(2 * time.Second)
select {
case <-asmtChan:
t.Fatal("shouold not see assignment with offline indexer")
case <-timeout.C:
case <-time.After(2 * time.Second):
}
require.False(t, assigner.InitDone())

Expand All @@ -566,13 +553,11 @@ func TestPoolIndexerOffline(t *testing.T) {
err = assigner.Announce(ctx, adCid, addrInfo)
require.NoError(t, err)

timeout.Reset(2 * time.Second)
select {
case <-asmtChan:
case <-timeout.C:
case <-time.After(2 * time.Second):
t.Fatal("timed out waiting for assignment")
}
timeout.Stop()

require.True(t, assigner.InitDone())

Expand Down Expand Up @@ -717,7 +702,6 @@ func TestFreezeHandoff(t *testing.T) {

var assignNum int
var assigns []int
timeout := time.NewTimer(3 * time.Second)
open := true
for open {
select {
Expand All @@ -727,11 +711,10 @@ func TestFreezeHandoff(t *testing.T) {
}
t.Log("Publisher", peer3IDStr, "assigned to indexer", assignNum)
assigns = append(assigns, assignNum)
case <-timeout.C:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for assignment")
}
}
timeout.Stop()
require.Equal(t, 1, len(assigns))
require.Equal(t, 0, assigns[0])

Expand All @@ -744,7 +727,6 @@ func TestFreezeHandoff(t *testing.T) {
assigner.PollNow()
var handoffCount int

timeout.Reset(5 * time.Second)
for handoffCount < 2 {
select {
case handoff := <-handoffChan:
Expand All @@ -753,16 +735,15 @@ func TestFreezeHandoff(t *testing.T) {
pubID := <-handoffPubs
require.True(t, peer1IDStr == pubID || peer3IDStr == pubID)
handoffCount++
case <-timeout.C:
case <-time.After(5 * time.Second):
t.Fatal("timed out waiting for handoff")
}
}

timeout.Reset(time.Second)
select {
case <-handoffChan:
t.Fatal("should not have another")
case <-timeout.C:
case <-time.After(time.Second):
}

require.NoError(t, assigner.Close())
Expand Down Expand Up @@ -803,12 +784,10 @@ func TestAssignerNoIndexers(t *testing.T) {
require.NoError(t, err)

// Check that channel is closed without receiving anything.
timeout := time.NewTimer(3 * time.Second)
defer timeout.Stop()
select {
case _, open := <-asmtChan:
require.False(t, open)
case <-timeout.C:
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for assignment")
}
}
Expand Down
11 changes: 3 additions & 8 deletions internal/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,16 +401,14 @@ func TestPollProvider(t *testing.T) {

// Check for auto-sync after pollInterval 0.
r.pollProviders(poll, nil, 1)
timeout := time.NewTimer(time.Second)
select {
case pinfo := <-r.SyncChan():
require.Equal(t, pinfo.AddrInfo.ID, peerID, "Wrong provider ID")
require.Equal(t, pinfo.Publisher, pubID, "Wrong publisher ID")
require.False(t, pinfo.Inactive(), "Expected provider not to be marked inactive")
case <-timeout.C:
case <-time.After(time.Second):
t.Fatal("Expected sync channel to be written")
}
timeout.Stop()

// Check that registry is not blocked by unread auto-sync channel.
poll.retryAfter = 0
Expand All @@ -424,10 +422,9 @@ func TestPollProvider(t *testing.T) {
close(done)
}()

timeout.Reset(2 * time.Second)
select {
case <-done:
case <-timeout.C:
case <-time.After(2 * time.Second):
t.Fatal("actions channel blocked")
}
select {
Expand All @@ -437,7 +434,6 @@ func TestPollProvider(t *testing.T) {
case <-timeout.C:

Check failure on line 434 in internal/registry/registry_test.go

View workflow job for this annotation

GitHub Actions / go-check / All

undefined: timeout (compile)

Check failure on line 434 in internal/registry/registry_test.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go this)

undefined: timeout

Check failure on line 434 in internal/registry/registry_test.go

View workflow job for this annotation

GitHub Actions / go-test / ubuntu (go next)

undefined: timeout

Check failure on line 434 in internal/registry/registry_test.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go this)

undefined: timeout

Check failure on line 434 in internal/registry/registry_test.go

View workflow job for this annotation

GitHub Actions / go-test / windows (go next)

undefined: timeout

Check failure on line 434 in internal/registry/registry_test.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go this)

undefined: timeout

Check failure on line 434 in internal/registry/registry_test.go

View workflow job for this annotation

GitHub Actions / go-test / macos (go next)

undefined: timeout
t.Fatal("Expected sync channel to be written")
}
timeout.Stop()

// Inactive provider should not be returned.
pinfo, _ := r.ProviderInfo(peerID)
Expand Down Expand Up @@ -520,12 +516,11 @@ func TestPollProviderOverrides(t *testing.T) {

// Check for auto-sync after pollInterval 0.
r.pollProviders(poll, overrides, 1)
timeout := time.After(2 * time.Second)
select {
case pinfo := <-r.SyncChan():
require.Equal(t, pinfo.AddrInfo.ID, peerID, "Wrong provider ID")
require.Equal(t, pinfo.Publisher, pubID, "Wrong publisher ID")
case <-timeout:
case <-time.After(2 * time.Second):
t.Fatal("Expected sync channel to be written")
}

Expand Down

0 comments on commit 039e8cd

Please sign in to comment.