reprovide_test.go 5.64 KB
Newer Older
1 2 3 4 5 6 7
package simple_test

import (
	"context"
	"testing"
	"time"

Steven Allen's avatar
Steven Allen committed
8 9
	bsrv "github.com/ipfs/go-blockservice"
	"github.com/ipfs/go-cid"
10 11
	ds "github.com/ipfs/go-datastore"
	dssync "github.com/ipfs/go-datastore/sync"
12
	"github.com/ipfs/go-fetcher"
13
	"github.com/ipfs/go-ipfs-blockstore"
Steven Allen's avatar
Steven Allen committed
14
	offline "github.com/ipfs/go-ipfs-exchange-offline"
15
	mock "github.com/ipfs/go-ipfs-routing/mock"
Steven Allen's avatar
Steven Allen committed
16
	cbor "github.com/ipfs/go-ipld-cbor"
17
	_ "github.com/ipld/go-ipld-prime/codec/dagcbor"
Raúl Kripalani's avatar
Raúl Kripalani committed
18 19
	peer "github.com/libp2p/go-libp2p-core/peer"
	testutil "github.com/libp2p/go-libp2p-testing/net"
Steven Allen's avatar
Steven Allen committed
20
	mh "github.com/multiformats/go-multihash"
21

Michael Avila's avatar
Michael Avila committed
22
	. "github.com/ipfs/go-ipfs-provider/simple"
23 24
)

Steven Allen's avatar
Steven Allen committed
25
func setupRouting(t *testing.T) (clA, clB mock.Client, idA, idB peer.ID) {
26 27
	mrserv := mock.NewServer()

Steven Allen's avatar
Steven Allen committed
28 29
	iidA := testutil.RandIdentityOrFatal(t)
	iidB := testutil.RandIdentityOrFatal(t)
30

Steven Allen's avatar
Steven Allen committed
31 32
	clA = mrserv.Client(iidA)
	clB = mrserv.Client(iidB)
33

Steven Allen's avatar
Steven Allen committed
34 35
	return clA, clB, iidA.ID(), iidB.ID()
}
36

Steven Allen's avatar
Steven Allen committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
func setupDag(t *testing.T) (nodes []cid.Cid, bstore blockstore.Blockstore) {
	bstore = blockstore.NewBlockstore(dssync.MutexWrap(ds.NewMapDatastore()))
	for _, data := range []string{"foo", "bar"} {
		blk, err := cbor.WrapObject(data, mh.SHA2_256, -1)
		if err != nil {
			t.Fatal(err)
		}
		err = bstore.Put(blk)
		if err != nil {
			t.Fatal(err)
		}
		nodes = append(nodes, blk.Cid())

		blk, err = cbor.WrapObject(map[string]interface{}{
			"child": blk.Cid(),
		}, mh.SHA2_256, -1)
		if err != nil {
			t.Fatal(err)
		}
		err = bstore.Put(blk)
		if err != nil {
			t.Fatal(err)
		}
		nodes = append(nodes, blk.Cid())
61 62
	}

Steven Allen's avatar
Steven Allen committed
63 64 65 66
	return nodes, bstore
}

func TestReprovide(t *testing.T) {
Steven Allen's avatar
Steven Allen committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	testReprovide(t, func(r *Reprovider, ctx context.Context) error {
		return r.Reprovide()
	})
}

func TestTrigger(t *testing.T) {
	testReprovide(t, func(r *Reprovider, ctx context.Context) error {
		go r.Run()
		time.Sleep(1 * time.Second)
		defer r.Close()
		err := r.Trigger(ctx)
		return err
	})
}

func testReprovide(t *testing.T, trigger func(r *Reprovider, ctx context.Context) error) {
Steven Allen's avatar
Steven Allen committed
83 84 85 86 87 88
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	clA, clB, idA, _ := setupRouting(t)
	nodes, bstore := setupDag(t)

89 90
	keyProvider := NewBlockstoreProvider(bstore)
	reprov := NewReprovider(ctx, time.Hour, clA, keyProvider)
Steven Allen's avatar
Steven Allen committed
91
	err := trigger(reprov, ctx)
92 93 94 95
	if err != nil {
		t.Fatal(err)
	}

Raúl Kripalani's avatar
Raúl Kripalani committed
96
	var providers []peer.AddrInfo
97 98
	maxProvs := 100

Steven Allen's avatar
Steven Allen committed
99 100 101 102 103
	for _, c := range nodes {
		provChan := clB.FindProvidersAsync(ctx, c, maxProvs)
		for p := range provChan {
			providers = append(providers, p)
		}
104

Steven Allen's avatar
Steven Allen committed
105 106 107 108 109 110 111
		if len(providers) == 0 {
			t.Fatal("Should have gotten a provider")
		}

		if providers[0].ID != idA {
			t.Fatal("Somehow got the wrong peer back as a provider.")
		}
112
	}
Steven Allen's avatar
Steven Allen committed
113 114
}

Steven Allen's avatar
Steven Allen committed
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 141 142 143 144 145 146 147 148 149 150
func TestTriggerTwice(t *testing.T) {
	// Ensure we can only trigger once at a time.
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	clA, _, _, _ := setupRouting(t)

	keyCh := make(chan cid.Cid)
	startCh := make(chan struct{})
	keyFunc := func(ctx context.Context) (<-chan cid.Cid, error) {
		<-startCh
		return keyCh, nil
	}

	reprov := NewReprovider(ctx, time.Hour, clA, keyFunc)
	go reprov.Run()
	defer reprov.Close()

	// Wait for the reprovider to start, otherwise, the reprovider will
	// think a concurrent reprovide is running.
	//
	// We _could_ fix this race... but that would be complexity for nothing.
	// 1. We start a reprovide 1 minute after startup anyways.
	// 2. The window is really narrow.
	time.Sleep(1 * time.Second)

	errCh := make(chan error, 2)

	// Trigger in the background
	go func() {
		errCh <- reprov.Trigger(ctx)
	}()

	// Wait for the trigger to really start.
	startCh <- struct{}{}

Steven Allen's avatar
Steven Allen committed
151
	start := time.Now()
Steven Allen's avatar
Steven Allen committed
152 153 154 155
	// Try to trigger again, this should fail immediately.
	if err := reprov.Trigger(ctx); err == nil {
		t.Fatal("expected an error")
	}
Steven Allen's avatar
Steven Allen committed
156 157 158
	if time.Since(start) > 10*time.Millisecond {
		t.Fatal("expected reprovide to fail instantly")
	}
Steven Allen's avatar
Steven Allen committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

	// Let the trigger progress.
	close(keyCh)

	// Check the result.
	err := <-errCh
	if err != nil {
		t.Fatal(err)
	}

	// Try to trigger again, this should work.
	go func() {
		errCh <- reprov.Trigger(ctx)
	}()
	startCh <- struct{}{}
	err = <-errCh
	if err != nil {
		t.Fatal(err)
	}
}

Steven Allen's avatar
Steven Allen committed
180 181 182 183 184
type mockPinner struct {
	recursive []cid.Cid
	direct    []cid.Cid
}

185 186
func (mp *mockPinner) DirectKeys(ctx context.Context) ([]cid.Cid, error) {
	return mp.direct, nil
Steven Allen's avatar
Steven Allen committed
187 188
}

189 190
func (mp *mockPinner) RecursiveKeys(ctx context.Context) ([]cid.Cid, error) {
	return mp.recursive, nil
Steven Allen's avatar
Steven Allen committed
191 192 193 194 195 196 197 198
}

func TestReprovidePinned(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	nodes, bstore := setupDag(t)

199
	fetchConfig := fetcher.NewFetcherConfig(bsrv.New(bstore, offline.Exchange(bstore)))
Steven Allen's avatar
Steven Allen committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

	for i := 0; i < 2; i++ {
		clA, clB, idA, _ := setupRouting(t)

		onlyRoots := i == 0
		t.Logf("only roots: %v", onlyRoots)

		var provide, dont []cid.Cid
		if onlyRoots {
			provide = []cid.Cid{nodes[1], nodes[3]}
			dont = []cid.Cid{nodes[0], nodes[2]}
		} else {
			provide = []cid.Cid{nodes[0], nodes[1], nodes[3]}
			dont = []cid.Cid{nodes[2]}
		}

		keyProvider := NewPinnedProvider(onlyRoots, &mockPinner{
			recursive: []cid.Cid{nodes[1]},
			direct:    []cid.Cid{nodes[3]},
219
		}, fetchConfig)
Steven Allen's avatar
Steven Allen committed
220 221 222 223 224 225 226 227 228 229 230 231 232

		reprov := NewReprovider(ctx, time.Hour, clA, keyProvider)
		err := reprov.Reprovide()
		if err != nil {
			t.Fatal(err)
		}

		for i, c := range provide {
			prov, ok := <-clB.FindProvidersAsync(ctx, c, 1)
			if !ok {
				t.Errorf("Should have gotten a provider for %d", i)
				continue
			}
233

Steven Allen's avatar
Steven Allen committed
234 235 236 237 238 239 240 241 242 243 244
			if prov.ID != idA {
				t.Errorf("Somehow got the wrong peer back as a provider.")
				continue
			}
		}
		for i, c := range dont {
			prov, ok := <-clB.FindProvidersAsync(ctx, c, 1)
			if ok {
				t.Fatalf("found provider %s for %d, expected none", prov.ID, i)
			}
		}
245 246
	}
}