Skip to content

Commit

Permalink
Simplify usage of kadtest.CtxShort
Browse files Browse the repository at this point in the history
  • Loading branch information
iand committed Sep 8, 2023
1 parent 54f20b5 commit 722e958
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 33 deletions.
12 changes: 4 additions & 8 deletions v2/coord/coordinator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ func TestConfigValidate(t *testing.T) {
}

func TestExhaustiveQuery(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down Expand Up @@ -198,8 +197,7 @@ func TestExhaustiveQuery(t *testing.T) {
}

func TestRoutingUpdatedEventEmittedForCloserNodes(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down Expand Up @@ -261,8 +259,7 @@ func TestRoutingUpdatedEventEmittedForCloserNodes(t *testing.T) {
}

func TestBootstrap(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down Expand Up @@ -315,8 +312,7 @@ func TestBootstrap(t *testing.T) {
}

func TestIncludeNode(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down
3 changes: 1 addition & 2 deletions v2/coord/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import (

// TODO: this is just a basic is-it-working test that needs to be improved
func TestGetClosestNodes(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down
21 changes: 7 additions & 14 deletions v2/coord/routing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ import (
)

func TestRoutingStartBootstrapSendsEvent(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down Expand Up @@ -62,8 +61,7 @@ func TestRoutingStartBootstrapSendsEvent(t *testing.T) {
}

func TestRoutingBootstrapGetClosestNodesSuccess(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down Expand Up @@ -96,8 +94,7 @@ func TestRoutingBootstrapGetClosestNodesSuccess(t *testing.T) {
}

func TestRoutingBootstrapGetClosestNodesFailure(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down Expand Up @@ -131,8 +128,7 @@ func TestRoutingBootstrapGetClosestNodesFailure(t *testing.T) {
}

func TestRoutingAddNodeInfoSendsEvent(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down Expand Up @@ -162,8 +158,7 @@ func TestRoutingAddNodeInfoSendsEvent(t *testing.T) {
}

func TestRoutingIncludeGetClosestNodesSuccess(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down Expand Up @@ -197,8 +192,7 @@ func TestRoutingIncludeGetClosestNodesSuccess(t *testing.T) {
}

func TestRoutingIncludeGetClosestNodesFailure(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down Expand Up @@ -233,8 +227,7 @@ func TestRoutingIncludeGetClosestNodesFailure(t *testing.T) {
}

func TestRoutingIncludedNodeAddToProbeList(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

clk := clock.NewMock()
_, nodes, err := nettest.LinearTopology(4, clk)
Expand Down
3 changes: 1 addition & 2 deletions v2/dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func expectEventType(t *testing.T, ctx context.Context, events <-chan coord.Rout
}

func TestAddAddresses(t *testing.T) {
ctx, cancel := kadtest.CtxShort(t)
defer cancel()
ctx := kadtest.CtxShort(t)

localCfg := DefaultConfig()

Expand Down
15 changes: 8 additions & 7 deletions v2/internal/kadtest/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"time"
)

// CtxShort returns a Context and a CancelFunc. The context will be
// cancelled after 10 seconds or just before the test binary deadline (as
// specified by the -timeout flag when running the test), whichever is
// sooner. The CancelFunc may be called to cancel the context earlier than
// the deadline.
func CtxShort(t *testing.T) (context.Context, context.CancelFunc) {
// CtxShort returns a Context for tests that are expected to complete quickly.
// The context will be cancelled after 10 seconds or just before the test
// binary deadline (as specified by the -timeout flag when running the test), whichever
// is sooner.
func CtxShort(t *testing.T) context.Context {
t.Helper()

timeout := 10 * time.Second
Expand All @@ -27,5 +26,7 @@ func CtxShort(t *testing.T) (context.Context, context.CancelFunc) {
}
}

return context.WithDeadline(context.Background(), deadline)
ctx, cancel := context.WithDeadline(context.Background(), deadline)
t.Cleanup(cancel)
return ctx
}

0 comments on commit 722e958

Please sign in to comment.