Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

receipt: dequeue not returning watch err #17198

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
receipt: dequeue not returning watch err
the error from watch is not returned, if the client token expired, the `ev` return by watch will be nil, therefore the Deque method will panic in such case.

Signed-off-by: guokairui <wxsms@foxmail.com>
  • Loading branch information
wxsms committed Jan 10, 2024
commit 4b41fcc17fc16b08fe81772c0d7aa466e82833c3
1 change: 1 addition & 0 deletions client/v3/experimental/recipes/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var (
ErrWaitMismatch = errors.New("unexpected wait result")
ErrTooManyClients = errors.New("too many clients")
ErrNoWatcher = errors.New("no watcher channel")
ErrWatchFailed = errors.New("watch failed")
)

// deleteRevKey deletes a key by revision, returning false if key is missing
Expand Down
13 changes: 8 additions & 5 deletions client/v3/experimental/recipes/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func WaitEvents(c *clientv3.Client, key string, rev int64, evs []mvccpb.Event_Ev
if wc == nil {
return nil, ErrNoWatcher
}
return waitEvents(wc, evs), nil
return waitEvents(wc, evs)
}

func WaitPrefixEvents(c *clientv3.Client, prefix string, rev int64, evs []mvccpb.Event_EventType) (*clientv3.Event, error) {
Expand All @@ -39,20 +39,23 @@ func WaitPrefixEvents(c *clientv3.Client, prefix string, rev int64, evs []mvccpb
if wc == nil {
return nil, ErrNoWatcher
}
return waitEvents(wc, evs), nil
return waitEvents(wc, evs)
}

func waitEvents(wc clientv3.WatchChan, evs []mvccpb.Event_EventType) *clientv3.Event {
func waitEvents(wc clientv3.WatchChan, evs []mvccpb.Event_EventType) (*clientv3.Event, error) {
i := 0
for wresp := range wc {
if wresp.Canceled {
return nil, wresp.Err()
}
for _, ev := range wresp.Events {
if ev.Type == evs[i] {
i++
if i == len(evs) {
return ev
return ev, nil
}
}
}
}
return nil
return nil, ErrWatchFailed
}
23 changes: 23 additions & 0 deletions tests/integration/clientv3/experimental/recipes/v3_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ package recipes_test
import (
"fmt"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"

recipe "go.etcd.io/etcd/client/v3/experimental/recipes"
integration2 "go.etcd.io/etcd/tests/v3/framework/integration"
Expand Down Expand Up @@ -78,6 +80,27 @@ func TestQueueManyReaderManyWriter(t *testing.T) {
testQueueNReaderMWriter(t, manyQueueClients, manyQueueClients)
}

// TestQueueAuthTokenExpired queue should return err on watch failed
func TestQueueWatchError(t *testing.T) {
integration2.BeforeTest(t)
clus := integration2.NewCluster(t, &integration2.ClusterConfig{Size: 1})
etcdc := clus.RandClient()
queue := recipe.NewQueue(etcdc, "testq")
var wg sync.WaitGroup
wg.Add(1)
go func() {
_, err := queue.Dequeue()
if err != recipe.ErrWatchFailed {
t.Errorf("expect ErrWatchFailed, got %v", err)
}
wg.Done()
}()
time.Sleep(1 * time.Second)
// queue.Enqueue("1")
clus.Terminate(t)
wg.Wait()
}

// BenchmarkQueue benchmarks Queues using many/many readers/writers
func BenchmarkQueue(b *testing.B) {
integration2.BeforeTest(b)
Expand Down
Loading