Skip to content

Commit

Permalink
test(proto): add unit tests for LogStreamReplica
Browse files Browse the repository at this point in the history
- Added `proto/varlogpb/replica_test.go` to test `EqualReplicas` and
  `ValidReplicas` functions.
- Included various test cases to ensure correct functionality.
  • Loading branch information
ijsong committed Dec 13, 2024
1 parent d43ca78 commit f953940
Showing 1 changed file with 152 additions and 0 deletions.
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)
}
})
}
}

0 comments on commit f953940

Please sign in to comment.