Skip to content

Commit

Permalink
Merge pull request ipfs#21 from polezaivsani/fix/providers/bugfixes
Browse files Browse the repository at this point in the history
Fix/providers/bugfixes
  • Loading branch information
whyrusleeping authored Nov 16, 2016
2 parents df7a822 + c12d865 commit 4e5a129
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
10 changes: 9 additions & 1 deletion providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,11 @@ func readTimeValue(i interface{}) (time.Time, error) {
func (pm *ProviderManager) addProv(k *cid.Cid, p peer.ID) error {
iprovs, ok := pm.providers.Get(k.KeyString())
if !ok {
iprovs = newProviderSet()
stored, err := loadProvSet(pm.dstore, k)
if err != nil {
return err
}
iprovs = stored
pm.providers.Add(k.KeyString(), iprovs)
}
provs := iprovs.(*providerSet)
Expand All @@ -186,6 +190,9 @@ func (pm *ProviderManager) deleteProvSet(k *cid.Cid) error {
KeysOnly: true,
Prefix: mkProvKey(k),
})
if err != nil {
return err
}

entries, err := res.Rest()
if err != nil {
Expand Down Expand Up @@ -288,6 +295,7 @@ func (pm *ProviderManager) run() {
}
}
case <-pm.proc.Closing():
tick.Stop()
return
}
}
Expand Down
23 changes: 23 additions & 0 deletions providers/providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,26 @@ func TestLargeProvidersSet(t *testing.T) {
}
//*/

func TestUponCacheMissProvidersAreReadFromDatastore(t *testing.T) {
old := lruCacheSize
lruCacheSize = 1
defer func() { lruCacheSize = old }()
ctx := context.Background()

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

pm.AddProvider(ctx, c1, p1)
// make the cached provider for c1 go to datastore
pm.AddProvider(ctx, c2, p1)
// now just offloaded record should be brought back and joined with p2
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 4e5a129

Please sign in to comment.