tests: Migrate watch test to common framework

Signed-off-by: nic-chen <chenjunxu6@gmail.com>
This commit is contained in:
nic-chen
2022-08-15 10:06:31 +08:00
parent a1405e9633
commit f6ed36372b
10 changed files with 201 additions and 44 deletions

View File

@ -15,6 +15,9 @@
package testutils
import (
"errors"
"time"
clientv3 "go.etcd.io/etcd/client/v3"
)
@ -35,3 +38,26 @@ func KeyValuesFromGetResponse(resp *clientv3.GetResponse) (kvs []KV) {
}
return kvs
}
func KeyValuesFromWatchResponse(resp clientv3.WatchResponse) (kvs []KV) {
for _, event := range resp.Events {
kvs = append(kvs, KV{Key: string(event.Kv.Key), Val: string(event.Kv.Value)})
}
return kvs
}
func KeyValuesFromWatchChan(wch clientv3.WatchChan, wantedLen int, timeout time.Duration) (kvs []KV, err error) {
for {
select {
case watchResp, ok := <-wch:
if ok {
kvs = append(kvs, KeyValuesFromWatchResponse(watchResp)...)
if len(kvs) == wantedLen {
return kvs, nil
}
}
case <-time.After(timeout):
return nil, errors.New("closed watcher channel should not block")
}
}
}