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(proto): add unit tests for LogStreamReplica #934

Open
wants to merge 1 commit into
base: test_proto_add_and_update_tests_for_proto_varlogpb_metadata.go
Choose a base branch
from
Open
Changes from all 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
152 changes: 152 additions & 0 deletions proto/varlogpb/replica_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package varlogpb_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/kakao/varlog/proto/varlogpb"
)

func TestEqualReplicas(t *testing.T) {
tcs := []struct {
name string
xs []varlogpb.LogStreamReplica
ys []varlogpb.LogStreamReplica
want bool
}{
{
name: "EqualReplicas",
xs: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
},
ys: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
},
want: true,
},
{
name: "DifferentLengths",
xs: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
},
ys: []varlogpb.LogStreamReplica{},
want: false,
},
{
name: "DifferentStorageNodeID",
xs: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
},
ys: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 2},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
},
want: false,
},
{
name: "DifferentLogStreamID",
xs: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
},
ys: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 2},
},
},
want: false,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
got := varlogpb.EqualReplicas(tc.xs, tc.ys)
require.Equal(t, tc.want, got)
})
}
}

func TestValidReplicas(t *testing.T) {
tcs := []struct {
name string
replicas []varlogpb.LogStreamReplica
wantErr bool
}{
{
name: "ValidReplicas",
replicas: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 2},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
},
wantErr: false,
},
{
name: "NoReplicas",
replicas: []varlogpb.LogStreamReplica{},
wantErr: true,
},
{
name: "LogStreamIDMismatch",
replicas: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 2},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 2},
},
},
wantErr: true,
},
{
name: "StorageNodeIDDuplicated",
replicas: []varlogpb.LogStreamReplica{
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
{
StorageNode: varlogpb.StorageNode{StorageNodeID: 1},
TopicLogStream: varlogpb.TopicLogStream{LogStreamID: 1},
},
},
wantErr: true,
},
}

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
err := varlogpb.ValidReplicas(tc.replicas)
if tc.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
})
}
}
Loading