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
|
|
|
)
|
|
|
|
|
2018-07-17 11:35:23 +00:00
|
|
|
func TestImporter(t *testing.T) {
|
2018-07-19 14:27:23 +00:00
|
|
|
sth := test.NewShardingTestHelper()
|
|
|
|
defer sth.Clean()
|
|
|
|
|
|
|
|
f := sth.GetTreeSerialFile(t)
|
2018-08-06 10:44:44 +00:00
|
|
|
p := api.DefaultAddParams()
|
2018-07-17 11:35:23 +00:00
|
|
|
|
|
|
|
imp, err := NewImporter(f, p)
|
2018-02-09 22:20:44 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-19 13:17:27 +00:00
|
|
|
expectedCids := test.ShardingDirCids[:]
|
2018-07-17 11:35:23 +00:00
|
|
|
resultCids := make(map[string]struct{})
|
2018-02-15 00:02:55 +00:00
|
|
|
|
2018-07-17 11:35:23 +00:00
|
|
|
blockHandler := func(ctx context.Context, n *api.NodeWithMeta) (string, error) {
|
|
|
|
resultCids[n.Cid] = struct{}{}
|
|
|
|
return n.Cid, nil
|
2018-02-15 00:02:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 11:35:23 +00:00
|
|
|
_, err = imp.Run(context.Background(), blockHandler)
|
2018-04-20 15:41:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2018-07-17 11:35:23 +00:00
|
|
|
// for i, c := range expectedCids {
|
|
|
|
// fmt.Printf("%d: %s\n", i, c)
|
|
|
|
// }
|
2018-04-20 15:41:15 +00:00
|
|
|
|
2018-07-17 11:35:23 +00:00
|
|
|
// for c := range resultCids {
|
|
|
|
// fmt.Printf("%s\n", c)
|
|
|
|
// }
|
2018-05-02 02:34:09 +00:00
|
|
|
|
2018-07-17 11:35:23 +00:00
|
|
|
if len(expectedCids) != len(resultCids) {
|
|
|
|
t.Fatal("unexpected number of blocks imported")
|
2018-05-02 02:34:09 +00:00
|
|
|
}
|
2018-04-20 15:41:15 +00:00
|
|
|
|
2018-07-17 11:35:23 +00:00
|
|
|
for _, c := range expectedCids {
|
|
|
|
_, ok := resultCids[c]
|
2018-05-02 02:34:09 +00:00
|
|
|
if !ok {
|
2018-07-17 11:35:23 +00:00
|
|
|
t.Fatal("unexpected block emitted:", c)
|
2018-04-20 15:41:15 +00:00
|
|
|
}
|
2018-07-17 11:35:23 +00:00
|
|
|
|
2018-05-02 02:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-04-20 15:41:15 +00:00
|
|
|
|
2018-07-17 11:35:23 +00:00
|
|
|
func TestImporter_DoubleStart(t *testing.T) {
|
2018-07-19 14:27:23 +00:00
|
|
|
sth := test.NewShardingTestHelper()
|
|
|
|
defer sth.Clean()
|
|
|
|
|
|
|
|
f := sth.GetTreeSerialFile(t)
|
2018-08-06 10:44:44 +00:00
|
|
|
p := api.DefaultAddParams()
|
2018-07-17 11:35:23 +00:00
|
|
|
|
|
|
|
imp, err := NewImporter(f, p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-04-20 15:41:15 +00:00
|
|
|
}
|
2018-07-17 11:35:23 +00:00
|
|
|
|
|
|
|
blockHandler := func(ctx context.Context, n *api.NodeWithMeta) (string, error) {
|
|
|
|
return "", nil
|
2018-04-20 15:41:15 +00:00
|
|
|
}
|
2018-07-17 11:35:23 +00:00
|
|
|
|
|
|
|
_, err = imp.Run(context.Background(), blockHandler)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2018-04-20 15:41:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-17 11:35:23 +00:00
|
|
|
_, err = imp.Run(context.Background(), blockHandler)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected an error: cannot run importer twice")
|
2018-02-15 00:02:55 +00:00
|
|
|
}
|
2018-02-09 22:20:44 +00:00
|
|
|
}
|