ipfs-cluster/importer/import_test.go
Wyatt Daviau 1adc409aa7 refactor import_test
License: MIT
Signed-off-by: Wyatt Daviau <wdaviau@cs.stanford.edu>
2018-08-07 20:11:24 +02:00

117 lines
2.3 KiB
Go

package importer
import (
"context"
"testing"
"github.com/ipfs/ipfs-cluster/test"
"github.com/ipfs/ipfs-cluster/api"
)
// import and receive all blocks
func TestToChannelOutput(t *testing.T) {
file, err := test.GetTestingDirSerial()
if err != nil {
t.Fatal(err)
}
printChan, outChan, errChan := ToChannel(context.Background(), file,
false, false, false, false, "")
go func() { // listen on printChan so progress can be made
for {
_, ok := <-printChan
if !ok {
// channel closed, safe to exit
return
}
}
}()
go listenErrCh(t, errChan)
objs := make([]interface{}, 0)
for obj := range outChan {
objs = append(objs, obj)
}
testChannelOutput(t, objs, test.TestDirCids[:])
}
func TestToChannelPrint(t *testing.T) {
file, err := test.GetTestingDirSerial()
if err != nil {
t.Fatal(err)
}
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
if !ok {
// channel closed, safe to exit
return
}
}
}()
objs := make([]interface{}, 0)
for obj := range printChan {
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 {
t.Fatalf("Duplicate cid %s", cid)
}
check[cid] = struct{}{}
}
if len(check) != len(expected) {
t.Fatalf("Witnessed cids: %v\nExpected cids: %v", check, test.TestDirCids[:15])
}
for cid := range check {
if !contains(expected, cid) {
t.Fatalf("Unexpected cid: %s", cid)
}
}
}
func contains(slice []string, s string) bool {
for _, a := range slice {
if a == s {
return true
}
}
return false
}