2018-07-04 16:30:24 +00:00
|
|
|
package adder
|
2018-02-09 22:20:44 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2018-05-02 02:34:09 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
2018-07-04 16:30:24 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/test"
|
2018-02-09 22:20:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// import and receive all blocks
|
2018-04-20 15:41:15 +00:00
|
|
|
func TestToChannelOutput(t *testing.T) {
|
2018-04-20 22:57:13 +00:00
|
|
|
file, err := test.GetTestingDirSerial()
|
2018-02-09 22:20:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-03-01 00:19:51 +00:00
|
|
|
printChan, outChan, errChan := ToChannel(context.Background(), file,
|
2018-05-02 03:08:22 +00:00
|
|
|
false, false, false, "")
|
2018-03-01 00:19:51 +00:00
|
|
|
|
|
|
|
go func() { // listen on printChan so progress can be made
|
|
|
|
for {
|
2018-03-12 18:13:05 +00:00
|
|
|
_, ok := <-printChan
|
2018-03-01 00:19:51 +00:00
|
|
|
if !ok {
|
|
|
|
// channel closed, safe to exit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-02 02:34:09 +00:00
|
|
|
go listenErrCh(t, errChan)
|
2018-02-15 00:02:55 +00:00
|
|
|
|
2018-05-02 02:34:09 +00:00
|
|
|
objs := make([]interface{}, 0)
|
2018-03-01 00:19:51 +00:00
|
|
|
for obj := range outChan {
|
2018-05-02 02:34:09 +00:00
|
|
|
objs = append(objs, obj)
|
2018-02-15 00:02:55 +00:00
|
|
|
}
|
2018-05-02 02:34:09 +00:00
|
|
|
testChannelOutput(t, objs, test.TestDirCids[:])
|
2018-02-15 00:02:55 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 15:41:15 +00:00
|
|
|
func TestToChannelPrint(t *testing.T) {
|
2018-04-20 22:57:13 +00:00
|
|
|
file, err := test.GetTestingDirSerial()
|
2018-04-20 15:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
printChan, outChan, errChan := ToChannel(context.Background(), file,
|
2018-05-02 03:08:22 +00:00
|
|
|
false, false, false, "")
|
2018-04-20 15:41:15 +00:00
|
|
|
|
2018-05-02 02:34:09 +00:00
|
|
|
go listenErrCh(t, errChan)
|
|
|
|
|
2018-04-20 15:41:15 +00:00
|
|
|
go func() { // listen on outChan so progress can be made
|
|
|
|
for {
|
|
|
|
_, ok := <-outChan
|
|
|
|
if !ok {
|
|
|
|
// channel closed, safe to exit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2018-05-02 02:34:09 +00:00
|
|
|
objs := make([]interface{}, 0)
|
|
|
|
for obj := range printChan {
|
|
|
|
objs = append(objs, obj)
|
|
|
|
}
|
|
|
|
testChannelOutput(t, objs, test.TestDirCids[:15])
|
|
|
|
}
|
2018-04-20 15:41:15 +00:00
|
|
|
|
2018-05-02 02:34:09 +00:00
|
|
|
// 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
|
2018-04-20 15:41:15 +00:00
|
|
|
}
|
2018-05-02 02:34:09 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2018-04-20 15:41:15 +00:00
|
|
|
|
2018-05-02 02:34:09 +00:00
|
|
|
// testChannelOutput is a utility for shared functionality of output and print
|
|
|
|
// channel testing
|
|
|
|
func testChannelOutput(t *testing.T, objs []interface{}, expected []string) {
|
2018-04-20 15:41:15 +00:00
|
|
|
check := make(map[string]struct{})
|
2018-05-02 02:34:09 +00:00
|
|
|
for _, obj := range objs {
|
|
|
|
var cid string
|
2018-07-04 16:30:24 +00:00
|
|
|
switch obj := obj.(type) {
|
2018-05-02 02:34:09 +00:00
|
|
|
case *api.AddedOutput:
|
|
|
|
cid = obj.Hash
|
|
|
|
case *api.NodeWithMeta:
|
|
|
|
cid = obj.Cid
|
|
|
|
}
|
2018-04-20 15:41:15 +00:00
|
|
|
if _, ok := check[cid]; ok {
|
|
|
|
t.Fatalf("Duplicate cid %s", cid)
|
|
|
|
}
|
|
|
|
check[cid] = struct{}{}
|
|
|
|
}
|
2018-05-02 02:34:09 +00:00
|
|
|
if len(check) != len(expected) {
|
2018-04-22 01:02:13 +00:00
|
|
|
t.Fatalf("Witnessed cids: %v\nExpected cids: %v", check, test.TestDirCids[:15])
|
2018-04-20 15:41:15 +00:00
|
|
|
}
|
|
|
|
for cid := range check {
|
2018-05-02 02:34:09 +00:00
|
|
|
if !contains(expected, cid) {
|
2018-04-20 15:41:15 +00:00
|
|
|
t.Fatalf("Unexpected cid: %s", cid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 00:02:55 +00:00
|
|
|
func contains(slice []string, s string) bool {
|
|
|
|
for _, a := range slice {
|
|
|
|
if a == s {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2018-02-09 22:20:44 +00:00
|
|
|
}
|