refactor import_test

License: MIT
Signed-off-by: Wyatt Daviau <wdaviau@cs.stanford.edu>
This commit is contained in:
Wyatt Daviau 2018-05-01 22:34:09 -04:00 committed by Hector Sanjuan
parent 82facd3629
commit 1adc409aa7
2 changed files with 43 additions and 42 deletions

View File

@ -19,6 +19,7 @@ all: service ctl
clean: rwundo clean_sharness clean: rwundo clean_sharness
$(MAKE) -C ipfs-cluster-service clean $(MAKE) -C ipfs-cluster-service clean
$(MAKE) -C ipfs-cluster-ctl clean $(MAKE) -C ipfs-cluster-ctl clean
@rm -rf ./test/testingData
gx-clean: clean gx-clean: clean
@rm -f $(deptools)/* @rm -f $(deptools)/*

View File

@ -5,6 +5,7 @@ import (
"testing" "testing"
"github.com/ipfs/ipfs-cluster/test" "github.com/ipfs/ipfs-cluster/test"
"github.com/ipfs/ipfs-cluster/api"
) )
// import and receive all blocks // import and receive all blocks
@ -27,34 +28,13 @@ func TestToChannelOutput(t *testing.T) {
} }
}() }()
go func() { // listen for errors go listenErrCh(t, errChan)
for {
err, ok := <-errChan
if !ok {
// channel closed, safe to exit
return
}
t.Fatal(err)
}
}()
check := make(map[string]struct{}) objs := make([]interface{}, 0)
for obj := range outChan { for obj := range outChan {
cid := obj.Cid objs = append(objs, obj)
if _, ok := check[cid]; ok {
t.Fatalf("Duplicate cid %s", cid)
}
check[cid] = struct{}{}
}
if len(check) != len(test.TestDirCids) {
t.Fatalf("Witnessed cids: %v\nExpected cids: %v", check, test.TestDirCids)
}
cidsS := test.TestDirCids[:]
for cid := range check {
if !contains(cidsS, cid) {
t.Fatalf("Unexpected cid: %s", cid)
}
} }
testChannelOutput(t, objs, test.TestDirCids[:])
} }
func TestToChannelPrint(t *testing.T) { func TestToChannelPrint(t *testing.T) {
@ -66,6 +46,8 @@ func TestToChannelPrint(t *testing.T) {
printChan, outChan, errChan := ToChannel(context.Background(), file, printChan, outChan, errChan := ToChannel(context.Background(), file,
false, false, false, false, "") false, false, false, false, "")
go listenErrCh(t, errChan)
go func() { // listen on outChan so progress can be made go func() { // listen on outChan so progress can be made
for { for {
_, ok := <-outChan _, ok := <-outChan
@ -75,32 +57,50 @@ func TestToChannelPrint(t *testing.T) {
} }
} }
}() }()
objs := make([]interface{}, 0)
go func() { // listen for errors
for {
err, ok := <-errChan
if !ok {
// channel closed, safe to exit
return
}
t.Fatal(err)
}
}()
check := make(map[string]struct{})
for obj := range printChan { for obj := range printChan {
cid := obj.Hash objs = append(objs, obj)
}
testChannelOutput(t, objs, test.TestDirCids[:15])
}
// listenErrCh listens on the error channel until closed and raise any errors
// that show up
func listenErrCh(t *testing.T, errChan <-chan error) {
for {
err, ok := <-errChan
if !ok {
// channel closed, safe to exit
return
}
t.Fatal(err)
}
}
// testChannelOutput is a utility for shared functionality of output and print
// channel testing
func testChannelOutput(t *testing.T, objs []interface{}, expected []string) {
check := make(map[string]struct{})
for _, obj := range objs {
var cid string
switch obj := obj.(type){
case *api.AddedOutput:
cid = obj.Hash
case *api.NodeWithMeta:
cid = obj.Cid
}
if _, ok := check[cid]; ok { if _, ok := check[cid]; ok {
t.Fatalf("Duplicate cid %s", cid) t.Fatalf("Duplicate cid %s", cid)
} }
check[cid] = struct{}{} check[cid] = struct{}{}
} }
if len(check) != len(test.TestDirCids[:15]) { if len(check) != len(expected) {
t.Fatalf("Witnessed cids: %v\nExpected cids: %v", check, test.TestDirCids[:15]) t.Fatalf("Witnessed cids: %v\nExpected cids: %v", check, test.TestDirCids[:15])
} }
cidsS := test.TestDirCids[:15]
for cid := range check { for cid := range check {
if !contains(cidsS, cid) { if !contains(expected, cid) {
t.Fatalf("Unexpected cid: %s", cid) t.Fatalf("Unexpected cid: %s", cid)
} }
} }