Skip to content

Commit

Permalink
providers: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Stebalien committed Apr 13, 2019
1 parent 3f52c66 commit 7ce5021
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions providers/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ func TestProviderManager(t *testing.T) {
p := NewProviderManager(ctx, mid, dssync.MutexWrap(ds.NewMapDatastore()))
a := cid.NewCidV0(u.Hash([]byte("test")))
p.AddProvider(ctx, a, peer.ID("testingprovider"))

// Not cached
resp := p.GetProviders(ctx, a)
if len(resp) != 1 {
t.Fatal("Could not retrieve provider.")
}

// Cached
resp = p.GetProviders(ctx, a)
if len(resp) != 1 {
t.Fatal("Could not retrieve provider.")
}
p.proc.Close()
}

Expand Down Expand Up @@ -235,6 +243,7 @@ func TestUponCacheMissProvidersAreReadFromDatastore(t *testing.T) {
c2 := cid.NewCidV1(cid.DagCBOR, u.Hash([]byte("2")))
pm := NewProviderManager(ctx, p1, dssync.MutexWrap(ds.NewMapDatastore()))

// add provider
pm.AddProvider(ctx, c1, p1)
// make the cached provider for c1 go to datastore
pm.AddProvider(ctx, c2, p1)
Expand All @@ -246,3 +255,24 @@ func TestUponCacheMissProvidersAreReadFromDatastore(t *testing.T) {
t.Fatalf("expected c1 to be provided by 2 peers, is by %d", len(c1Provs))
}
}

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

p1, p2 := peer.ID("a"), peer.ID("b")
c1 := cid.NewCidV1(cid.DagCBOR, u.Hash([]byte("1")))
pm := NewProviderManager(ctx, p1, dssync.MutexWrap(ds.NewMapDatastore()))

// add provider
pm.AddProvider(ctx, c1, p1)
// force into the cache
pm.GetProviders(ctx, c1)
// add a second provider
pm.AddProvider(ctx, c1, p2)

c1Provs := pm.GetProviders(ctx, c1)
if len(c1Provs) != 2 {
t.Fatalf("expected c1 to be provided by 2 peers, is by %d", len(c1Provs))
}
}

0 comments on commit 7ce5021

Please sign in to comment.