Skip to content

Commit

Permalink
client/v3/watch_test.go: test fmt metadata print
Browse files Browse the repository at this point in the history
Signed-off-by: Mohamed Awnallah <[email protected]>
  • Loading branch information
mohamedawnallah committed Jul 10, 2024
1 parent 84c4fe2 commit ed6b76a
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions client/v3/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
package clientv3

import (
"context"
"testing"

"go.etcd.io/etcd/api/v3/mvccpb"
"google.golang.org/grpc/metadata"
)

func TestEvent(t *testing.T) {
Expand Down Expand Up @@ -53,3 +55,53 @@ func TestEvent(t *testing.T) {
}
}
}

// TestStreamKeyFromCtx tests the streamKeyFromCtx function to ensure it correctly
// formats metadata as a map[string][]string when extracting metadata from the context.
//
// The fmt package in Go guarantees that maps are printed in a consistent order,
// sorted by the keys. This test verifies that the streamKeyFromCtx function
// produces the expected formatted string representation of metadata maps when called with
// various context scenarios.
func TestStreamKeyFromCtx(t *testing.T) {
tests := []struct {
md metadata.MD
expected string
}{
{
md: metadata.MD{
"key1": []string{"value1"},
"key2": []string{"value2a", "value2b"},
},
expected: "map[key1:[value1] key2:[value2a value2b]]",
},
{
md: metadata.MD{},
expected: "map[]",
},
{
md: metadata.MD{
"key1": []string{"value1", "value1a"},
},
expected: "map[key1:[value1 value1a]]",
},
}

// Iterate through each test case and verify the function's output matches
// the expected result.
for i, tt := range tests {
ctx := metadata.NewOutgoingContext(context.Background(), tt.md)
actual := streamKeyFromCtx(ctx)
if actual != tt.expected {
t.Errorf("#%d: streamKeyFromCtx() = %v, want %v", i, actual, tt.expected)
}
}

// Test with no metadata in context to ensure it returns an empty string.
ctx := context.Background()
expected := ""
actual := streamKeyFromCtx(ctx)
if actual != expected {
t.Errorf("streamKeyFromCtx() = %v, want %v", actual, expected)
}
}

0 comments on commit ed6b76a

Please sign in to comment.