reprovide_test.go 5.59 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 12
	ds "github.com/ipfs/go-datastore"
	dssync "github.com/ipfs/go-datastore/sync"
	"github.com/ipfs/go-ipfs-blockstore"
Steven Allen's avatar
Steven Allen committed
13
	offline "github.com/ipfs/go-ipfs-exchange-offline"
14
	mock "github.com/ipfs/go-ipfs-routing/mock"
Steven Allen's avatar
Steven Allen committed
15
	cbor "github.com/ipfs/go-ipld-cbor"
16
	merkledag "github.com/ipfs/go-merkledag"
Raúl Kripalani's avatar
Raúl Kripalani committed
17 18
	peer "github.com/libp2p/go-libp2p-core/peer"
	testutil "github.com/libp2p/go-libp2p-testing/net"
Steven Allen's avatar
Steven Allen committed
19
	mh "github.com/multiformats/go-multihash"
20

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

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

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

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

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

Steven Allen's avatar
Steven Allen committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
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())
60 61
	}

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

func TestReprovide(t *testing.T) {
Steven Allen's avatar
Steven Allen committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
	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
82 83 84 85 86 87
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

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

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

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

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

Steven Allen's avatar
Steven Allen committed
104 105 106 107 108 109 110
		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.")
		}
111
	}
Steven Allen's avatar
Steven Allen committed
112 113
}

Steven Allen's avatar
Steven Allen committed
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 141 142 143 144 145 146 147 148 149
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
150
	start := time.Now()
Steven Allen's avatar
Steven Allen committed
151 152 153 154
	// 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
155 156 157
	if time.Since(start) > 10*time.Millisecond {
		t.Fatal("expected reprovide to fail instantly")
	}
Steven Allen's avatar
Steven Allen committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

	// 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
179 180 181 182 183
type mockPinner struct {
	recursive []cid.Cid
	direct    []cid.Cid
}

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

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

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

	nodes, bstore := setupDag(t)

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

	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]},
218
		}, dag)
Steven Allen's avatar
Steven Allen committed
219 220 221 222 223 224 225 226 227 228 229 230 231

		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
			}
232

Steven Allen's avatar
Steven Allen committed
233 234 235 236 237 238 239 240 241 242 243
			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)
			}
		}
244 245
	}
}