google.golang.org/grpc@v1.62.1/attributes/attributes_test.go (about) 1 /* 2 * 3 * Copyright 2019 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package attributes_test 20 21 import ( 22 "fmt" 23 "testing" 24 25 "google.golang.org/grpc/attributes" 26 ) 27 28 type stringVal struct { 29 s string 30 } 31 32 func (s stringVal) Equal(o any) bool { 33 os, ok := o.(stringVal) 34 return ok && s.s == os.s 35 } 36 37 type stringerVal struct { 38 s string 39 } 40 41 func (s stringerVal) String() string { 42 return s.s 43 } 44 45 func ExampleAttributes() { 46 type keyOne struct{} 47 type keyTwo struct{} 48 a := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"}) 49 fmt.Println("Key one:", a.Value(keyOne{})) 50 fmt.Println("Key two:", a.Value(keyTwo{})) 51 // Output: 52 // Key one: 1 53 // Key two: {two} 54 } 55 56 func ExampleAttributes_WithValue() { 57 type keyOne struct{} 58 type keyTwo struct{} 59 a := attributes.New(keyOne{}, 1) 60 a = a.WithValue(keyTwo{}, stringVal{s: "two"}) 61 fmt.Println("Key one:", a.Value(keyOne{})) 62 fmt.Println("Key two:", a.Value(keyTwo{})) 63 // Output: 64 // Key one: 1 65 // Key two: {two} 66 } 67 68 func ExampleAttributes_String() { 69 type key struct{} 70 var typedNil *stringerVal 71 a1 := attributes.New(key{}, typedNil) // typed nil implements [fmt.Stringer] 72 a2 := attributes.New(key{}, (*stringerVal)(nil)) // typed nil implements [fmt.Stringer] 73 a3 := attributes.New(key{}, (*stringVal)(nil)) // typed nil not implements [fmt.Stringer] 74 a4 := attributes.New(key{}, nil) // untyped nil 75 a5 := attributes.New(key{}, 1) 76 a6 := attributes.New(key{}, stringerVal{s: "two"}) 77 a7 := attributes.New(key{}, stringVal{s: "two"}) 78 a8 := attributes.New(1, true) 79 fmt.Println("a1:", a1.String()) 80 fmt.Println("a2:", a2.String()) 81 fmt.Println("a3:", a3.String()) 82 fmt.Println("a4:", a4.String()) 83 fmt.Println("a5:", a5.String()) 84 fmt.Println("a6:", a6.String()) 85 fmt.Println("a7:", a7.String()) 86 fmt.Println("a8:", a8.String()) 87 // Output: 88 // a1: {"<%!p(attributes_test.key={})>": "<nil>" } 89 // a2: {"<%!p(attributes_test.key={})>": "<nil>" } 90 // a3: {"<%!p(attributes_test.key={})>": "<0x0>" } 91 // a4: {"<%!p(attributes_test.key={})>": "<%!p(<nil>)>" } 92 // a5: {"<%!p(attributes_test.key={})>": "<%!p(int=1)>" } 93 // a6: {"<%!p(attributes_test.key={})>": "two" } 94 // a7: {"<%!p(attributes_test.key={})>": "<%!p(attributes_test.stringVal={two})>" } 95 // a8: {"<%!p(int=1)>": "<%!p(bool=true)>" } 96 } 97 98 // Test that two attributes with the same content are Equal. 99 func TestEqual(t *testing.T) { 100 type keyOne struct{} 101 type keyTwo struct{} 102 a1 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"}) 103 a2 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"}) 104 if !a1.Equal(a2) { 105 t.Fatalf("%+v.Equals(%+v) = false; want true", a1, a2) 106 } 107 if !a2.Equal(a1) { 108 t.Fatalf("%+v.Equals(%+v) = false; want true", a2, a1) 109 } 110 } 111 112 // Test that two attributes with different content are not Equal. 113 func TestNotEqual(t *testing.T) { 114 type keyOne struct{} 115 type keyTwo struct{} 116 a1 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"}) 117 a2 := attributes.New(keyOne{}, 2).WithValue(keyTwo{}, stringVal{s: "two"}) 118 a3 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "one"}) 119 if a1.Equal(a2) { 120 t.Fatalf("%+v.Equals(%+v) = true; want false", a1, a2) 121 } 122 if a2.Equal(a1) { 123 t.Fatalf("%+v.Equals(%+v) = true; want false", a2, a1) 124 } 125 if a3.Equal(a1) { 126 t.Fatalf("%+v.Equals(%+v) = true; want false", a3, a1) 127 } 128 }