cache_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Copyright 2017 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package client_test
  14. import (
  15. "errors"
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "time"
  20. "context"
  21. "notabug.org/themusicgod1/gnmi/client"
  22. fake "notabug.org/themusicgod1/gnmi/client/fake"
  23. )
  24. const (
  25. cacheTest = "cacheTest"
  26. cacheFail = "cacheFail"
  27. )
  28. func TestPollCache(t *testing.T) {
  29. tests := []struct {
  30. desc string
  31. q client.Query
  32. u [][]interface{}
  33. want []client.Leaves
  34. err bool
  35. }{{
  36. desc: "invalid query type",
  37. q: client.Query{
  38. Type: client.Once,
  39. Addrs: []string{"fake"},
  40. Queries: []client.Path{{"*"}},
  41. NotificationHandler: func(client.Notification) error { return nil },
  42. },
  43. u: [][]interface{}{{
  44. client.Update{TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
  45. client.Sync{},
  46. }, {}},
  47. want: []client.Leaves{{
  48. {TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
  49. }, {}},
  50. err: true,
  51. }, {
  52. desc: "Poll With Entry Test",
  53. q: client.Query{
  54. Type: client.Poll,
  55. Addrs: []string{"fake"},
  56. Queries: []client.Path{{"*"}},
  57. NotificationHandler: func(client.Notification) error { return nil },
  58. },
  59. u: [][]interface{}{{
  60. client.Update{TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
  61. client.Sync{},
  62. }, {
  63. client.Update{TS: time.Unix(3, 0), Path: client.Path{"a", "b"}, Val: 1},
  64. client.Sync{},
  65. }},
  66. want: []client.Leaves{{
  67. {TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
  68. }, {
  69. {TS: time.Unix(3, 0), Path: client.Path{"a", "b"}, Val: 1},
  70. }},
  71. }}
  72. for _, tt := range tests {
  73. t.Run(tt.desc, func(t *testing.T) {
  74. fake.Mock(cacheTest, tt.u[0])
  75. c := client.New()
  76. defer c.Close()
  77. if err := c.Subscribe(context.Background(), tt.q, cacheTest); err != nil {
  78. t.Errorf("Subscribe() failed: %v", err)
  79. }
  80. l := c.Leaves()
  81. if !reflect.DeepEqual(l, tt.want[0]) {
  82. t.Fatalf("Unexpected updates: got:\n%v\nwant:\n%v", l, tt.want[0])
  83. }
  84. impl, err := c.Impl()
  85. if err != nil {
  86. t.Fatalf("c.Impl: %v", err)
  87. }
  88. impl.(*fake.Client).Reset(tt.u[1])
  89. err = c.Poll()
  90. switch {
  91. case err != nil && tt.err:
  92. return
  93. case err != nil && !tt.err:
  94. t.Errorf("Poll() failed: %v", err)
  95. return
  96. case err == nil && tt.err:
  97. t.Errorf("Poll() expected error.")
  98. return
  99. }
  100. l = c.Leaves()
  101. if !reflect.DeepEqual(l, tt.want[1]) {
  102. t.Fatalf("Unexpected updates: got:\n%v\nwant:\n%v", l, tt.want[1])
  103. }
  104. })
  105. }
  106. }
  107. func TestCache(t *testing.T) {
  108. fake.Mock(cacheFail, []interface{}{errors.New("client failed")})
  109. nTest := false
  110. tests := []struct {
  111. desc string
  112. q client.Query
  113. u []interface{}
  114. clientType []string
  115. want client.Leaves
  116. err bool
  117. }{{
  118. desc: "Error New",
  119. clientType: []string{cacheFail},
  120. q: client.Query{
  121. Type: client.Once,
  122. Addrs: []string{"fake"},
  123. Queries: []client.Path{{"*"}},
  124. },
  125. want: nil,
  126. err: true,
  127. }, {
  128. desc: "Once Test",
  129. q: client.Query{
  130. Type: client.Once,
  131. Addrs: []string{"fake"},
  132. Queries: []client.Path{{"*"}},
  133. },
  134. u: []interface{}{
  135. client.Update{TS: time.Unix(1, 0), Path: client.Path{"a", "b"}, Val: 1},
  136. client.Delete{TS: time.Unix(2, 0), Path: client.Path{"a", "b"}},
  137. client.Sync{},
  138. },
  139. want: nil,
  140. }, {
  141. desc: "Once With Entry Test",
  142. q: client.Query{
  143. Type: client.Once,
  144. Addrs: []string{"fake"},
  145. Queries: []client.Path{{"*"}},
  146. },
  147. u: []interface{}{
  148. client.Update{TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
  149. client.Sync{},
  150. },
  151. want: client.Leaves{
  152. {TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
  153. },
  154. }, {
  155. desc: "Custom handler with Sync test",
  156. q: client.Query{
  157. Type: client.Once,
  158. Addrs: []string{"fake"},
  159. Queries: []client.Path{{"*"}},
  160. NotificationHandler: func(n client.Notification) error {
  161. if _, ok := n.(client.Sync); ok {
  162. nTest = true
  163. }
  164. return nil
  165. },
  166. },
  167. u: []interface{}{
  168. client.Update{TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
  169. client.Sync{},
  170. },
  171. want: client.Leaves{
  172. {TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
  173. },
  174. }, {
  175. desc: "Error on notification",
  176. q: client.Query{
  177. Type: client.Once,
  178. Addrs: []string{"fake"},
  179. Queries: []client.Path{{"*"}},
  180. },
  181. u: []interface{}{
  182. client.Error{},
  183. },
  184. err: true,
  185. }, {
  186. desc: "Error on Recv",
  187. q: client.Query{
  188. Type: client.Once,
  189. Addrs: []string{"fake"},
  190. Queries: []client.Path{{"*"}},
  191. },
  192. u: []interface{}{
  193. fmt.Errorf("Recv() error"),
  194. },
  195. err: true,
  196. }}
  197. for _, tt := range tests {
  198. t.Run(tt.desc, func(t *testing.T) {
  199. fake.Mock(cacheTest, tt.u)
  200. c := client.New()
  201. defer c.Close()
  202. if tt.q.NotificationHandler != nil {
  203. go func() {
  204. <-c.Synced()
  205. if !nTest {
  206. t.Errorf("Synced() failed: got %v, want true", nTest)
  207. }
  208. }()
  209. } else {
  210. tt.q.NotificationHandler = func(client.Notification) error { return nil }
  211. }
  212. clientType := []string{cacheTest}
  213. if tt.clientType != nil {
  214. clientType = tt.clientType
  215. }
  216. err := c.Subscribe(context.Background(), tt.q, clientType...)
  217. switch {
  218. case err != nil && tt.err:
  219. return
  220. case err != nil && !tt.err:
  221. t.Errorf("Subscribe() failed: %v", err)
  222. return
  223. case err == nil && tt.err:
  224. t.Errorf("Subscribe() expected error.")
  225. return
  226. }
  227. l := c.Leaves()
  228. if !reflect.DeepEqual(l, tt.want) {
  229. t.Fatalf("Unexpected updates: got:\n%v\nwant:\n%v", l, tt.want)
  230. }
  231. })
  232. }
  233. }