Skip to content

Commit

Permalink
feature/remove-peer (#24)
Browse files Browse the repository at this point in the history
* started working on remove

* added test
  • Loading branch information
decanus committed May 26, 2020
1 parent 7c04910 commit ab71d5a
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dht/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ func (d *DHT) AddPeer(id state.Peer) {
d.LeafSet.Insert(id)
}

// RemovePeer removes a peer from the dht.
func (d *DHT) RemovePeer(id state.Peer) {
d.Lock()
defer d.Unlock()

ns, _ := d.NeighborhoodSet.Remove(id)
d.NeighborhoodSet = ns

d.RoutingTable = d.RoutingTable.Remove(d.ID, id)
d.LeafSet.Remove(id)
}

// deliver sends the message to all connected applications.
func (d *DHT) deliver(msg *pb.Message) {
d.RLock()
Expand Down
35 changes: 35 additions & 0 deletions dht/dht_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dht_test

import (
"bytes"
"context"
"testing"

Expand All @@ -12,6 +13,40 @@ import (
"github.com/decanus/bureka/pb"
)

func TestNode_AddPeer_And_RemovePeer(t *testing.T) {
id := []byte{5, 5, 5, 5}
insert := []byte{0, 1, 3, 3}
n := dht.New(id, nil)

n.AddPeer(insert)

if !bytes.Equal(n.LeafSet.Closest(insert), insert) {
t.Error("failed to insert in LeafSet")
}

if !bytes.Equal(n.NeighborhoodSet.Closest(insert), insert) {
t.Error("failed to insert in NeighborhoodSet")
}

if !bytes.Equal(n.RoutingTable.Route(id, insert), insert) {
t.Error("failed to insert in NeighborhoodSet")
}

n.RemovePeer(insert)

if n.RoutingTable.Route(id, insert) != nil {
t.Error("failed to remove peer from RoutingTable")
}

if n.NeighborhoodSet.Closest(insert) != nil {
t.Error("failed to remove peer from NeighborhoodSet")
}

if n.LeafSet.Closest(insert) != nil {
t.Error("failed to remove peer from LeafSet")
}
}

func TestNode_Send_ToSelf(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
Expand Down
12 changes: 12 additions & 0 deletions dht/state/routingtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ func (r RoutingTable) grow(n int) RoutingTable {
return nr
}

func (r RoutingTable) Remove(self, id Peer) RoutingTable {
nr := r
p := row(self, id)

newrow, ok := nr[p].Remove(id)
if ok {
nr[p] = newrow
}

return nr
}

func row(self, target Peer) int {
for i, v := range self {
if v == target[i] {
Expand Down
17 changes: 17 additions & 0 deletions dht/state/routingtable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ func TestRoutingTable_Insert(t *testing.T) {
}
}

func TestRoutingTable_Remove(t *testing.T) {
id := []byte{1, 2, 3, 4}
insert := []byte{1, 2, 2, 3}

r := make(state.RoutingTable, 0)

r = r.Insert(id, insert)
if !bytes.Equal(r[2][0], insert) {
t.Error("not inserted")
}

r = r.Remove(id, insert)
if len(r[2]) != 0 {
t.Error("not removed")
}
}

func TestRoutingTable_Route(t *testing.T) {
id := []byte{1, 2, 3, 4}
insert := []byte{1, 2, 2, 3}
Expand Down

0 comments on commit ab71d5a

Please sign in to comment.