|
15 | 15 | package clientv3
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "context" |
18 | 19 | "testing"
|
19 | 20 |
|
| 21 | + "google.golang.org/grpc/metadata" |
| 22 | + |
20 | 23 | "go.etcd.io/etcd/mvcc/mvccpb"
|
21 | 24 | )
|
22 | 25 |
|
@@ -53,3 +56,53 @@ func TestEvent(t *testing.T) {
|
53 | 56 | }
|
54 | 57 | }
|
55 | 58 | }
|
| 59 | + |
| 60 | +// TestStreamKeyFromCtx tests the streamKeyFromCtx function to ensure it correctly |
| 61 | +// formats metadata as a map[string][]string when extracting metadata from the context. |
| 62 | +// |
| 63 | +// The fmt package in Go guarantees that maps are printed in a consistent order, |
| 64 | +// sorted by the keys. This test verifies that the streamKeyFromCtx function |
| 65 | +// produces the expected formatted string representation of metadata maps when called with |
| 66 | +// various context scenarios. |
| 67 | +func TestStreamKeyFromCtx(t *testing.T) { |
| 68 | + tests := []struct { |
| 69 | + name string |
| 70 | + ctx context.Context |
| 71 | + expected string |
| 72 | + }{ |
| 73 | + { |
| 74 | + name: "multiple keys", |
| 75 | + ctx: metadata.NewOutgoingContext(context.Background(), metadata.MD{ |
| 76 | + "key1": []string{"value1"}, |
| 77 | + "key2": []string{"value2a", "value2b"}, |
| 78 | + }), |
| 79 | + expected: "map[key1:[value1] key2:[value2a value2b]]", |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "no keys", |
| 83 | + ctx: metadata.NewOutgoingContext(context.Background(), metadata.MD{}), |
| 84 | + expected: "map[]", |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "only one key", |
| 88 | + ctx: metadata.NewOutgoingContext(context.Background(), metadata.MD{ |
| 89 | + "key1": []string{"value1", "value1a"}, |
| 90 | + }), |
| 91 | + expected: "map[key1:[value1 value1a]]", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "no metadata", |
| 95 | + ctx: context.Background(), |
| 96 | + expected: "", |
| 97 | + }, |
| 98 | + } |
| 99 | + |
| 100 | + for _, tt := range tests { |
| 101 | + t.Run(tt.name, func(t *testing.T) { |
| 102 | + actual := streamKeyFromCtx(tt.ctx) |
| 103 | + if actual != tt.expected { |
| 104 | + t.Errorf("streamKeyFromCtx() = %v, expected %v", actual, tt.expected) |
| 105 | + } |
| 106 | + }) |
| 107 | + } |
| 108 | +} |
0 commit comments