-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(proto): add unit tests for metadata descriptors
- 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
Showing
1 changed file
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |