attributes_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. package attributes_test
  19. import (
  20. "fmt"
  21. "testing"
  22. "google.golang.org/grpc/attributes"
  23. )
  24. type stringVal struct {
  25. s string
  26. }
  27. func (s stringVal) Equal(o interface{}) bool {
  28. os, ok := o.(stringVal)
  29. return ok && s.s == os.s
  30. }
  31. func ExampleAttributes() {
  32. type keyOne struct{}
  33. type keyTwo struct{}
  34. a := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"})
  35. fmt.Println("Key one:", a.Value(keyOne{}))
  36. fmt.Println("Key two:", a.Value(keyTwo{}))
  37. // Output:
  38. // Key one: 1
  39. // Key two: {two}
  40. }
  41. func ExampleAttributes_WithValue() {
  42. type keyOne struct{}
  43. type keyTwo struct{}
  44. a := attributes.New(keyOne{}, 1)
  45. a = a.WithValue(keyTwo{}, stringVal{s: "two"})
  46. fmt.Println("Key one:", a.Value(keyOne{}))
  47. fmt.Println("Key two:", a.Value(keyTwo{}))
  48. // Output:
  49. // Key one: 1
  50. // Key two: {two}
  51. }
  52. // Test that two attributes with the same content are Equal.
  53. func TestEqual(t *testing.T) {
  54. type keyOne struct{}
  55. type keyTwo struct{}
  56. a1 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"})
  57. a2 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"})
  58. if !a1.Equal(a2) {
  59. t.Fatalf("%+v.Equals(%+v) = false; want true", a1, a2)
  60. }
  61. if !a2.Equal(a1) {
  62. t.Fatalf("%+v.Equals(%+v) = false; want true", a2, a1)
  63. }
  64. }
  65. // Test that two attributes with different content are not Equal.
  66. func TestNotEqual(t *testing.T) {
  67. type keyOne struct{}
  68. type keyTwo struct{}
  69. a1 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "two"})
  70. a2 := attributes.New(keyOne{}, 2).WithValue(keyTwo{}, stringVal{s: "two"})
  71. a3 := attributes.New(keyOne{}, 1).WithValue(keyTwo{}, stringVal{s: "one"})
  72. if a1.Equal(a2) {
  73. t.Fatalf("%+v.Equals(%+v) = true; want false", a1, a2)
  74. }
  75. if a2.Equal(a1) {
  76. t.Fatalf("%+v.Equals(%+v) = true; want false", a2, a1)
  77. }
  78. if a3.Equal(a1) {
  79. t.Fatalf("%+v.Equals(%+v) = true; want false", a3, a1)
  80. }
  81. }