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

Fix issue removing subscriptions #29

Merged
merged 9 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 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]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems that this could just be subCfs[i] = subCfs[l] given that we're dropping subCfs[l] on the next line.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right. Simplified.

subscriber.filter.ContentFilters = subCfs[:l]
}
}
sub.subscribers[subIndex] = subscriber
Copy link

@mkobetic mkobetic Jul 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, right, []Subscriber instead of []*Subscriber, it's messy when you don't use pointers. Alternative would be to set subscriber := &(sub.subscribers[subIndex]) up top, but this works too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took me a while banging my head against the wall to notice that one...

}

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)
}