@@ -3,12 +3,12 @@ package cache_test
3
3
import (
4
4
"context"
5
5
"fmt"
6
- "reflect"
7
6
"testing"
8
7
"time"
9
8
10
9
"github.com/google/go-cmp/cmp"
11
10
"github.com/stretchr/testify/assert"
11
+ "github.com/stretchr/testify/require"
12
12
"google.golang.org/protobuf/testing/protocmp"
13
13
14
14
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
@@ -35,13 +35,14 @@ func TestSnapshotCacheDeltaWatch(t *testing.T) {
35
35
// Make our initial request as a wildcard to get all resources and make sure the wildcard requesting works as intended
36
36
for _ , typ := range testTypes {
37
37
watches [typ ] = make (chan cache.DeltaResponse , 1 )
38
+ state := stream .NewStreamState (true , nil )
38
39
c .CreateDeltaWatch (& discovery.DeltaDiscoveryRequest {
39
40
Node : & core.Node {
40
41
Id : "node" ,
41
42
},
42
43
TypeUrl : typ ,
43
44
ResourceNamesSubscribe : names [typ ],
44
- }, stream . NewStreamState ( true , nil ) , watches [typ ])
45
+ }, & state , watches [typ ])
45
46
}
46
47
47
48
if err := c .SetSnapshot (context .Background (), key , fixture .snapshot ()); err != nil {
@@ -69,15 +70,15 @@ func TestSnapshotCacheDeltaWatch(t *testing.T) {
69
70
watches [typ ] = make (chan cache.DeltaResponse , 1 )
70
71
state := stream .NewStreamState (false , versionMap [typ ])
71
72
for resource := range versionMap [typ ] {
72
- state .GetSubscribedResourceNames ()[resource ] = struct {}{}
73
+ state .GetSubscribedResources ()[resource ] = struct {}{}
73
74
}
74
75
c .CreateDeltaWatch (& discovery.DeltaDiscoveryRequest {
75
76
Node : & core.Node {
76
77
Id : "node" ,
77
78
},
78
79
TypeUrl : typ ,
79
80
ResourceNamesSubscribe : names [typ ],
80
- }, state , watches [typ ])
81
+ }, & state , watches [typ ])
81
82
}
82
83
83
84
if count := c .GetStatusInfo (key ).GetNumDeltaWatches (); count != len (testTypes ) {
@@ -123,12 +124,10 @@ func TestDeltaRemoveResources(t *testing.T) {
123
124
Id : "node" ,
124
125
},
125
126
TypeUrl : typ ,
126
- }, * streams [typ ], watches [typ ])
127
+ }, streams [typ ], watches [typ ])
127
128
}
128
129
129
- if err := c .SetSnapshot (context .Background (), key , fixture .snapshot ()); err != nil {
130
- t .Fatal (err )
131
- }
130
+ require .NoError (t , c .SetSnapshot (context .Background (), key , fixture .snapshot ()))
132
131
133
132
for _ , typ := range testTypes {
134
133
t .Run (typ , func (t * testing.T ) {
@@ -139,7 +138,7 @@ func TestDeltaRemoveResources(t *testing.T) {
139
138
nextVersionMap := out .GetNextVersionMap ()
140
139
streams [typ ].SetResourceVersions (nextVersionMap )
141
140
case <- time .After (time .Second ):
142
- t . Fatal ( "failed to receive a snapshot response" )
141
+ require . Fail ( t , "failed to receive a snapshot response" )
143
142
}
144
143
})
145
144
}
@@ -152,20 +151,17 @@ func TestDeltaRemoveResources(t *testing.T) {
152
151
Node : & core.Node {
153
152
Id : "node" ,
154
153
},
155
- TypeUrl : typ ,
156
- }, * streams [typ ], watches [typ ])
154
+ TypeUrl : typ ,
155
+ ResponseNonce : "nonce" ,
156
+ }, streams [typ ], watches [typ ])
157
157
}
158
158
159
- if count := c .GetStatusInfo (key ).GetNumDeltaWatches (); count != len (testTypes ) {
160
- t .Errorf ("watches should be created for the latest version, saw %d watches expected %d" , count , len (testTypes ))
161
- }
159
+ assert .Equal (t , len (testTypes ), c .GetStatusInfo (key ).GetNumDeltaWatches (), "watches should be created for the latest version" )
162
160
163
161
// set a partially versioned snapshot with no endpoints
164
162
snapshot2 := fixture .snapshot ()
165
163
snapshot2 .Resources [types .Endpoint ] = cache .NewResources (fixture .version2 , []types.Resource {})
166
- if err := c .SetSnapshot (context .Background (), key , snapshot2 ); err != nil {
167
- t .Fatal (err )
168
- }
164
+ require .NoError (t , c .SetSnapshot (context .Background (), key , snapshot2 ))
169
165
170
166
// validate response for endpoints
171
167
select {
@@ -176,11 +172,9 @@ func TestDeltaRemoveResources(t *testing.T) {
176
172
nextVersionMap := out .GetNextVersionMap ()
177
173
178
174
// make sure the version maps are different since we no longer are tracking any endpoint resources
179
- if reflect .DeepEqual (streams [testTypes [0 ]].GetResourceVersions (), nextVersionMap ) {
180
- t .Fatalf ("versionMap for the endpoint resource type did not change, received: %v, instead of an empty map" , nextVersionMap )
181
- }
175
+ require .Equal (t , nextVersionMap , streams [testTypes [0 ]].GetKnownResources (), "versionMap for the endpoint resource type did not change" )
182
176
case <- time .After (time .Second ):
183
- t . Fatal ( "failed to receive snapshot response" )
177
+ assert . Fail ( t , "failed to receive snapshot response" )
184
178
}
185
179
}
186
180
@@ -203,13 +197,14 @@ func TestConcurrentSetDeltaWatch(t *testing.T) {
203
197
t .Fatalf ("snapshot failed: %s" , err )
204
198
}
205
199
} else {
200
+ state := stream .NewStreamState (false , make (map [string ]string ))
206
201
cancel := c .CreateDeltaWatch (& discovery.DeltaDiscoveryRequest {
207
202
Node : & core.Node {
208
203
Id : id ,
209
204
},
210
205
TypeUrl : rsrc .EndpointType ,
211
206
ResourceNamesSubscribe : []string {clusterName },
212
- }, stream . NewStreamState ( false , make ( map [ string ] string )) , responses )
207
+ }, & state , responses )
213
208
214
209
defer cancel ()
215
210
}
@@ -226,14 +221,14 @@ func TestSnapshotDeltaCacheWatchTimeout(t *testing.T) {
226
221
// Create a non-buffered channel that will block sends.
227
222
watchCh := make (chan cache.DeltaResponse )
228
223
state := stream .NewStreamState (false , nil )
229
- state .SetSubscribedResourceNames (map [string ]struct {}{names [rsrc .EndpointType ][0 ]: {}})
224
+ state .SetSubscribedResources (map [string ]struct {}{names [rsrc .EndpointType ][0 ]: {}})
230
225
c .CreateDeltaWatch (& discovery.DeltaDiscoveryRequest {
231
226
Node : & core.Node {
232
227
Id : key ,
233
228
},
234
229
TypeUrl : rsrc .EndpointType ,
235
230
ResourceNamesSubscribe : names [rsrc .EndpointType ],
236
- }, state , watchCh )
231
+ }, & state , watchCh )
237
232
238
233
// The first time we set the snapshot without consuming from the blocking channel, so this should time out.
239
234
ctx , cancel := context .WithTimeout (context .Background (), time .Millisecond )
@@ -269,13 +264,14 @@ func TestSnapshotCacheDeltaWatchCancel(t *testing.T) {
269
264
c := cache .NewSnapshotCache (true , group {}, logger {t : t })
270
265
for _ , typ := range testTypes {
271
266
responses := make (chan cache.DeltaResponse , 1 )
267
+ state := stream .NewStreamState (false , make (map [string ]string ))
272
268
cancel := c .CreateDeltaWatch (& discovery.DeltaDiscoveryRequest {
273
269
Node : & core.Node {
274
270
Id : key ,
275
271
},
276
272
TypeUrl : typ ,
277
273
ResourceNamesSubscribe : names [typ ],
278
- }, stream . NewStreamState ( false , make ( map [ string ] string )) , responses )
274
+ }, & state , responses )
279
275
280
276
// Cancel the watch
281
277
cancel ()
0 commit comments