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 <mohamedmohey2352@gmail.com>
Co-authored-by: Benjamin Wang <ahrtr>
  • Loading branch information
mohamedawnallah committed Jul 12, 2024
1 parent ba11e87 commit 095eebe
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion clientv3/watch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package clientv3

import (
"context"
"testing"

"go.etcd.io/etcd/mvcc/mvccpb"
"google.golang.org/grpc/metadata"

"go.etcd.io/etcd/api/v3/mvccpb"
)

func TestEvent(t *testing.T) {
Expand Down Expand Up @@ -53,3 +56,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 {
name string
ctx context.Context
expected string
}{
{
name: "multiple keys",
ctx: metadata.NewOutgoingContext(context.Background(), metadata.MD{
"key1": []string{"value1"},
"key2": []string{"value2a", "value2b"},
}),
expected: "map[key1:[value1] key2:[value2a value2b]]",
},
{
name: "no keys",
ctx: metadata.NewOutgoingContext(context.Background(), metadata.MD{}),
expected: "map[]",
},
{
name: "only one key",
ctx: metadata.NewOutgoingContext(context.Background(), metadata.MD{
"key1": []string{"value1", "value1a"},
}),
expected: "map[key1:[value1 value1a]]",
},
{
name: "no metadata",
ctx: context.Background(),
expected: "",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := streamKeyFromCtx(tt.ctx)
if actual != tt.expected {
t.Errorf("streamKeyFromCtx() = %v, expected %v", actual, tt.expected)
}
})
}
}

0 comments on commit 095eebe

Please sign in to comment.