本文整理汇总了Golang中github.com/contiv/netplugin/core.StateDriver.WatchAllState方法的典型用法代码示例。如果您正苦于以下问题:Golang StateDriver.WatchAllState方法的具体用法?Golang StateDriver.WatchAllState怎么用?Golang StateDriver.WatchAllState使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/contiv/netplugin/core.StateDriver
的用法示例。
在下文中一共展示了StateDriver.WatchAllState方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: commonTestStateDriverWatchAllStateModify
func commonTestStateDriverWatchAllStateModify(t *testing.T, d core.StateDriver) {
state := &testState{IntField: 1234, StrField: "testString"}
modState := &testState{IntField: 5678, StrField: "modString"}
baseKey := "modify"
key := baseKey + "/testKeyWatchAll"
err := d.WriteState(key, state, json.Marshal)
if err != nil {
t.Fatalf("failed to write state. Error: %s", err)
}
defer func() {
d.ClearState(key)
}()
recvErr := make(chan error, 1)
stateCh := make(chan core.WatchState, 1)
timer := time.After(waitTimeout)
go func(rsps chan core.WatchState, retErr chan error) {
err := d.WatchAllState(baseKey, state, json.Unmarshal, stateCh)
if err != nil {
retErr <- err
return
}
}(stateCh, recvErr)
// trigger modify after a slight pause to ensure that events are not missed
time.Sleep(time.Second)
err = d.WriteState(key, modState, json.Marshal)
if err != nil {
t.Fatalf("failed to write state. Error: %s", err)
}
for_label:
for {
select {
case watchState := <-stateCh:
s := watchState.Curr.(*testState)
if s.IntField != modState.IntField || s.StrField != modState.StrField {
t.Fatalf("Watch state mismatch. Expctd: %+v, Rcvd: %+v", modState, s)
}
if watchState.Prev == nil {
t.Fatalf("Received a modify event without previous state value set.")
}
s = watchState.Prev.(*testState)
if s.IntField != state.IntField || s.StrField != state.StrField {
t.Fatalf("Watch state mismatch. Expctd: %+v, Rcvd: %+v", state, s)
}
break for_label
case err := <-recvErr:
t.Fatalf("Watch failed. Error: %s", err)
break for_label
case <-timer:
t.Fatalf("timed out waiting for events")
break for_label
}
}
}