@@ -18,17 +18,22 @@ import (
18
18
"context"
19
19
"errors"
20
20
"fmt"
21
+ "time"
21
22
22
23
"go.uber.org/zap"
23
24
"golang.org/x/sync/errgroup"
24
25
25
26
"go.etcd.io/etcd/tests/v3/robustness/report"
26
27
)
27
28
29
+ var watchEventCollectionTimeout = time .Second
30
+
28
31
func CollectClusterWatchEvents (ctx context.Context , lg * zap.Logger , endpoints []string , maxRevisionChan <- chan int64 , cfg WatchConfig , clientSet * ClientSet ) error {
29
32
var g errgroup.Group
30
33
reports := make ([]report.ClientReport , len (endpoints ))
31
34
memberMaxRevisionChans := make ([]chan int64 , len (endpoints ))
35
+ ctx , cancel := context .WithCancel (ctx )
36
+ defer cancel ()
32
37
for i , endpoint := range endpoints {
33
38
memberMaxRevisionChan := make (chan int64 , 1 )
34
39
memberMaxRevisionChans [i ] = memberMaxRevisionChan
@@ -49,6 +54,8 @@ func CollectClusterWatchEvents(ctx context.Context, lg *zap.Logger, endpoints []
49
54
for _ , memberChan := range memberMaxRevisionChans {
50
55
memberChan <- maxRevision
51
56
}
57
+ time .Sleep (watchEventCollectionTimeout )
58
+ cancel ()
52
59
return nil
53
60
})
54
61
return g .Wait ()
@@ -62,19 +69,27 @@ type WatchConfig struct {
62
69
func watchUntilRevision (ctx context.Context , lg * zap.Logger , c * RecordingClient , maxRevisionChan <- chan int64 , cfg WatchConfig ) error {
63
70
var maxRevision int64
64
71
var lastRevision int64 = 1
65
- var closing bool
66
72
ctx , cancel := context .WithCancel (ctx )
67
73
defer cancel ()
68
74
resetWatch:
69
75
for {
70
- if closing {
76
+ select {
77
+ case <- ctx .Done ():
78
+ select {
79
+ case revision , ok := <- maxRevisionChan :
80
+ if ok {
81
+ maxRevision = revision
82
+ }
83
+ default :
84
+ }
71
85
if maxRevision == 0 {
72
86
return errors .New ("Client didn't collect all events, max revision not set" )
73
87
}
74
88
if lastRevision < maxRevision {
75
- return fmt .Errorf ("Client didn't collect all events, got: %d, expected: %d" , lastRevision , maxRevision )
89
+ return fmt .Errorf ("Client didn't collect all events, revision got: %d, expected: %d" , lastRevision , maxRevision )
76
90
}
77
91
return nil
92
+ default :
78
93
}
79
94
watch := c .Watch (ctx , "" , lastRevision + 1 , true , true , false )
80
95
for {
@@ -83,13 +98,11 @@ resetWatch:
83
98
if ok {
84
99
maxRevision = revision
85
100
if lastRevision >= maxRevision {
86
- closing = true
87
101
cancel ()
88
102
}
89
103
} else {
90
104
// Only cancel if maxRevision was never set.
91
105
if maxRevision == 0 {
92
- closing = true
93
106
cancel ()
94
107
}
95
108
}
@@ -115,7 +128,6 @@ resetWatch:
115
128
lastRevision = resp .Events [len (resp .Events )- 1 ].Kv .ModRevision
116
129
}
117
130
if maxRevision != 0 && lastRevision >= maxRevision {
118
- closing = true
119
131
cancel ()
120
132
}
121
133
}
0 commit comments