Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Expand tests for Peer subscribe actions #1287

Merged
merged 11 commits into from
Apr 5, 2023
12 changes: 9 additions & 3 deletions net/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,10 @@ type EvtPubSub struct {
Peer peer.ID
}

// AddP2PCollectionTopic adds the collectionID to the pubsup topics
// AddP2PCollectionTopic adds the given collectionIDs to the pubsup topics.
Copy link
Member

Choose a reason for hiding this comment

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

todo:

Suggested change
// AddP2PCollectionTopic adds the given collectionIDs to the pubsup topics.
// AddP2PCollections adds the given collectionIDs to the pubsup topics.

Copy link
Contributor Author

@AndrewSisley AndrewSisley Apr 5, 2023

Choose a reason for hiding this comment

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

Will sort out

  • Upadate Doc func name

//
// It will error if any of the given collectionIDs are invalid, in such a case some of the
// changes to the server may still be applied.
func (p *Peer) AddP2PCollections(collections []string) error {
txn, err := p.db.NewTxn(p.ctx, false)
if err != nil {
Expand Down Expand Up @@ -803,7 +806,10 @@ func (p *Peer) AddP2PCollections(collections []string) error {
return txn.Commit(p.ctx)
}

// RemoveP2PCollectionTopics adds the collectionID from the pubsup topics
// RemoveP2PCollectionTopics removes the given collectionIDs from the pubsup topics.
Copy link
Member

Choose a reason for hiding this comment

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

todo:

Suggested change
// RemoveP2PCollectionTopics removes the given collectionIDs from the pubsup topics.
// RemoveP2PCollections removes the given collectionIDs from the pubsup topics.

Copy link
Contributor Author

@AndrewSisley AndrewSisley Apr 5, 2023

Choose a reason for hiding this comment

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

Will sort out

  • Upadate Doc func name

//
// It will error if any of the given collectionIDs are invalid, in such a case some of the
// changes to the server may still be applied.
func (p *Peer) RemoveP2PCollections(collections []string) error {
txn, err := p.db.NewTxn(p.ctx, false)
if err != nil {
Expand Down Expand Up @@ -861,7 +867,7 @@ func (p *Peer) GetAllP2PCollections() ([]client.P2PCollection, error) {
return nil, err
}

var p2pCols []client.P2PCollection
p2pCols := []client.P2PCollection{}
for _, colID := range collections {
col, err := store.GetCollectionBySchemaID(p.ctx, colID)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package subscribe_test

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestP2PSubscribeAddRemoveGetSingle(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.RandomNetworkingConfig(),
testUtils.RandomNetworkingConfig(),
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
}
`,
},
testUtils.ConnectPeers{
SourceNodeID: 1,
TargetNodeID: 0,
},
testUtils.SubscribeToCollection{
NodeID: 1,
CollectionIDs: []int{0},
},
testUtils.UnsubscribeToCollection{
NodeID: 1,
CollectionIDs: []int{0},
},
testUtils.GetAllP2PCollections{
NodeID: 1,
ExpectedCollectionIDs: []int{},
},
},
}

testUtils.ExecuteTestCase(t, []string{"Users"}, test)
}

func TestP2PSubscribeAddRemoveGetMultiple(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.RandomNetworkingConfig(),
testUtils.RandomNetworkingConfig(),
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
}
type Giraffes {
name: String
}
`,
},
testUtils.ConnectPeers{
SourceNodeID: 1,
TargetNodeID: 0,
},
testUtils.SubscribeToCollection{
NodeID: 1,
CollectionIDs: []int{0, 1},
},
testUtils.UnsubscribeToCollection{
NodeID: 1,
// Unsubscribe from Users, but remain subscribed to Giraffes
CollectionIDs: []int{0},
},
testUtils.GetAllP2PCollections{
NodeID: 1,
ExpectedCollectionIDs: []int{1},
},
},
}

testUtils.ExecuteTestCase(t, []string{"Users", "Giraffes"}, test)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion: We could probably add a test where we specify no collection in the subscription request and it will add automatically add all collections

Copy link
Contributor Author

@AndrewSisley AndrewSisley Apr 5, 2023

Choose a reason for hiding this comment

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

The code does not look like it does that - it would just add nothing? Is a good spot either way though, as it is not tested :)

  • Close test gap

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry my mistake. I mixed up with replicators.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package subscribe_test

import (
"testing"

testUtils "github.com/sourcenetwork/defradb/tests/integration"
)

func TestP2PSubscribeAddGetSingle(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.RandomNetworkingConfig(),
testUtils.RandomNetworkingConfig(),
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
}
`,
},
testUtils.ConnectPeers{
SourceNodeID: 1,
TargetNodeID: 0,
},
testUtils.SubscribeToCollection{
NodeID: 1,
CollectionIDs: []int{0},
},
testUtils.GetAllP2PCollections{
NodeID: 1,
ExpectedCollectionIDs: []int{0},
},
},
}

testUtils.ExecuteTestCase(t, []string{"Users"}, test)
}

func TestP2PSubscribeAddGetMultiple(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.RandomNetworkingConfig(),
testUtils.RandomNetworkingConfig(),
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
}
type Giraffes {
name: String
}
type Bears {
name: String
}
`,
},
testUtils.ConnectPeers{
SourceNodeID: 1,
TargetNodeID: 0,
},
testUtils.SubscribeToCollection{
NodeID: 1,
CollectionIDs: []int{0, 2},
},
testUtils.GetAllP2PCollections{
NodeID: 1,
ExpectedCollectionIDs: []int{2, 0},
},
},
}

testUtils.ExecuteTestCase(t, []string{"Users", "Giraffes", "Bears"}, test)
}
Loading