Skip to content

Commit

Permalink
Merge pull request #132 from openconfig/batch-map
Browse files Browse the repository at this point in the history
Support batch on map root
  • Loading branch information
wenovus authored Oct 16, 2023
2 parents e6330a1 + 2f84a19 commit a0ee6ed
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 21 deletions.
10 changes: 5 additions & 5 deletions ygnmi/ygnmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,13 +753,13 @@ func (b *Batch[T]) Query() SingletonQuery[T] {
return NewSingletonQuery[T](
b.root.dirName(),
b.root.IsState(),
false,
false,
b.root.isLeaf(),
b.root.isScalar(),
b.root.isCompressedSchema(),
false,
b.root.isListContainer(),
b.root.PathStruct(),
nil,
nil,
b.root.extract,
b.root.goStruct,
b.root.schema,
queryPaths,
b.root.compressInfo(),
Expand Down
117 changes: 101 additions & 16 deletions ygnmi/ygnmi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func getCheckFn[T any](t *testing.T, fakeGNMI *testutil.FakeGNMI, c *ygnmi.Clien
}

func watchCheckFn[T any](t *testing.T, fakeGNMI *testutil.FakeGNMI, duration time.Duration, c *ygnmi.Client, inQuery ygnmi.SingletonQuery[T],
inOpts []ygnmi.Option, valPred func(T) bool, wantErrSubstring string, wantSubscriptionPath *gpb.Path, wantMode gpb.SubscriptionMode, wantInterval uint64, wantVals []*ygnmi.Value[T], wantLastVal *ygnmi.Value[T]) {
inOpts []ygnmi.Option, valPred func(T) bool, wantErrSubstring string, wantSubscriptionPaths []*gpb.Path, wantModes []gpb.SubscriptionMode, wantIntervals []uint64, wantVals []*ygnmi.Value[T], wantLastVal *ygnmi.Value[T]) {
t.Helper()
i := 0
ctx, cancel := context.WithTimeout(context.Background(), duration)
Expand Down Expand Up @@ -191,9 +191,9 @@ func watchCheckFn[T any](t *testing.T, fakeGNMI *testutil.FakeGNMI, duration tim
if err != nil {
return
}
verifySubscriptionPathsSent(t, fakeGNMI, wantSubscriptionPath)
verifySubscriptionModesSent(t, fakeGNMI, wantMode)
verifySubscriptionSampleIntervalsSent(t, fakeGNMI, wantInterval)
verifySubscriptionPathsSent(t, fakeGNMI, wantSubscriptionPaths...)
verifySubscriptionModesSent(t, fakeGNMI, wantModes...)
verifySubscriptionSampleIntervalsSent(t, fakeGNMI, wantIntervals...)
if val != nil {
checkJustReceived(t, val.RecvTimestamp)
wantLastVal.RecvTimestamp = val.RecvTimestamp
Expand Down Expand Up @@ -1354,9 +1354,9 @@ func TestWatch(t *testing.T) {
tt.opts,
func(val string) bool { return val == "foo" },
tt.wantErr,
tt.wantSubscriptionPath,
tt.wantMode,
tt.wantInterval,
[]*gpb.Path{tt.wantSubscriptionPath},
[]gpb.SubscriptionMode{tt.wantMode},
[]uint64{tt.wantInterval},
tt.wantVals,
tt.wantLastVal,
)
Expand Down Expand Up @@ -1586,9 +1586,9 @@ func TestWatch(t *testing.T) {
return val.One != nil && *val.One == "foo" && val.Three == exampleoc.Child_Three_ONE
},
tt.wantErr,
tt.wantSubscriptionPath,
gpb.SubscriptionMode_TARGET_DEFINED,
0,
[]*gpb.Path{tt.wantSubscriptionPath},
[]gpb.SubscriptionMode{gpb.SubscriptionMode_TARGET_DEFINED},
[]uint64{0},
tt.wantVals,
tt.wantLastVal,
)
Expand Down Expand Up @@ -1661,9 +1661,9 @@ func TestWatch(t *testing.T) {
return cmp.Equal(val, want, cmp.AllowUnexported(exampleoc.Model_SingleKey_OrderedList_OrderedMap{}))
},
"",
testutil.GNMIPath(t, "/model/a/single-key[key=foo]/ordered-lists"),
gpb.SubscriptionMode_TARGET_DEFINED,
0,
[]*gpb.Path{testutil.GNMIPath(t, "/model/a/single-key[key=foo]/ordered-lists")},
[]gpb.SubscriptionMode{gpb.SubscriptionMode_TARGET_DEFINED},
[]uint64{0},
[]*ygnmi.Value[*exampleoc.Model_SingleKey_OrderedList_OrderedMap]{
(&ygnmi.Value[*exampleoc.Model_SingleKey_OrderedList_OrderedMap]{
Path: testutil.GNMIPath(t, "/model/a/single-key[key=foo]/ordered-lists"),
Expand Down Expand Up @@ -1745,9 +1745,9 @@ func TestWatch(t *testing.T) {
return cmp.Equal(val, want)
},
"",
testutil.GNMIPath(t, "/model/a"),
gpb.SubscriptionMode_TARGET_DEFINED,
0,
[]*gpb.Path{testutil.GNMIPath(t, "/model/a")},
[]gpb.SubscriptionMode{gpb.SubscriptionMode_TARGET_DEFINED},
[]uint64{0},
[]*ygnmi.Value[map[string]*exampleoc.Model_SingleKey]{
(&ygnmi.Value[map[string]*exampleoc.Model_SingleKey]{
Path: testutil.GNMIPath(t, "/model/a"),
Expand Down Expand Up @@ -4287,6 +4287,91 @@ func TestCustomRootBatch(t *testing.T) {
})
}

fakeGNMI, client := newClient(t)
startTime := time.Now()
t.Run("success whole single-keyed map", func(t *testing.T) {
fakeGNMI.Stub().Notification(&gpb.Notification{
Timestamp: startTime.UnixNano(),
Prefix: testutil.GNMIPath(t, "/model/a"),
Update: []*gpb.Update{{
Path: testutil.GNMIPath(t, `single-key[key=foo]/state/key`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_StringVal{StringVal: "foo"}},
}, {
Path: testutil.GNMIPath(t, `single-key[key=foo]/state/value`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_IntVal{IntVal: 42}},
}, {
Path: testutil.GNMIPath(t, `single-key[key=bar]/state/key`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_StringVal{StringVal: "bar"}},
}, {
Path: testutil.GNMIPath(t, `single-key[key=bar]/state/value`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_IntVal{IntVal: 43}},
}},
}).Sync().Notification(&gpb.Notification{
Timestamp: startTime.Add(time.Millisecond).UnixNano(),
Prefix: testutil.GNMIPath(t, "/model/a"),
Update: []*gpb.Update{{
Path: testutil.GNMIPath(t, `single-key[key=foo]/state/key`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_StringVal{StringVal: "foo"}},
}, {
Path: testutil.GNMIPath(t, `single-key[key=foo]/state/value`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_IntVal{IntVal: 42}},
}, {
Path: testutil.GNMIPath(t, `single-key[key=bar]/state/key`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_StringVal{StringVal: "bar"}},
}, {
Path: testutil.GNMIPath(t, `single-key[key=bar]/state/value`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_IntVal{IntVal: 43}},
}, {
Path: testutil.GNMIPath(t, `single-key[key=baz]/state/key`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_StringVal{StringVal: "baz"}},
}, {
Path: testutil.GNMIPath(t, `single-key[key=baz]/state/value`),
Val: &gpb.TypedValue{Value: &gpb.TypedValue_IntVal{IntVal: 44}},
}},
})

modelPath := exampleocpath.Root().Model()
b := ygnmi.NewBatch(modelPath.SingleKeyMap().State())
if err := b.AddPaths(
modelPath.SingleKeyAny().Key().State().PathStruct(),
modelPath.SingleKeyAny().Value().State().PathStruct(),
); err != nil {
t.Fatal(err)
}

want := getSampleSingleKeyedMap(t)
watchCheckFn(t, fakeGNMI, 2*time.Second, client,
b.Query(),
nil,
func(val map[string]*exampleoc.Model_SingleKey) bool {
return cmp.Equal(val, want)
},
"",
[]*gpb.Path{
testutil.GNMIPath(t, "/model/a/single-key[key=*]/state/key"),
testutil.GNMIPath(t, "/model/a/single-key[key=*]/state/value"),
},
[]gpb.SubscriptionMode{
gpb.SubscriptionMode_TARGET_DEFINED,
gpb.SubscriptionMode_TARGET_DEFINED,
},
[]uint64{0, 0},
[]*ygnmi.Value[map[string]*exampleoc.Model_SingleKey]{
(&ygnmi.Value[map[string]*exampleoc.Model_SingleKey]{
Path: testutil.GNMIPath(t, "/model/a"),
Timestamp: startTime,
}).SetVal(getSampleSingleKeyedMapIncomplete(t)),
(&ygnmi.Value[map[string]*exampleoc.Model_SingleKey]{
Path: testutil.GNMIPath(t, "/model/a"),
Timestamp: startTime.Add(time.Millisecond),
}).SetVal(getSampleSingleKeyedMap(t)),
},
(&ygnmi.Value[map[string]*exampleoc.Model_SingleKey]{
Path: testutil.GNMIPath(t, "/model/a"),
Timestamp: startTime.Add(time.Millisecond),
}).SetVal(getSampleSingleKeyedMap(t)),
)
})
}

func TestSetBatch(t *testing.T) {
Expand Down

0 comments on commit a0ee6ed

Please sign in to comment.