Skip to content

Commit

Permalink
coreapi: make sure to cancel context in tests
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Dec 20, 2018
1 parent 71d4aca commit 7885a98
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 66 deletions.
37 changes: 36 additions & 1 deletion core/coreapi/interface/tests/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests
import (
"context"
"testing"
"time"

coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
)
Expand All @@ -21,12 +22,37 @@ type Provider interface {
MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error)
}

func (tp *provider) MakeAPISwarm(ctx context.Context, fullIdentity bool, n int) ([]coreiface.CoreAPI, error) {
tp.apis <- 1
go func() {
<-ctx.Done()
tp.apis <- -1
}()

return tp.Provider.MakeAPISwarm(ctx, fullIdentity, n)
}

type provider struct {
Provider

apis chan int
}

func TestApi(p Provider) func(t *testing.T) {
tp := &provider{p}
running := 1
apis := make(chan int)
zeroRunning := make(chan struct{})
go func() {
for i := range apis {
running += i
if running < 1 {
close(zeroRunning)
return
}
}
}()

tp := &provider{Provider: p, apis: apis}

return func(t *testing.T) {
t.Run("Block", tp.TestBlock)
Expand All @@ -39,5 +65,14 @@ func TestApi(p Provider) func(t *testing.T) {
t.Run("Pin", tp.TestPin)
t.Run("PubSub", tp.TestPubSub)
t.Run("Unixfs", tp.TestUnixfs)

apis <- -1
t.Run("TestsCancelCtx", func(t *testing.T) {
select {
case <-zeroRunning:
case <-time.After(time.Second):
t.Errorf("%d node(s) not closed", running)
}
})
}
}
18 changes: 12 additions & 6 deletions core/coreapi/interface/tests/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ func (tp *provider) TestBlock(t *testing.T) {
}

func (tp *provider) TestBlockPut(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand All @@ -39,7 +40,8 @@ func (tp *provider) TestBlockPut(t *testing.T) {
}

func (tp *provider) TestBlockPutFormat(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand All @@ -56,7 +58,8 @@ func (tp *provider) TestBlockPutFormat(t *testing.T) {
}

func (tp *provider) TestBlockPutHash(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand All @@ -73,7 +76,8 @@ func (tp *provider) TestBlockPutHash(t *testing.T) {
}

func (tp *provider) TestBlockGet(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -113,7 +117,8 @@ func (tp *provider) TestBlockGet(t *testing.T) {
}

func (tp *provider) TestBlockRm(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -166,7 +171,8 @@ func (tp *provider) TestBlockRm(t *testing.T) {
}

func (tp *provider) TestBlockStat(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down
15 changes: 10 additions & 5 deletions core/coreapi/interface/tests/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var (
)

func (tp *provider) TestPut(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand All @@ -48,7 +49,8 @@ func (tp *provider) TestPut(t *testing.T) {
}

func (tp *provider) TestPutWithHash(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand All @@ -65,7 +67,8 @@ func (tp *provider) TestPutWithHash(t *testing.T) {
}

func (tp *provider) TestDagPath(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -97,7 +100,8 @@ func (tp *provider) TestDagPath(t *testing.T) {
}

func (tp *provider) TestTree(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -126,7 +130,8 @@ func (tp *provider) TestTree(t *testing.T) {
}

func (tp *provider) TestBatch(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down
9 changes: 6 additions & 3 deletions core/coreapi/interface/tests/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ func (tp *provider) TestDht(t *testing.T) {
}

func (tp *provider) TestDhtFindPeer(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
apis, err := tp.MakeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -51,7 +52,8 @@ func (tp *provider) TestDhtFindPeer(t *testing.T) {
}

func (tp *provider) TestDhtFindProviders(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
apis, err := tp.MakeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -80,7 +82,8 @@ func (tp *provider) TestDhtFindProviders(t *testing.T) {
}

func (tp *provider) TestDhtProvide(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
apis, err := tp.MakeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
Expand Down
48 changes: 32 additions & 16 deletions core/coreapi/interface/tests/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func (tp *provider) TestKey(t *testing.T) {
}

func (tp *provider) TestListSelf(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -61,7 +62,8 @@ func (tp *provider) TestListSelf(t *testing.T) {
}

func (tp *provider) TestRenameSelf(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Fatal(err)
Expand All @@ -88,7 +90,8 @@ func (tp *provider) TestRenameSelf(t *testing.T) {
}

func (tp *provider) TestRemoveSelf(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Fatal(err)
Expand All @@ -106,7 +109,8 @@ func (tp *provider) TestRemoveSelf(t *testing.T) {
}

func (tp *provider) TestGenerate(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand All @@ -128,7 +132,8 @@ func (tp *provider) TestGenerate(t *testing.T) {
}

func (tp *provider) TestGenerateSize(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand All @@ -150,7 +155,8 @@ func (tp *provider) TestGenerateSize(t *testing.T) {
}

func (tp *provider) TestGenerateType(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
t.Skip("disabled until libp2p/specs#111 is fixed")

api, err := tp.makeAPI(ctx)
Expand All @@ -175,7 +181,8 @@ func (tp *provider) TestGenerateType(t *testing.T) {
}

func (tp *provider) TestGenerateExisting(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -207,7 +214,8 @@ func (tp *provider) TestGenerateExisting(t *testing.T) {
}

func (tp *provider) TestList(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -252,7 +260,8 @@ func (tp *provider) TestList(t *testing.T) {
}

func (tp *provider) TestRename(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -280,7 +289,8 @@ func (tp *provider) TestRename(t *testing.T) {
}

func (tp *provider) TestRenameToSelf(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand All @@ -303,7 +313,8 @@ func (tp *provider) TestRenameToSelf(t *testing.T) {
}

func (tp *provider) TestRenameToSelfForce(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand All @@ -326,7 +337,8 @@ func (tp *provider) TestRenameToSelfForce(t *testing.T) {
}

func (tp *provider) TestRenameOverwriteNoForce(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -355,7 +367,8 @@ func (tp *provider) TestRenameOverwriteNoForce(t *testing.T) {
}

func (tp *provider) TestRenameOverwrite(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -393,7 +406,8 @@ func (tp *provider) TestRenameOverwrite(t *testing.T) {
}

func (tp *provider) TestRenameSameNameNoForce(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -421,7 +435,8 @@ func (tp *provider) TestRenameSameNameNoForce(t *testing.T) {
}

func (tp *provider) TestRenameSameName(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -449,7 +464,8 @@ func (tp *provider) TestRenameSameName(t *testing.T) {
}

func (tp *provider) TestRemove(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
api, err := tp.makeAPI(ctx)
if err != nil {
t.Error(err)
Expand Down
9 changes: 6 additions & 3 deletions core/coreapi/interface/tests/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ func appendPath(p coreiface.Path, sub string) coreiface.Path {
}

func (tp *provider) TestPublishResolve(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
init := func() (coreiface.CoreAPI, coreiface.Path) {
apis, err := tp.MakeAPISwarm(ctx, true, 5)
if err != nil {
Expand Down Expand Up @@ -185,7 +186,8 @@ func (tp *provider) TestPublishResolve(t *testing.T) {
}

func (tp *provider) TestBasicPublishResolveKey(t *testing.T) {
ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
apis, err := tp.MakeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -228,7 +230,8 @@ func (tp *provider) TestBasicPublishResolveKey(t *testing.T) {
func (tp *provider) TestBasicPublishResolveTimeout(t *testing.T) {
t.Skip("ValidTime doesn't appear to work at this time resolution")

ctx := context.Background()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
apis, err := tp.MakeAPISwarm(ctx, true, 5)
if err != nil {
t.Fatal(err)
Expand Down
Loading

0 comments on commit 7885a98

Please sign in to comment.