This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
forked from waku-org/go-waku
-
Notifications
You must be signed in to change notification settings - Fork 3
Fix issue removing subscriptions #29
Merged
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9eb7629
Add failing test
neekolas eb6824c
Actually fix
neekolas bfc80b2
Add test for partial removal
neekolas 1cf7af2
Working solution I don't like a lot
neekolas 78fa2ef
Working solution I like more
neekolas 1e2f76f
Remove log line
neekolas 109db7a
Clean up tests
neekolas 51700e7
Simplify test
neekolas f7bd079
Simplify replacement
neekolas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
subscriber.filter.ContentFilters = subCfs[:l] | ||
} | ||
} | ||
sub.subscribers[subIndex] = subscriber | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, right, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 droppingsubCfs[l]
on the next line.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. Simplified.