providers_test.go 3.09 KB
Newer Older
1 2 3
package providers

import (
4
	"context"
5 6 7 8
	"fmt"
	"testing"
	"time"

9
	cid "github.com/ipfs/go-cid"
10
	ds "github.com/ipfs/go-datastore"
11
	u "github.com/ipfs/go-ipfs-util"
12
	peer "github.com/libp2p/go-libp2p-peer"
13 14 15 16 17 18
)

func TestProviderManager(t *testing.T) {
	ctx := context.Background()
	mid := peer.ID("testing")
	p := NewProviderManager(ctx, mid, ds.NewMapDatastore())
19
	a := cid.NewCidV0(u.Hash([]byte("test")))
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
	p.AddProvider(ctx, a, peer.ID("testingprovider"))
	resp := p.GetProviders(ctx, a)
	if len(resp) != 1 {
		t.Fatal("Could not retrieve provider.")
	}
	p.proc.Close()
}

func TestProvidersDatastore(t *testing.T) {
	old := lruCacheSize
	lruCacheSize = 10
	defer func() { lruCacheSize = old }()

	ctx := context.Background()
	mid := peer.ID("testing")
	p := NewProviderManager(ctx, mid, ds.NewMapDatastore())
	defer p.proc.Close()

	friend := peer.ID("friend")
39
	var cids []*cid.Cid
40
	for i := 0; i < 100; i++ {
41 42 43
		c := cid.NewCidV0(u.Hash([]byte(fmt.Sprint(i))))
		cids = append(cids, c)
		p.AddProvider(ctx, c, friend)
44 45
	}

46 47
	for _, c := range cids {
		resp := p.GetProviders(ctx, c)
48 49 50 51 52 53 54 55 56 57 58 59
		if len(resp) != 1 {
			t.Fatal("Could not retrieve provider.")
		}
		if resp[0] != friend {
			t.Fatal("expected provider to be 'friend'")
		}
	}
}

func TestProvidersSerialization(t *testing.T) {
	dstore := ds.NewMapDatastore()

60
	k := cid.NewCidV0(u.Hash(([]byte("my key!"))))
Jeromy's avatar
Jeromy committed
61 62 63 64
	p1 := peer.ID("peer one")
	p2 := peer.ID("peer two")
	pt1 := time.Now()
	pt2 := pt1.Add(time.Hour)
65

Jeromy's avatar
Jeromy committed
66 67 68 69 70 71
	err := writeProviderEntry(dstore, k, p1, pt1)
	if err != nil {
		t.Fatal(err)
	}

	err = writeProviderEntry(dstore, k, p2, pt2)
72 73 74 75 76 77 78 79 80
	if err != nil {
		t.Fatal(err)
	}

	pset, err := loadProvSet(dstore, k)
	if err != nil {
		t.Fatal(err)
	}

Jeromy's avatar
Jeromy committed
81 82 83 84 85 86 87 88 89 90
	lt1, ok := pset.set[p1]
	if !ok {
		t.Fatal("failed to load set correctly")
	}

	if pt1 != lt1 {
		t.Fatal("time wasnt serialized correctly")
	}

	lt2, ok := pset.set[p2]
91 92 93 94
	if !ok {
		t.Fatal("failed to load set correctly")
	}

Jeromy's avatar
Jeromy committed
95
	if pt2 != lt2 {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
		t.Fatal("time wasnt serialized correctly")
	}
}

func TestProvidesExpire(t *testing.T) {
	pval := ProvideValidity
	cleanup := defaultCleanupInterval
	ProvideValidity = time.Second / 2
	defaultCleanupInterval = time.Second / 2
	defer func() {
		ProvideValidity = pval
		defaultCleanupInterval = cleanup
	}()

	ctx := context.Background()
	mid := peer.ID("testing")
	p := NewProviderManager(ctx, mid, ds.NewMapDatastore())

	peers := []peer.ID{"a", "b"}
115
	var cids []*cid.Cid
116
	for i := 0; i < 10; i++ {
117 118 119 120
		c := cid.NewCidV0(u.Hash([]byte(fmt.Sprint(i))))
		cids = append(cids, c)
		p.AddProvider(ctx, c, peers[0])
		p.AddProvider(ctx, c, peers[1])
121 122 123
	}

	for i := 0; i < 10; i++ {
124
		out := p.GetProviders(ctx, cids[i])
125 126 127 128 129 130 131
		if len(out) != 2 {
			t.Fatal("expected providers to still be there")
		}
	}

	time.Sleep(time.Second)
	for i := 0; i < 10; i++ {
132 133 134
		out := p.GetProviders(ctx, cids[i])
		if len(out) > 0 {
			t.Fatal("expected providers to be cleaned up, got: ", out)
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		}
	}

	if p.providers.Len() != 0 {
		t.Fatal("providers map not cleaned up")
	}

	allprovs, err := p.getAllProvKeys()
	if err != nil {
		t.Fatal(err)
	}

	if len(allprovs) != 0 {
		t.Fatal("expected everything to be cleaned out of the datastore")
	}
}