Skip to content

Commit

Permalink
test(proto): add unit tests for metadata descriptors
Browse files Browse the repository at this point in the history
- Add tests for StorageNodeMetadataDescriptor and LogStreamReplicaMetadataDescriptor
- Test methods: ToStorageNodeDescriptor, GetLogStream, Head, and Tail
- Ensure proper coverage for edge cases and nil values
  • Loading branch information
ijsong committed Nov 24, 2024
1 parent a0af1e1 commit 18fcbc4
Showing 1 changed file with 194 additions and 0 deletions.
194 changes: 194 additions & 0 deletions proto/snpb/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
package snpb_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/kakao/varlog/pkg/types"
"github.com/kakao/varlog/proto/snpb"
"github.com/kakao/varlog/proto/varlogpb"
)

func TestStorageNodeMetadataDescriptor_ToStorageNodeDescriptor(t *testing.T) {
tests := []struct {
snmd *snpb.StorageNodeMetadataDescriptor
want *varlogpb.StorageNodeDescriptor
name string
}{
{
name: "Nil",
snmd: nil,
want: nil,
},
{
name: "NonNil",
snmd: &snpb.StorageNodeMetadataDescriptor{
StorageNode: varlogpb.StorageNode{
StorageNodeID: types.MinStorageNodeID,
Address: "node1",
},
Storages: []varlogpb.StorageDescriptor{
{Path: "/path1"},
{Path: "/path2"},
},
},
want: &varlogpb.StorageNodeDescriptor{
StorageNode: varlogpb.StorageNode{
StorageNodeID: types.MinStorageNodeID,
Address: "node1",
},
Paths: []string{"/path1", "/path2"},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var got *varlogpb.StorageNodeDescriptor
if tt.snmd != nil {
got = tt.snmd.ToStorageNodeDescriptor()
}
assert.Equal(t, tt.want, got)
})
}
}

func TestStorageNodeMetadataDescriptor_GetLogStream(t *testing.T) {
snmd := &snpb.StorageNodeMetadataDescriptor{
LogStreamReplicas: []snpb.LogStreamReplicaMetadataDescriptor{
{
LogStreamReplica: varlogpb.LogStreamReplica{
TopicLogStream: varlogpb.TopicLogStream{
LogStreamID: types.LogStreamID(1),
},
},
},
{
LogStreamReplica: varlogpb.LogStreamReplica{
TopicLogStream: varlogpb.TopicLogStream{
LogStreamID: types.LogStreamID(2),
},
},
},
},
}

tests := []struct {
name string
logStreamID types.LogStreamID
want snpb.LogStreamReplicaMetadataDescriptor
wantFound bool
}{
{
name: "Found",
logStreamID: types.LogStreamID(1),
want: snpb.LogStreamReplicaMetadataDescriptor{
LogStreamReplica: varlogpb.LogStreamReplica{
TopicLogStream: varlogpb.TopicLogStream{
LogStreamID: types.LogStreamID(1),
},
},
},
wantFound: true,
},
{
name: "NotFound",
logStreamID: types.LogStreamID(3),
want: snpb.LogStreamReplicaMetadataDescriptor{},
wantFound: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, found := snmd.GetLogStream(tt.logStreamID)
assert.Equal(t, tt.wantFound, found)
assert.Equal(t, tt.want, got)
})
}
}

func TestLogStreamReplicaMetadataDescriptor_Head(t *testing.T) {
tests := []struct {
lsrmd *snpb.LogStreamReplicaMetadataDescriptor
name string
want varlogpb.LogEntryMeta
}{
{
name: "Nil",
lsrmd: nil,
want: varlogpb.LogEntryMeta{},
},
{
name: "NonNil",
lsrmd: &snpb.LogStreamReplicaMetadataDescriptor{
LogStreamReplica: varlogpb.LogStreamReplica{
TopicLogStream: varlogpb.TopicLogStream{
TopicID: types.TopicID(1),
LogStreamID: types.LogStreamID(2),
},
},
LocalLowWatermark: varlogpb.LogSequenceNumber{
LLSN: types.LLSN(3),
GLSN: types.GLSN(4),
},
},
want: varlogpb.LogEntryMeta{
TopicID: 1,
LogStreamID: 2,
LLSN: 3,
GLSN: 4,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.lsrmd.Head()
assert.Equal(t, tt.want, got)
})
}
}

func TestLogStreamReplicaMetadataDescriptor_Tail(t *testing.T) {
tests := []struct {
lsrmd *snpb.LogStreamReplicaMetadataDescriptor
name string
want varlogpb.LogEntryMeta
}{
{
name: "Nil",
lsrmd: nil,
want: varlogpb.LogEntryMeta{},
},
{
name: "NonNil",
lsrmd: &snpb.LogStreamReplicaMetadataDescriptor{
LogStreamReplica: varlogpb.LogStreamReplica{
TopicLogStream: varlogpb.TopicLogStream{
TopicID: types.TopicID(1),
LogStreamID: types.LogStreamID(2),
},
},
LocalHighWatermark: varlogpb.LogSequenceNumber{
LLSN: types.LLSN(5),
GLSN: types.GLSN(6),
},
},
want: varlogpb.LogEntryMeta{
TopicID: 1,
LogStreamID: 2,
LLSN: 5,
GLSN: 6,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.lsrmd.Tail()
assert.Equal(t, tt.want, got)
})
}
}

0 comments on commit 18fcbc4

Please sign in to comment.