Skip to content

Commit

Permalink
Add tests for GetAllP2PCollections
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Apr 4, 2023
1 parent ef5fa76 commit 870184b
Show file tree
Hide file tree
Showing 5 changed files with 253 additions and 0 deletions.
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)
}
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)
}
40 changes: 40 additions & 0 deletions tests/integration/net/state/simple/peer/subscribe/with_get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 TestP2PSubscribeGetAll(t *testing.T) {
test := testUtils.TestCase{
Actions: []any{
testUtils.RandomNetworkingConfig(),
testUtils.RandomNetworkingConfig(),
testUtils.ConnectPeers{
SourceNodeID: 1,
TargetNodeID: 0,
},
testUtils.GetAllP2PCollections{
NodeID: 0,
ExpectedCollectionIDs: []int{},
},
testUtils.GetAllP2PCollections{
NodeID: 1,
ExpectedCollectionIDs: []int{},
},
},
}

testUtils.ExecuteTestCase(t, []string{}, test)
}
38 changes: 38 additions & 0 deletions tests/integration/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ type UnsubscribeToCollection struct {
CollectionIDs []int
}

type GetAllP2PCollections struct {
// NodeID is the node ID (index) of the node in which to get the subscriptions for.
NodeID int

// ExpectedCollectionIDs are the collection IDs (indexes) of the collections expected.
ExpectedCollectionIDs []int
}

// WaitForSync is an action that instructs the test framework to wait for all document synchronization
// to complete before progressing.
//
Expand Down Expand Up @@ -373,6 +381,36 @@ func unsubscribeToCollection(
time.Sleep(100 * time.Millisecond)
}

// getAllP2PCollections gets all the active peer subscriptions and compares them against the
// given expected results.
//
// Any errors generated during this process will result in a test failure.
func getAllP2PCollections(
ctx context.Context,
t *testing.T,
action GetAllP2PCollections,
nodes []*node.Node,
collections [][]client.Collection,
) {
expectedCollections := []client.P2PCollection{}
for _, collectionIndex := range action.ExpectedCollectionIDs {
col := collections[action.NodeID][collectionIndex]
expectedCollections = append(
expectedCollections,
client.P2PCollection{
ID: col.SchemaID(),
Name: col.Name(),
},
)
}

n := nodes[action.NodeID]
cols, err := n.Peer.GetAllP2PCollections()
require.NoError(t, err)

assert.Equal(t, expectedCollections, cols)
}

// waitForSync waits for all given wait channels to receive an item signaling completion.
//
// Will fail the test if an event is not received within the expected time interval to prevent tests
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,9 @@ func executeTestCase(
case UnsubscribeToCollection:
unsubscribeToCollection(ctx, t, action, nodes, collections)

case GetAllP2PCollections:
getAllP2PCollections(ctx, t, action, nodes, collections)

case SchemaUpdate:
updateSchema(ctx, t, nodes, testCase, action)
// If the schema was updated we need to refresh the collection definitions.
Expand Down

0 comments on commit 870184b

Please sign in to comment.