add_test.go 3.54 KB
Newer Older
1 2 3
package coreunix

import (
Jeromy's avatar
Jeromy committed
4 5 6
	"bytes"
	"io"
	"io/ioutil"
7
	"testing"
Jeromy's avatar
Jeromy committed
8
	"time"
9

Jeromy's avatar
Jeromy committed
10
	"github.com/ipfs/go-ipfs/commands/files"
11
	"github.com/ipfs/go-ipfs/core"
Jeromy's avatar
Jeromy committed
12 13
	dag "github.com/ipfs/go-ipfs/merkledag"
	"github.com/ipfs/go-ipfs/pin/gc"
14 15
	"github.com/ipfs/go-ipfs/repo"
	"github.com/ipfs/go-ipfs/repo/config"
16
	"github.com/ipfs/go-ipfs/thirdparty/testutil"
Jeromy's avatar
Jeromy committed
17

18
	"context"
19
	cid "gx/ipfs/QmXUuRadqDq5BuFWzVU6VuKaSjTcNm1gNCtLvvP1TJCW4z/go-cid"
20 21 22 23 24 25 26 27 28 29 30
)

func TestAddRecursive(t *testing.T) {
	r := &repo.Mock{
		C: config.Config{
			Identity: config.Identity{
				PeerID: "Qmfoo", // required by offline node
			},
		},
		D: testutil.ThreadSafeCloserMapDatastore(),
	}
31
	node, err := core.NewNode(context.Background(), &core.BuildCfg{Repo: r})
32 33 34
	if err != nil {
		t.Fatal(err)
	}
Jeromy's avatar
Jeromy committed
35
	if k, err := AddR(node, "test_data"); err != nil {
36 37
		t.Fatal(err)
	} else if k != "QmWCCga8AbTyfAQ7pTnGT6JgmRMAB3Qp8ZmTEFi5q5o8jC" {
Jeromy's avatar
Jeromy committed
38
		t.Fatal("keys do not match: ", k)
39 40
	}
}
Jeromy's avatar
Jeromy committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

func TestAddGCLive(t *testing.T) {
	r := &repo.Mock{
		C: config.Config{
			Identity: config.Identity{
				PeerID: "Qmfoo", // required by offline node
			},
		},
		D: testutil.ThreadSafeCloserMapDatastore(),
	}
	node, err := core.NewNode(context.Background(), &core.BuildCfg{Repo: r})
	if err != nil {
		t.Fatal(err)
	}

	errs := make(chan error)
	out := make(chan interface{})
58
	adder, err := NewAdder(context.Background(), node.Pinning, node.Blockstore, node.DAG)
Jeromy's avatar
Jeromy committed
59 60 61
	if err != nil {
		t.Fatal(err)
	}
62
	adder.Out = out
Jeromy's avatar
Jeromy committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

	dataa := ioutil.NopCloser(bytes.NewBufferString("testfileA"))
	rfa := files.NewReaderFile("a", "a", dataa, nil)

	// make two files with pipes so we can 'pause' the add for timing of the test
	piper, pipew := io.Pipe()
	hangfile := files.NewReaderFile("b", "b", piper, nil)

	datad := ioutil.NopCloser(bytes.NewBufferString("testfileD"))
	rfd := files.NewReaderFile("d", "d", datad, nil)

	slf := files.NewSliceFile("files", "files", []files.File{rfa, hangfile, rfd})

	addDone := make(chan struct{})
	go func() {
		defer close(addDone)
		defer close(out)
		err := adder.AddFile(slf)

		if err != nil {
			t.Fatal(err)
		}

	}()

	addedHashes := make(map[string]struct{})
	select {
	case o := <-out:
		addedHashes[o.(*AddedObject).Hash] = struct{}{}
	case <-addDone:
		t.Fatal("add shouldnt complete yet")
	}

96
	var gcout <-chan *cid.Cid
Jeromy's avatar
Jeromy committed
97 98 99
	gcstarted := make(chan struct{})
	go func() {
		defer close(gcstarted)
100
		gcchan, err := gc.GC(context.Background(), node.Blockstore, node.DAG, node.Pinning, nil)
Jeromy's avatar
Jeromy committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
		if err != nil {
			log.Error("GC ERROR:", err)
			errs <- err
			return
		}

		gcout = gcchan
	}()

	// gc shouldnt start until we let the add finish its current file.
	pipew.Write([]byte("some data for file b"))

	select {
	case <-gcstarted:
		t.Fatal("gc shouldnt have started yet")
	case err := <-errs:
		t.Fatal(err)
	default:
	}

	time.Sleep(time.Millisecond * 100) // make sure gc gets to requesting lock

	// finish write and unblock gc
	pipew.Close()

	// receive next object from adder
	select {
	case o := <-out:
		addedHashes[o.(*AddedObject).Hash] = struct{}{}
	case err := <-errs:
		t.Fatal(err)
	}

	select {
	case <-gcstarted:
	case err := <-errs:
		t.Fatal(err)
	}

	for k := range gcout {
141
		if _, ok := addedHashes[k.String()]; ok {
Jeromy's avatar
Jeromy committed
142 143 144 145
			t.Fatal("gc'ed a hash we just added")
		}
	}

Jeromy's avatar
Jeromy committed
146
	var last *cid.Cid
Jeromy's avatar
Jeromy committed
147 148
	for a := range out {
		// wait for it to finish
Jeromy's avatar
Jeromy committed
149 150 151 152 153
		c, err := cid.Decode(a.(*AddedObject).Hash)
		if err != nil {
			t.Fatal(err)
		}
		last = c
Jeromy's avatar
Jeromy committed
154 155 156 157 158
	}

	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
	defer cancel()

Jeromy's avatar
Jeromy committed
159
	set := cid.NewSet()
160
	err = dag.EnumerateChildren(ctx, node.DAG, last, set.Visit, false)
Jeromy's avatar
Jeromy committed
161 162 163 164
	if err != nil {
		t.Fatal(err)
	}
}