123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- /*
- Copyright 2017 Google Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package client_test
- import (
- "errors"
- "fmt"
- "reflect"
- "testing"
- "time"
- "context"
- "notabug.org/themusicgod1/gnmi/client"
- fake "notabug.org/themusicgod1/gnmi/client/fake"
- )
- const (
- cacheTest = "cacheTest"
- cacheFail = "cacheFail"
- )
- func TestPollCache(t *testing.T) {
- tests := []struct {
- desc string
- q client.Query
- u [][]interface{}
- want []client.Leaves
- err bool
- }{{
- desc: "invalid query type",
- q: client.Query{
- Type: client.Once,
- Addrs: []string{"fake"},
- Queries: []client.Path{{"*"}},
- NotificationHandler: func(client.Notification) error { return nil },
- },
- u: [][]interface{}{{
- client.Update{TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
- client.Sync{},
- }, {}},
- want: []client.Leaves{{
- {TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
- }, {}},
- err: true,
- }, {
- desc: "Poll With Entry Test",
- q: client.Query{
- Type: client.Poll,
- Addrs: []string{"fake"},
- Queries: []client.Path{{"*"}},
- NotificationHandler: func(client.Notification) error { return nil },
- },
- u: [][]interface{}{{
- client.Update{TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
- client.Sync{},
- }, {
- client.Update{TS: time.Unix(3, 0), Path: client.Path{"a", "b"}, Val: 1},
- client.Sync{},
- }},
- want: []client.Leaves{{
- {TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
- }, {
- {TS: time.Unix(3, 0), Path: client.Path{"a", "b"}, Val: 1},
- }},
- }}
- for _, tt := range tests {
- t.Run(tt.desc, func(t *testing.T) {
- fake.Mock(cacheTest, tt.u[0])
- c := client.New()
- defer c.Close()
- if err := c.Subscribe(context.Background(), tt.q, cacheTest); err != nil {
- t.Errorf("Subscribe() failed: %v", err)
- }
- l := c.Leaves()
- if !reflect.DeepEqual(l, tt.want[0]) {
- t.Fatalf("Unexpected updates: got:\n%v\nwant:\n%v", l, tt.want[0])
- }
- impl, err := c.Impl()
- if err != nil {
- t.Fatalf("c.Impl: %v", err)
- }
- impl.(*fake.Client).Reset(tt.u[1])
- err = c.Poll()
- switch {
- case err != nil && tt.err:
- return
- case err != nil && !tt.err:
- t.Errorf("Poll() failed: %v", err)
- return
- case err == nil && tt.err:
- t.Errorf("Poll() expected error.")
- return
- }
- l = c.Leaves()
- if !reflect.DeepEqual(l, tt.want[1]) {
- t.Fatalf("Unexpected updates: got:\n%v\nwant:\n%v", l, tt.want[1])
- }
- })
- }
- }
- func TestCache(t *testing.T) {
- fake.Mock(cacheFail, []interface{}{errors.New("client failed")})
- nTest := false
- tests := []struct {
- desc string
- q client.Query
- u []interface{}
- clientType []string
- want client.Leaves
- err bool
- }{{
- desc: "Error New",
- clientType: []string{cacheFail},
- q: client.Query{
- Type: client.Once,
- Addrs: []string{"fake"},
- Queries: []client.Path{{"*"}},
- },
- want: nil,
- err: true,
- }, {
- desc: "Once Test",
- q: client.Query{
- Type: client.Once,
- Addrs: []string{"fake"},
- Queries: []client.Path{{"*"}},
- },
- u: []interface{}{
- client.Update{TS: time.Unix(1, 0), Path: client.Path{"a", "b"}, Val: 1},
- client.Delete{TS: time.Unix(2, 0), Path: client.Path{"a", "b"}},
- client.Sync{},
- },
- want: nil,
- }, {
- desc: "Once With Entry Test",
- q: client.Query{
- Type: client.Once,
- Addrs: []string{"fake"},
- Queries: []client.Path{{"*"}},
- },
- u: []interface{}{
- client.Update{TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
- client.Sync{},
- },
- want: client.Leaves{
- {TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
- },
- }, {
- desc: "Custom handler with Sync test",
- q: client.Query{
- Type: client.Once,
- Addrs: []string{"fake"},
- Queries: []client.Path{{"*"}},
- NotificationHandler: func(n client.Notification) error {
- if _, ok := n.(client.Sync); ok {
- nTest = true
- }
- return nil
- },
- },
- u: []interface{}{
- client.Update{TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
- client.Sync{},
- },
- want: client.Leaves{
- {TS: time.Unix(0, 0), Path: client.Path{"a", "b"}, Val: 1},
- },
- }, {
- desc: "Error on notification",
- q: client.Query{
- Type: client.Once,
- Addrs: []string{"fake"},
- Queries: []client.Path{{"*"}},
- },
- u: []interface{}{
- client.Error{},
- },
- err: true,
- }, {
- desc: "Error on Recv",
- q: client.Query{
- Type: client.Once,
- Addrs: []string{"fake"},
- Queries: []client.Path{{"*"}},
- },
- u: []interface{}{
- fmt.Errorf("Recv() error"),
- },
- err: true,
- }}
- for _, tt := range tests {
- t.Run(tt.desc, func(t *testing.T) {
- fake.Mock(cacheTest, tt.u)
- c := client.New()
- defer c.Close()
- if tt.q.NotificationHandler != nil {
- go func() {
- <-c.Synced()
- if !nTest {
- t.Errorf("Synced() failed: got %v, want true", nTest)
- }
- }()
- } else {
- tt.q.NotificationHandler = func(client.Notification) error { return nil }
- }
- clientType := []string{cacheTest}
- if tt.clientType != nil {
- clientType = tt.clientType
- }
- err := c.Subscribe(context.Background(), tt.q, clientType...)
- switch {
- case err != nil && tt.err:
- return
- case err != nil && !tt.err:
- t.Errorf("Subscribe() failed: %v", err)
- return
- case err == nil && tt.err:
- t.Errorf("Subscribe() expected error.")
- return
- }
- l := c.Leaves()
- if !reflect.DeepEqual(l, tt.want) {
- t.Fatalf("Unexpected updates: got:\n%v\nwant:\n%v", l, tt.want)
- }
- })
- }
- }
|