Commit aa64f771 authored by Łukasz Magiera's avatar Łukasz Magiera

coreapi: make sure to cancel context in tests

License: MIT
Signed-off-by: default avatarŁukasz Magiera <magik6k@gmail.com>
parent 1532d260
......@@ -3,6 +3,7 @@ package tests
import (
"context"
"testing"
"time"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
)
......@@ -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)
......@@ -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)
}
})
}
}
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......
......@@ -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)
......@@ -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)
......@@ -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)
......
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......@@ -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)
......
......@@ -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 {
......@@ -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)
......@@ -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)
......
......@@ -28,7 +28,8 @@ func (tp *provider) TestObject(t *testing.T) {
}
func (tp *provider) TestNew(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)
......@@ -54,7 +55,8 @@ func (tp *provider) TestNew(t *testing.T) {
}
func (tp *provider) TestObjectPut(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)
......@@ -94,7 +96,8 @@ func (tp *provider) TestObjectPut(t *testing.T) {
}
func (tp *provider) TestObjectGet(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)
......@@ -116,7 +119,8 @@ func (tp *provider) TestObjectGet(t *testing.T) {
}
func (tp *provider) TestObjectData(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)
......@@ -143,7 +147,8 @@ func (tp *provider) TestObjectData(t *testing.T) {
}
func (tp *provider) TestObjectLinks(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)
......@@ -178,7 +183,8 @@ func (tp *provider) TestObjectLinks(t *testing.T) {
}
func (tp *provider) TestObjectStat(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)
......@@ -225,7 +231,8 @@ func (tp *provider) TestObjectStat(t *testing.T) {
}
func (tp *provider) TestObjectAddLink(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)
......@@ -265,7 +272,8 @@ func (tp *provider) TestObjectAddLink(t *testing.T) {
}
func (tp *provider) TestObjectAddLinkCreate(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)
......@@ -313,7 +321,8 @@ func (tp *provider) TestObjectAddLinkCreate(t *testing.T) {
}
func (tp *provider) TestObjectRmLink(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)
......@@ -345,7 +354,8 @@ func (tp *provider) TestObjectRmLink(t *testing.T) {
}
func (tp *provider) TestObjectAddData(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)
......@@ -374,7 +384,8 @@ func (tp *provider) TestObjectAddData(t *testing.T) {
}
func (tp *provider) TestObjectSetData(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)
......@@ -403,7 +414,8 @@ func (tp *provider) TestObjectSetData(t *testing.T) {
}
func (tp *provider) TestDiffTest(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)
......
......@@ -18,7 +18,8 @@ func (tp *provider) TestPath(t *testing.T) {
}
func (tp *provider) TestMutablePath(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)
......@@ -45,7 +46,8 @@ func (tp *provider) TestMutablePath(t *testing.T) {
}
func (tp *provider) TestPathRemainder(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)
......@@ -72,7 +74,8 @@ func (tp *provider) TestPathRemainder(t *testing.T) {
}
func (tp *provider) TestEmptyPathRemainder(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)
......@@ -103,7 +106,8 @@ func (tp *provider) TestEmptyPathRemainder(t *testing.T) {
}
func (tp *provider) TestInvalidPathRemainder(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)
......@@ -126,7 +130,8 @@ func (tp *provider) TestInvalidPathRemainder(t *testing.T) {
}
func (tp *provider) TestPathRoot(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)
......
......@@ -15,7 +15,8 @@ func (tp *provider) TestPin(t *testing.T) {
}
func (tp *provider) TestPinAdd(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)
......@@ -33,7 +34,8 @@ func (tp *provider) TestPinAdd(t *testing.T) {
}
func (tp *provider) TestPinSimple(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)
......@@ -82,7 +84,8 @@ func (tp *provider) TestPinSimple(t *testing.T) {
}
func (tp *provider) TestPinRecursive(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)
......
......@@ -79,7 +79,8 @@ func wrapped(name string) func(f files.Node) files.Node {
}
func (tp *provider) TestAdd(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)
......@@ -536,7 +537,8 @@ func (tp *provider) TestAdd(t *testing.T) {
}
func (tp *provider) TestAddPinned(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)
......@@ -558,7 +560,8 @@ func (tp *provider) TestAddPinned(t *testing.T) {
}
func (tp *provider) TestAddHashOnly(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)
......@@ -583,7 +586,8 @@ func (tp *provider) TestAddHashOnly(t *testing.T) {
}
func (tp *provider) TestGetEmptyFile(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)
......@@ -615,7 +619,8 @@ func (tp *provider) TestGetEmptyFile(t *testing.T) {
}
func (tp *provider) TestGetDir(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)
......@@ -647,7 +652,8 @@ func (tp *provider) TestGetDir(t *testing.T) {
}
func (tp *provider) TestGetNonUnixfs(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)
......@@ -666,7 +672,8 @@ func (tp *provider) TestGetNonUnixfs(t *testing.T) {
}
func (tp *provider) TestLs(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)
......@@ -702,7 +709,8 @@ func (tp *provider) TestLs(t *testing.T) {
}
func (tp *provider) TestEntriesExpired(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)
......@@ -718,7 +726,7 @@ func (tp *provider) TestEntriesExpired(t *testing.T) {
t.Error(err)
}
ctx, cancel := context.WithCancel(ctx)
ctx, cancel = context.WithCancel(ctx)
nd, err := api.Unixfs().Get(ctx, p)
if err != nil {
......@@ -745,7 +753,8 @@ func (tp *provider) TestEntriesExpired(t *testing.T) {
}
func (tp *provider) TestLsEmptyDir(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)
......@@ -773,7 +782,8 @@ func (tp *provider) TestLsEmptyDir(t *testing.T) {
// TODO(lgierth) this should test properly, with len(links) > 0
func (tp *provider) TestLsNonUnixfs(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)
......@@ -830,7 +840,8 @@ func (f *closeTestF) Close() error {
}
func (tp *provider) TestAddCloses(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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment