Skip to content

Commit

Permalink
Add check for empty json in test
Browse files Browse the repository at this point in the history
  • Loading branch information
zbud-msft committed Nov 5, 2024
1 parent ad4d6cf commit 58a5342
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
42 changes: 32 additions & 10 deletions gnmi_server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3584,28 +3584,40 @@ func TestNonExistentTableNoError(t *testing.T) {
go runServer(t, s)
defer s.ForceStop()

fileName := "../testdata/EMPTY_JSON.txt"
transceiverDomSensorTableByte, err := ioutil.ReadFile(fileName)
if err != nil {
t.Fatalf("read file %v err: %v", fileName, err)
}

var transceiverDomSensorTableJson interface{}
json.Unmarshal(transceiverDomSensorTableByte, &transceiverDomSensorTableJson)

tests := []struct {
desc string
q client.Query
want []client.Notification
poll int
desc string
q client.Query
wantNoti []client.Notification
poll int
}{
{
desc: "poll query for TRANSCEIVER_DOM_SENSOR",
poll: 10,
poll: 1,
q: client.Query{
Target: "STATE_DB",
Type: client.Poll,
Queries: []client.Path{{"TRANSCEIVER_DOM_SENSOR"}},
TLS: &tls.Config{InsecureSkipVerify: true},
},
want: []client.Notification{
client.Connected{},
client.Sync{},
wantNoti: []client.Notification{
client.Update{Path: []string{"TRANSCEIVER_DOM_SENSOR"}, TS: time.Unix(0, 200), Val: transceiverDomSensorTableJson},
client.Update{Path: []string{"TRANSCEIVER_DOM_SENSOR"}, TS: time.Unix(0, 200), Val: transceiverDomSensorTableJson},
},
},
}
namespace, _ := sdcfg.GetDbDefaultNamespace()
prepareStateDb(t, namespace)
var mutexNoti sync.Mutex

for _, tt := range tests {
prepareStateDb(t, namespace)
t.Run(tt.desc, func(t *testing.T) {
Expand All @@ -3614,12 +3626,12 @@ func TestNonExistentTableNoError(t *testing.T) {
c := client.New()
var gotNoti []client.Notification
q.NotificationHandler = func(n client.Notification) error {
mutexNoti.Lock()
if nn, ok := n.(client.Update); ok {
nn.TS = time.Unix(0, 200)
gotNoti = append(gotNoti, nn)
} else {
gotNoti = append(gotNoti, n)
}
mutexNoti.Unlock()
return nil
}

Expand All @@ -3641,10 +3653,20 @@ func TestNonExistentTableNoError(t *testing.T) {
}
}

mutexNoti.Lock()

if len(gotNoti) == 0 {
t.Errorf("expected non zero notifications")
}

if diff := pretty.Compare(tt.wantNoti, gotNoti); diff != "" {
t.Log("\n Want: \n", tt.wantNoti)
t.Log("\n Got: \n", gotNoti)
t.Errorf("unexpected updates: \n%s", diff)
}

mutexNoti.Unlock()

c.Close()
})
}
Expand Down
1 change: 1 addition & 0 deletions testdata/EMPTY_JSON.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 58a5342

Please sign in to comment.