Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #29 from xmtp/fix-panic-on-remove
Browse files Browse the repository at this point in the history
Fix issue removing subscriptions
  • Loading branch information
neekolas authored Jul 26, 2022
2 parents 3c34a8c + f7bd079 commit 7710a1d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 4 deletions.
9 changes: 5 additions & 4 deletions waku/v2/protocol/filter/filter_subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,23 @@ func (sub *Subscribers) RemoveContentFilters(peerID peer.ID, contentFilters []*p

var peerIdsToRemove []peer.ID

for _, subscriber := range sub.subscribers {
for subIndex, subscriber := range sub.subscribers {
if subscriber.peer != peerID {
continue
}

// make sure we delete the content filter
// if no more topics are left
for i, contentFilter := range contentFilters {
for _, contentFilter := range contentFilters {
subCfs := subscriber.filter.ContentFilters
for _, cf := range subCfs {
for i, cf := range subCfs {
if cf.ContentTopic == contentFilter.ContentTopic {
l := len(subCfs) - 1
subCfs[l], subCfs[i] = subCfs[i], subCfs[l]
subCfs[i] = subCfs[l]
subscriber.filter.ContentFilters = subCfs[:l]
}
}
sub.subscribers[subIndex] = subscriber
}

if len(subscriber.filter.ContentFilters) == 0 {
Expand Down
91 changes: 91 additions & 0 deletions waku/v2/protocol/filter/filter_subscribers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package filter

import (
"testing"
"time"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/test"
"github.com/status-im/go-waku/waku/v2/protocol/pb"
"github.com/stretchr/testify/assert"
)

const TOPIC = "/test/topic"

func createPeerId(t *testing.T) peer.ID {
peerId, err := test.RandPeerID()
assert.NoError(t, err)
return peerId
}

func firstSubscriber(subs *Subscribers, contentTopic string) *Subscriber {
for sub := range subs.Items(&contentTopic) {
return &sub
}
return nil
}

func TestAppend(t *testing.T) {
subs := NewSubscribers(10 * time.Second)
peerId := createPeerId(t)
contentTopic := "topic1"
request := pb.FilterRequest{
Subscribe: true,
Topic: TOPIC,
ContentFilters: []*pb.FilterRequest_ContentFilter{{ContentTopic: contentTopic}},
}
subs.Append(Subscriber{peerId, "request_1", request})

sub := firstSubscriber(subs, contentTopic)
assert.NotNil(t, sub)
}

func TestRemove(t *testing.T) {
subs := NewSubscribers(10 * time.Second)
peerId := createPeerId(t)
contentTopic := "topic1"
request := pb.FilterRequest{
Subscribe: true,
Topic: TOPIC,
ContentFilters: []*pb.FilterRequest_ContentFilter{{ContentTopic: contentTopic}},
}
subs.Append(Subscriber{peerId, "request_1", request})
subs.RemoveContentFilters(peerId, request.ContentFilters)

sub := firstSubscriber(subs, contentTopic)
assert.Nil(t, sub)
}

func TestRemovePartial(t *testing.T) {
subs := NewSubscribers(10 * time.Second)
peerId := createPeerId(t)
topic1 := "topic1"
topic2 := "topic2"
request := pb.FilterRequest{
Subscribe: true,
Topic: TOPIC,
ContentFilters: []*pb.FilterRequest_ContentFilter{{ContentTopic: topic1}, {ContentTopic: topic2}},
}
subs.Append(Subscriber{peerId, "request_1", request})
subs.RemoveContentFilters(peerId, []*pb.FilterRequest_ContentFilter{{ContentTopic: topic1}})

sub := firstSubscriber(subs, topic2)
assert.NotNil(t, sub)
assert.Len(t, sub.filter.ContentFilters, 1)
}

func TestRemoveBogus(t *testing.T) {
subs := NewSubscribers(10 * time.Second)
peerId := createPeerId(t)
contentTopic := "topic1"
request := pb.FilterRequest{
Subscribe: true,
Topic: TOPIC,
ContentFilters: []*pb.FilterRequest_ContentFilter{{ContentTopic: contentTopic}},
}
subs.Append(Subscriber{peerId, "request_1", request})
subs.RemoveContentFilters(peerId, []*pb.FilterRequest_ContentFilter{{ContentTopic: "does not exist"}, {ContentTopic: contentTopic}})

sub := firstSubscriber(subs, contentTopic)
assert.Nil(t, sub)
}

0 comments on commit 7710a1d

Please sign in to comment.