2018-02-09 22:20:44 +00:00
|
|
|
package importer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
2018-04-20 22:57:13 +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-04-22 01:02:13 +00:00
|
|
|
false, 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() { // listen for errors
|
|
|
|
for {
|
|
|
|
err, ok := <-errChan
|
|
|
|
if !ok {
|
|
|
|
// channel closed, safe to exit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
2018-02-15 00:02:55 +00:00
|
|
|
|
|
|
|
check := make(map[string]struct{})
|
2018-03-01 00:19:51 +00:00
|
|
|
for obj := range outChan {
|
|
|
|
cid := obj.Cid
|
2018-02-15 00:02:55 +00:00
|
|
|
if _, ok := check[cid]; ok {
|
|
|
|
t.Fatalf("Duplicate cid %s", cid)
|
|
|
|
}
|
|
|
|
check[cid] = struct{}{}
|
|
|
|
}
|
2018-04-22 01:02:13 +00:00
|
|
|
if len(check) != len(test.TestDirCids) {
|
|
|
|
t.Fatalf("Witnessed cids: %v\nExpected cids: %v", check, test.TestDirCids)
|
2018-02-15 00:02:55 +00:00
|
|
|
}
|
2018-04-22 01:02:13 +00:00
|
|
|
cidsS := test.TestDirCids[:]
|
2018-02-15 00:02:55 +00:00
|
|
|
for cid := range check {
|
|
|
|
if !contains(cidsS, cid) {
|
|
|
|
t.Fatalf("Unexpected cid: %s", cid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-22 01:02:13 +00:00
|
|
|
false, false, false, false, "")
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
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 {
|
|
|
|
cid := obj.Hash
|
|
|
|
if _, ok := check[cid]; ok {
|
|
|
|
t.Fatalf("Duplicate cid %s", cid)
|
|
|
|
}
|
|
|
|
check[cid] = struct{}{}
|
|
|
|
}
|
2018-04-22 01:02:13 +00:00
|
|
|
if len(check) != len(test.TestDirCids[:15]) {
|
|
|
|
t.Fatalf("Witnessed cids: %v\nExpected cids: %v", check, test.TestDirCids[:15])
|
2018-04-20 15:41:15 +00:00
|
|
|
}
|
2018-04-22 01:02:13 +00:00
|
|
|
cidsS := test.TestDirCids[:15]
|
2018-04-20 15:41:15 +00:00
|
|
|
for cid := range check {
|
|
|
|
if !contains(cidsS, cid) {
|
|
|
|
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
|
|
|
}
|