Skip to content

Commit

Permalink
Merge pull request #36 from manu844/list-peers
Browse files Browse the repository at this point in the history
add ListPeers method that returns universe's fetchers.
  • Loading branch information
sergiosalvatore authored Dec 19, 2022
2 parents 7157786 + 928f2cd commit 3fc36a9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
6 changes: 6 additions & 0 deletions galaxycache.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,12 @@ func (universe *Universe) RemovePeers(ids ...string) error {
return universe.peerPicker.remove(ids...)
}

// ListPeers returns a map of remote fetchers keyed by Peer ID,
// useful for testing incremental changes to galaxycache peers.
func (universe *Universe) ListPeers() map[string]RemoteFetcher {
return universe.peerPicker.listPeers()
}

// Shutdown closes all open fetcher connections
func (universe *Universe) Shutdown() error {
return universe.peerPicker.shutdown()
Expand Down
13 changes: 13 additions & 0 deletions peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,19 @@ func (pp *PeerPicker) remove(ids ...string) error {
return eg.Wait()
}

func (pp *PeerPicker) listPeers() map[string]RemoteFetcher {
pp.mu.RLock()
defer pp.mu.RUnlock()

// copy of pp.fetchers map.
fetchers := make(map[string]RemoteFetcher, len(pp.fetchers))
for p, f := range pp.fetchers {
fetchers[p] = f
}

return fetchers
}

func (pp *PeerPicker) shutdown() error {
pp.setIncludeSelf(false)
// Clear out all the existing peers
Expand Down
27 changes: 27 additions & 0 deletions peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,14 @@ func TestPeersIncremental(t *testing.T) {
fetcherURIs[f.(*TestFetcher).uri] = struct{}{}
}

allFetchers := u.ListPeers()
allPeerIDs := make(map[string]struct{}, len(allFetchers))
allFetcherURIs := make(map[string]struct{}, len(allFetchers))
for peerID, fetcher := range allFetchers {
allPeerIDs[peerID] = struct{}{}
allFetcherURIs[fetcher.(*TestFetcher).uri] = struct{}{}
}

for _, expPeer := range step.expectedPeers {
if _, ok := allPeers[expPeer.ID]; !ok {
t.Errorf("missing peer %q from hashring at step %d", expPeer.ID, si)
Expand All @@ -258,6 +266,17 @@ func TestPeersIncremental(t *testing.T) {
expPeer.ID, expPeer.URI, si)
}
delete(fetcherURIs, expPeer.URI)
// Checks for peer IDs and fetchers from listPeers method.
if _, ok := allPeerIDs[expPeer.ID]; !ok {
t.Errorf("missing peer %q from copy of fetchers map keys at step %d",
expPeer.ID, si)
}
delete(allPeerIDs, expPeer.ID)
if _, ok := allFetcherURIs[expPeer.URI]; !ok {
t.Errorf("missing peer %q (URI %q) from copy of fetchers values at step %d",
expPeer.ID, expPeer.URI, si)
}
delete(allFetcherURIs, expPeer.URI)
}
if step.includeSelf {
if _, ok := allPeers[selfID]; !ok {
Expand All @@ -274,6 +293,14 @@ func TestPeersIncremental(t *testing.T) {
if len(fetcherURIs) > 0 {
t.Errorf("unexpected peer(s)' URI(s) in fetcher-map at step %d: %v", si, fetcherURIs)
}
// Checks for peer IDs and fetchers from listPeers method.
if len(allPeerIDs) > 0 {
t.Errorf("unexpected peer(s) in copy of fetcher-map at step %d: %v", si, allPeerIDs)
}
if len(allFetcherURIs) > 0 {
t.Errorf("unexpected peer(s)' URI(s) in copy of fetcher-map at step %d: %v",
si, allFetcherURIs)
}
}
})
}
Expand Down

0 comments on commit 3fc36a9

Please sign in to comment.