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
$(MAKE) -C ipfs-cluster-service clean
$(MAKE) -C ipfs-cluster-ctl clean
@rm -rf ./test/testingData
gx-clean: clean
@rm -f $(deptools)/*

View File

@ -5,6 +5,7 @@ import (
"testing"
"github.com/ipfs/ipfs-cluster/test"
"github.com/ipfs/ipfs-cluster/api"
)
// import and receive all blocks
@ -27,34 +28,13 @@ func TestToChannelOutput(t *testing.T) {
}
}()
go func() { // listen for errors
for {
err, ok := <-errChan
if !ok {
// channel closed, safe to exit
return
}
t.Fatal(err)
}
}()
go listenErrCh(t, errChan)
check := make(map[string]struct{})
objs := make([]interface{}, 0)
for obj := range outChan {
cid := obj.Cid
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)
}
objs = append(objs, obj)
}
testChannelOutput(t, objs, test.TestDirCids[:])
}
func TestToChannelPrint(t *testing.T) {
@ -66,6 +46,8 @@ func TestToChannelPrint(t *testing.T) {
printChan, outChan, errChan := ToChannel(context.Background(), file,
false, false, false, false, "")
go listenErrCh(t, errChan)
go func() { // listen on outChan so progress can be made
for {
_, ok := <-outChan
@ -75,8 +57,16 @@ func TestToChannelPrint(t *testing.T) {
}
}
}()
objs := make([]interface{}, 0)
for obj := range printChan {
objs = append(objs, obj)
}
testChannelOutput(t, objs, test.TestDirCids[:15])
}
go func() { // listen for errors
// 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 {
@ -85,22 +75,32 @@ func TestToChannelPrint(t *testing.T) {
}
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 printChan {
cid := obj.Hash
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 {
t.Fatalf("Duplicate cid %s", cid)
}
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])
}
cidsS := test.TestDirCids[:15]
for cid := range check {
if !contains(cidsS, cid) {
if !contains(expected, cid) {
t.Fatalf("Unexpected cid: %s", cid)
}
}