-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
984 lines (840 loc) · 31.4 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
package diderot
import (
"context"
"crypto/rand"
"encoding/json"
"net"
"os"
"sync"
"sync/atomic"
"testing"
"time"
core "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
endpoint "github.com/envoyproxy/go-control-plane/envoy/config/endpoint/v3"
discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
"github.com/linkedin/diderot/ads"
internal "github.com/linkedin/diderot/internal/server"
"github.com/linkedin/diderot/internal/utils"
serverstats "github.com/linkedin/diderot/stats/server"
"github.com/linkedin/diderot/testutils"
"github.com/stretchr/testify/require"
"google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/xds"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/wrapperspb"
)
var (
badTypeURL = "foobar"
badResources = []string{"badResource1", "badResource2"}
controlPlane = &core.ControlPlane{Identifier: "fooBar"}
)
type serverStatsHandler struct {
UnknownTypes atomic.Int64
UnknownResources atomic.Int64
NACKsReceived atomic.Int64
ACKsReceived atomic.Int64
}
func (m *serverStatsHandler) HandleServerEvent(ctx context.Context, event serverstats.Event) {
switch e := event.(type) {
case *serverstats.RequestReceived:
if e.IsNACK {
m.NACKsReceived.Add(1)
}
if e.IsACK {
m.ACKsReceived.Add(1)
}
if e.IsRequestedTypeUnknown {
m.UnknownTypes.Add(1)
}
case *serverstats.ResourceQueued:
if !e.ResourceExists {
m.UnknownResources.Add(1)
}
}
}
func (m *serverStatsHandler) reset() {
m.UnknownTypes.Store(0)
m.UnknownResources.Store(0)
m.NACKsReceived.Store(0)
m.ACKsReceived.Store(0)
}
type testLocator struct {
t *testing.T
node *ads.Node
caches map[string]RawCache
}
func (tl *testLocator) checkContextNode(streamCtx context.Context) {
if tl.node == nil {
// skip checking node if nil
return
}
node, ok := NodeFromContext(streamCtx)
require.True(tl.t, ok)
testutils.ProtoEquals(tl.t, tl.node, node)
}
func (tl *testLocator) IsTypeSupported(streamCtx context.Context, typeURL string) bool {
tl.checkContextNode(streamCtx)
_, ok := tl.caches[typeURL]
return ok
}
func (tl *testLocator) Subscribe(
streamCtx context.Context,
typeURL, resourceName string,
handler ads.RawSubscriptionHandler,
) (unsubscribe func()) {
tl.checkContextNode(streamCtx)
c := tl.caches[typeURL]
Subscribe(c, resourceName, handler)
return func() {
Unsubscribe(c, resourceName, handler)
}
}
func (tl *testLocator) Resubscribe(
streamCtx context.Context,
typeURL, resourceName string,
handler ads.RawSubscriptionHandler,
) {
tl.checkContextNode(streamCtx)
Subscribe(tl.caches[typeURL], resourceName, handler)
}
func newTestLocator(t *testing.T, node *ads.Node, types ...Type) *testLocator {
tl := &testLocator{
t: t,
node: node,
caches: make(map[string]RawCache),
}
for _, tpe := range types {
tl.caches[tpe.URL()] = tpe.NewCache()
}
return tl
}
func getCache[T proto.Message](tl *testLocator) Cache[T] {
return tl.caches[TypeOf[T]().URL()].(Cache[T])
}
func TestEndToEnd(t *testing.T) {
locator := newTestLocator(
t,
&ads.Node{
Id: "diderot-test",
UserAgentName: "gRPC Go",
UserAgentVersionType: &core.Node_UserAgentVersion{UserAgentVersion: grpc.Version},
ClientFeatures: []string{
"envoy.lb.does_not_support_overprovisioning",
"xds.config.resource-in-sotw",
},
},
TypeOf[*ads.Endpoint](),
TypeOf[*ads.Cluster](),
TypeOf[*ads.Route](),
TypeOf[*ads.Listener](),
TypeOf[*wrapperspb.BytesValue](),
)
endpointCache := getCache[*ads.Endpoint](locator)
listenerCache := getCache[*ads.Listener](locator)
bytesCache := getCache[*wrapperspb.BytesValue](locator)
ts := testutils.NewTestGRPCServer(t)
resources := readResourcesFromJSONFile(t, "test_xds_config.json")
require.Len(t, resources, 3)
for _, r := range resources {
c, ok := locator.caches[r.Resource.TypeUrl]
require.Truef(t, ok, "Unknown type loaded from test config %q: %+v", r.Resource.TypeUrl, r)
require.NoError(t, c.SetRaw(r, time.Now()))
}
addr := ts.Addr().(*net.TCPAddr)
endpointCache.Set(
"testADSServer",
"0",
&ads.Endpoint{
ClusterName: "testADSServer",
Endpoints: []*endpoint.LocalityLbEndpoints{{
Locality: new(core.Locality),
LoadBalancingWeight: wrapperspb.UInt32(1),
LbEndpoints: []*endpoint.LbEndpoint{{
HostIdentifier: &endpoint.LbEndpoint_Endpoint{
Endpoint: &endpoint.Endpoint{
Address: &core.Address{
Address: &core.Address_SocketAddress{
SocketAddress: &core.SocketAddress{
Protocol: core.SocketAddress_TCP,
Address: addr.IP.String(),
PortSpecifier: &core.SocketAddress_PortValue{PortValue: uint32(addr.Port)},
},
},
},
Hostname: "localhost",
},
},
}},
}},
},
time.Now(),
)
statsHandler := new(serverStatsHandler)
s := NewADSServer(
locator,
WithGranularResponseRateLimit(0),
WithGlobalResponseRateLimit(0),
WithServerStatsHandler(statsHandler),
WithControlPlane(controlPlane),
)
discovery.RegisterAggregatedDiscoveryServiceServer(ts.Server, s)
ts.Start()
xdsResolverBuilder, err := xds.NewXDSResolverWithConfigForTesting([]byte(`{
"xds_servers": [
{
"server_uri": "` + ts.AddrString() + `",
"channel_creds": [{"type": "insecure"}],
"server_features": ["xds_v3"]
}
],
"node": { "id": "diderot-test" }
}`))
require.NoError(t, err)
conn, err := grpc.NewClient(
"xds:///testADSServer",
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithResolvers(xdsResolverBuilder),
)
require.NoError(t, err)
client := discovery.NewAggregatedDiscoveryServiceClient(conn)
t.Run("xDS sanity check", func(t *testing.T) {
// It's expected that the xDS client will have ACKed the responses it received from the server during
// the LDS -> RDS -> CDS -> EDS flow. However, since grpc.NewClient does not actually attempt to
// establish a connection until the very last moment, testing this actually requires opening a
// stream. This test opens a stream, checks the ACK counter then closes it.
stream, err := client.DeltaAggregatedResources(testutils.ContextWithTimeout(t, 5*time.Second))
require.NoError(t, err)
require.Equal(t, int64(4), statsHandler.ACKsReceived.Load())
require.NoError(t, stream.CloseSend())
})
testData := &wrapperspb.BytesValue{Value: make([]byte, 20)}
_, err = rand.Read(testData.Value)
require.NoError(t, err)
testResource := ads.NewResource("testData", "0", testData)
clearEntry := func() {
bytesCache.Clear(testResource.Name, time.Now())
}
setEntry := func(t *testing.T) {
bytesCache.SetResource(testResource, time.Now())
t.Cleanup(clearEntry)
}
t.Run("delta", func(t *testing.T) {
statsHandler.reset()
stream, err := client.DeltaAggregatedResources(testutils.ContextWithTimeout(t, 5*time.Second))
require.NoError(t, err)
req := &ads.DeltaDiscoveryRequest{
Node: locator.node,
TypeUrl: testResource.TypeURL(),
ResourceNamesSubscribe: []string{testResource.Name},
}
require.NoError(t, stream.Send(req))
res := new(ads.DeltaDiscoveryResponse)
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Equal(t, res.RemovedResources, []string{testResource.Name})
require.Equal(t, int64(1), statsHandler.UnknownResources.Load())
setEntry(t)
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Len(t, res.Resources, 1)
testutils.ProtoEquals(t, testutils.MustMarshal(t, testResource), res.Resources[0])
// check that re-subscribing to a resource causes the server to resend it.
require.NoError(t, stream.Send(req))
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Len(t, res.Resources, 1)
testutils.ProtoEquals(t, testutils.MustMarshal(t, testResource), res.Resources[0])
req.ResourceNamesSubscribe = nil
req.ResponseNonce = res.Nonce
require.NoError(t, stream.Send(req))
// It's hard to test for the _absence_ of a response to the ACK, however the followup check for the
// removed resource will fail if the server responds to the ACK with anything unexpected. The test
// can still be forced to fail early by checking the value of the ACK metric.
require.Eventually(t, func() bool {
return statsHandler.ACKsReceived.Load() == 1
}, 2*time.Second, 100*time.Millisecond)
clearEntry()
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Len(t, res.Resources, 0)
require.Equal(t, res.RemovedResources, []string{testResource.Name})
// However, the server should respect subscription changes in an ACK. By subscribing to a resource
// that does not exist, it can be forced to respond with a deletion.
req.ResourceNamesSubscribe = []string{"noSuchResource"}
req.ResponseNonce = res.Nonce
require.NoError(t, stream.Send(req))
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Len(t, res.Resources, 0)
require.Equal(t, res.RemovedResources, req.ResourceNamesSubscribe)
require.Equal(t, int64(2), statsHandler.ACKsReceived.Load())
// Finally, the NACK metric can be tested by NACKing the previous response. No response is expected
// from the server for this NACK, so the NACK metric needs to be checked.
req.ResourceNamesSubscribe = nil
req.ResponseNonce = res.Nonce
req.ErrorDetail = &status.Status{
Code: 420,
Message: "Testing NACK",
}
require.NoError(t, stream.Send(req))
require.Eventually(t, func() bool {
return statsHandler.NACKsReceived.Load() == 1
}, 2*time.Second, 100*time.Millisecond)
require.NoError(t, stream.Send(&ads.DeltaDiscoveryRequest{
Node: new(core.Node),
TypeUrl: badTypeURL,
ResourceNamesSubscribe: badResources,
}))
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Equal(t, badTypeURL, res.GetTypeUrl())
require.Equal(t, badResources, res.GetRemovedResources())
require.Equal(t, int64(1), statsHandler.UnknownTypes.Load())
})
t.Run("SotW", func(t *testing.T) {
statsHandler.reset()
testListener1 := ads.NewResource("testListener1", "0", &ads.Listener{Name: "testListener1"})
testListener2 := ads.NewResource("testListener2", "1", &ads.Listener{Name: "testListener2"})
// This test relies on Listener not being a pseudo delta resource type, so fail the test early otherwise
require.False(t, utils.IsPseudoDeltaSotW(testListener1.TypeURL()))
stream, err := client.StreamAggregatedResources(testutils.Context(t))
require.NoError(t, err)
req := &ads.SotWDiscoveryRequest{
Node: locator.node,
TypeUrl: testListener1.TypeURL(),
ResourceNames: []string{testListener1.Name, testListener2.Name},
}
require.NoError(t, stream.Send(req))
res := new(ads.SotWDiscoveryResponse)
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Empty(t, res.Resources)
require.Equal(t, int64(2), statsHandler.UnknownResources.Load())
listenerCache.SetResource(testListener1, time.Now())
t.Cleanup(func() {
listenerCache.Clear(testListener1.Name, time.Now())
})
waitForResponse(t, res, stream, 10*time.Millisecond)
if len(res.Resources) == 0 {
// This can happen because the responses from the cache notifying the client that testListener1 and
// testListener2 arrive asynchronously, so the server may have sent the response for testListener1
// not being present before receiving the notification for testListener2. This is simply a property
// of SotW, and it's hard to work around.
waitForResponse(t, res, stream, 10*time.Millisecond)
}
require.Len(t, res.Resources, 1)
testutils.ProtoEquals(t, testutils.MustMarshal(t, testListener1).Resource, res.Resources[0])
listenerCache.SetResource(testListener2, time.Now())
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Len(t, res.Resources, 2)
// Order is not guaranteed, so it must be checked explicitly
if proto.Equal(testutils.MustMarshal(t, testListener1).Resource, res.Resources[0]) {
testutils.ProtoEquals(t, testutils.MustMarshal(t, testListener2).Resource, res.Resources[1])
} else {
testutils.ProtoEquals(t, testutils.MustMarshal(t, testListener1).Resource, res.Resources[1])
testutils.ProtoEquals(t, testutils.MustMarshal(t, testListener2).Resource, res.Resources[0])
}
req.VersionInfo = res.VersionInfo
req.ResponseNonce = res.Nonce
require.NoError(t, stream.Send(req))
// It's hard to test for the _absence_ of a response to the ACK, however the followup check for the
// removed resource will fail if the server responds to the ACK with anything unexpected. The test
// can still be forced to fail early by checking the value of the ACK metric.
require.Eventually(t, func() bool {
return statsHandler.ACKsReceived.Load() == 1
}, 2*time.Second, 100*time.Millisecond)
listenerCache.Clear(testListener2.Name, time.Now())
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Len(t, res.Resources, 1)
testutils.ProtoEquals(t, testutils.MustMarshal(t, testListener1).Resource, res.Resources[0])
// However, the server is supposed to respect any changes to subscriptions when ACKing, so ACKing the
// most recent response with a different subscription list, namely by adding a resource that does not
// exist (testListener3 in this case), the server should send back a response indicating that it does
// not exist (which in SotW means sending only testListener1).
req.VersionInfo = res.VersionInfo
req.ResponseNonce = res.Nonce
req.ResourceNames = append(req.ResourceNames, "testListener3")
require.NoError(t, stream.Send(req))
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Len(t, res.Resources, 1)
testutils.ProtoEquals(t, testutils.MustMarshal(t, testListener1).Resource, res.Resources[0])
require.Equal(t, int64(2), statsHandler.ACKsReceived.Load())
// Finally, check the NACK logic. Note that even though the subscription list hasn't changed, no
// response is expected.
req.VersionInfo = res.VersionInfo
req.ResponseNonce = res.Nonce
req.ErrorDetail = &status.Status{
Code: 420,
Message: "Testing NACK",
}
require.NoError(t, stream.Send(req))
require.Eventually(t, func() bool {
return statsHandler.NACKsReceived.Load() == 1
}, 2*time.Second, 100*time.Millisecond)
require.NoError(t, stream.Send(&ads.SotWDiscoveryRequest{
Node: new(core.Node),
TypeUrl: badTypeURL,
ResourceNames: badResources,
}))
waitForResponse(t, res, stream, 10*time.Millisecond)
require.Equal(t, badTypeURL, res.GetTypeUrl())
require.Empty(t, res.GetResources())
require.Equal(t, int64(1), statsHandler.UnknownTypes.Load())
})
// Author's note: there are no semantic differences in the way subscriptions and ACKs are managed for
// pseudo delta SotW, so this test avoids retesting what was already tested in the previous test for
// brevity.
t.Run("PseudoDeltaSotW", func(t *testing.T) {
statsHandler.reset()
// This test relies on Bytes being a pseudo delta resource type, so fail the test early otherwise
require.True(t, utils.IsPseudoDeltaSotW(testResource.TypeURL()))
stream, err := client.StreamAggregatedResources(testutils.Context(t))
require.NoError(t, err)
req := &ads.SotWDiscoveryRequest{
Node: locator.node,
TypeUrl: testResource.TypeURL(),
ResourceNames: []string{testResource.Name},
}
require.NoError(t, stream.Send(req))
// PseudoDeltaSotW does not provide a mechanism for the server to communicate that a resource does not exist.
// Functionally, PseudoDeltaSotW clients are expected to treat requested resources that don't arrive within a
// given timeout to be deleted. Here we check that the server does not respond with the requested resource
// before it's set by using a wait then checking the time at which the resource was received after it was set.
const wait = time.Second
time.AfterFunc(wait, func() {
// The metric should have been updated at this point, if not, fail the test
require.Equal(t, int64(1), statsHandler.UnknownResources.Load())
setEntry(t)
})
const delta = 50 * time.Millisecond
startWait := time.Now()
res := new(ads.SotWDiscoveryResponse)
waitForResponse(t, res, stream, wait+delta)
require.WithinDuration(t, time.Now(), startWait.Add(wait), delta)
require.Len(t, res.Resources, 1)
testutils.ProtoEquals(t, testutils.MustMarshal(t, testResource).Resource, res.Resources[0])
// Note that the following is technically a protocol violation. As noted above, PseudoDeltaSotW
// cannot signal to the client that a resource does not exist, it simply never responds. However, in
// the event that the server receives a request for a type it does not know (and never will since
// they are not dynamic and determined at startup), since there is no way to signal that this request
// will never be satisfied, the server will respond with an empty response.
require.NoError(t, stream.Send(&ads.SotWDiscoveryRequest{
Node: new(core.Node),
TypeUrl: badTypeURL,
ResourceNames: badResources,
}))
waitForResponse(t, res, stream, wait+delta)
require.Equal(t, badTypeURL, res.GetTypeUrl(), prototext.Format(res))
require.Empty(t, res.GetResources())
require.Equal(t, int64(1), statsHandler.UnknownTypes.Load())
})
}
type xDSResponse interface {
proto.Message
GetControlPlane() *core.ControlPlane
}
// waitForResponse waits for a response on the given stream, failing the test if the response does
// not arrive within the timeout or if an error is returned.
func waitForResponse(
t *testing.T,
res xDSResponse,
stream interface{ RecvMsg(any) error },
timeout time.Duration,
) {
t.Helper()
ch := make(chan error)
go func() {
ch <- stream.RecvMsg(res)
}()
select {
case err := <-ch:
require.NoError(t, err)
case <-time.After(timeout):
t.Fatalf("Did not receive response in %s", timeout)
}
testutils.ProtoEquals(t, controlPlane, res.GetControlPlane())
}
func readResourcesFromJSONFile(t *testing.T, f string) (resources []*ads.RawResource) {
data, err := os.ReadFile(f)
require.NoError(t, err)
var rawResources []json.RawMessage
require.NoError(t, json.Unmarshal(data, &rawResources))
for _, raw := range rawResources {
r := new(ads.RawResource)
require.NoError(t, protojson.Unmarshal(raw, r), string(raw))
resources = append(resources, r)
}
return resources
}
type simpleBatchHandler struct {
t *testing.T
notify func(name string, r *ads.RawResource, metadata ads.SubscriptionMetadata)
ch atomic.Pointer[chan struct{}]
}
func (h *simpleBatchHandler) StartNotificationBatch() {
ch := make(chan struct{}, 1)
require.True(h.t, h.ch.CompareAndSwap(nil, &ch))
}
func (h *simpleBatchHandler) Notify(name string, r *ads.RawResource, metadata ads.SubscriptionMetadata) {
h.notify(name, r, metadata)
}
func (h *simpleBatchHandler) ResourceMarshalError(name string, resource proto.Message, err error) {
h.t.Fatalf("Unexpected resource marshal error for %q: %v\n%v", name, err, resource)
}
func (h *simpleBatchHandler) EndNotificationBatch() {
close(*h.ch.Load())
}
func (h *simpleBatchHandler) check() {
<-*h.ch.Swap(nil)
}
func newSotWReq(subscribe ...string) *ads.SotWDiscoveryRequest {
return &ads.SotWDiscoveryRequest{
ResourceNames: subscribe,
}
}
func newDeltaReq(subscribe, unsubscribe []string) *ads.DeltaDiscoveryRequest {
return &ads.DeltaDiscoveryRequest{
ResourceNamesSubscribe: subscribe,
ResourceNamesUnsubscribe: unsubscribe,
}
}
func TestSubscriptionManagerSubscriptions(t *testing.T) {
const (
r1 = "r1"
r2 = "r2"
)
checkSubs := func(t *testing.T, c RawCache, h ads.RawSubscriptionHandler, wildcard, r1Sub, r2Sub bool) {
t.Helper()
require.Equal(t, wildcard, IsSubscribedTo(c, ads.WildcardSubscription, h), "wildcard")
require.Equal(t, r1Sub, IsSubscribedTo(c, r1, h), r1)
require.Equal(t, r2Sub, IsSubscribedTo(c, r2, h), r2)
}
newCacheAndHandler := func(t *testing.T) (Cache[*wrapperspb.BoolValue], ResourceLocator, *simpleBatchHandler) {
tl := newTestLocator(t, nil, TypeOf[*wrapperspb.BoolValue]())
c := getCache[*wrapperspb.BoolValue](tl)
expected := ads.NewResource(r1, "0", wrapperspb.Bool(true))
c.SetResource(expected, time.Time{})
h := &simpleBatchHandler{
t: t,
notify: func(name string, r *ads.RawResource, metadata ads.SubscriptionMetadata) {
if name == r1 {
require.Same(t, testutils.MustMarshal(t, expected), r)
b, err := r.Resource.UnmarshalNew()
require.NoError(t, err)
testutils.ProtoEquals(t, wrapperspb.Bool(true), b)
} else {
require.Nil(t, r)
}
},
}
return c, tl, h
}
for _, streamType := range []ads.StreamType{ads.DeltaStreamType, ads.SotWStreamType} {
t.Run(streamType.String(), func(t *testing.T) {
t.Run("wildcard", func(t *testing.T) {
c, l, h := newCacheAndHandler(t)
var sotw internal.SubscriptionManager[*ads.SotWDiscoveryRequest]
var delta internal.SubscriptionManager[*ads.DeltaDiscoveryRequest]
if streamType == ads.DeltaStreamType {
delta = internal.NewDeltaSubscriptionManager(testutils.Context(t), l, c.Type().URL(), h)
} else {
sotw = internal.NewSotWSubscriptionManager(testutils.Context(t), l, c.Type().URL(), h)
}
checkSubs(t, c, h, false, false, false)
// subscribe to the wildcard
if streamType == ads.DeltaStreamType {
delta.ProcessSubscriptions(newDeltaReq([]string{ads.WildcardSubscription}, nil))
} else {
sotw.ProcessSubscriptions(newSotWReq(ads.WildcardSubscription))
}
h.check()
checkSubs(t, c, h,
true,
// implicit subscription to r1 via wildcard
true,
// implicit subscription to r2 via wildcard
true,
)
// subscribe to r2, unsubscribe from wildcard
if streamType == ads.DeltaStreamType {
delta.ProcessSubscriptions(newDeltaReq([]string{r2}, []string{ads.WildcardSubscription}))
} else {
sotw.ProcessSubscriptions(newSotWReq(r2))
}
h.check()
checkSubs(t, c, h,
false,
// because r1 was not explicitly subscribed to, its implicit subscription should also be removed
false,
// explicit subscription
true,
)
})
t.Run("normal", func(t *testing.T) {
c, l, h := newCacheAndHandler(t)
var sotw internal.SubscriptionManager[*ads.SotWDiscoveryRequest]
var delta internal.SubscriptionManager[*ads.DeltaDiscoveryRequest]
if streamType == ads.DeltaStreamType {
delta = internal.NewDeltaSubscriptionManager(testutils.Context(t), l, c.Type().URL(), h)
} else {
sotw = internal.NewSotWSubscriptionManager(testutils.Context(t), l, c.Type().URL(), h)
}
// subscribe to r1 and r2
if streamType == ads.DeltaStreamType {
delta.ProcessSubscriptions(newDeltaReq([]string{r1, r2}, nil))
} else {
sotw.ProcessSubscriptions(newSotWReq(r1, r2))
}
h.check()
checkSubs(t, c, h,
false,
true,
true,
)
// unsubscribe from r2, keep r1
if streamType == ads.DeltaStreamType {
delta.ProcessSubscriptions(newDeltaReq(nil, []string{r2}))
} else {
sotw.ProcessSubscriptions(newSotWReq(r1))
}
h.check()
checkSubs(t, c, h,
false,
true,
// unsubscribed
false,
)
})
})
}
}
type mockResourceLocator struct {
isTypeSupported func(typeURL string) bool
subscribe func(typeURL, resourceName string) func()
resubscribe func(typeURL, resourceName string)
}
func (m *mockResourceLocator) IsTypeSupported(_ context.Context, typeURL string) bool {
return m.isTypeSupported(typeURL)
}
func (m *mockResourceLocator) Subscribe(_ context.Context, typeURL, resourceName string, _ ads.RawSubscriptionHandler) func() {
return m.subscribe(typeURL, resourceName)
}
func (m *mockResourceLocator) Resubscribe(_ context.Context, typeURL, resourceName string, _ ads.RawSubscriptionHandler) {
m.resubscribe(typeURL, resourceName)
}
func TestImplicitWildcardSubscription(t *testing.T) {
const foo = "foo"
h := NewNoopBatchSubscriptionHandler(t)
typeURL := TypeOf[*ads.Secret]().URL()
newMockLocator := func(t *testing.T) (l *mockResourceLocator, wildcardSub, fooSub chan struct{}) {
wildcardSub = make(chan struct{}, 1)
fooSub = make(chan struct{}, 1)
l = &mockResourceLocator{
isTypeSupported: func(actualTypeURL string) bool {
require.Equal(t, typeURL, actualTypeURL)
return true
},
subscribe: func(actualTypeURL, resourceName string) func() {
require.Equal(t, typeURL, actualTypeURL)
switch resourceName {
case ads.WildcardSubscription:
wildcardSub <- struct{}{}
return func() {
close(wildcardSub)
}
case foo:
fooSub <- struct{}{}
return func() {
close(fooSub)
}
default:
t.Fatalf("Unexpected resource name %q", resourceName)
return nil
}
},
resubscribe: func(actualTypeURL, resourceName string) {
switch resourceName {
case ads.WildcardSubscription:
wildcardSub <- struct{}{}
case foo:
fooSub <- struct{}{}
default:
t.Fatalf("Unexpected resource name %q", resourceName)
}
},
}
return l, wildcardSub, fooSub
}
requireSelect := func(t *testing.T, ch <-chan struct{}, shouldBeClosed bool) {
t.Helper()
select {
case _, ok := <-ch:
if ok && shouldBeClosed {
t.Fatalf("Channel not closed")
}
if !ok && !shouldBeClosed {
t.Fatalf("Channel unexpectedly closed")
}
default:
t.Fatalf("empty channel!")
}
}
t.Run("SotW", func(t *testing.T) {
t.Run("empty first call", func(t *testing.T) {
l, wildcardSub, fooSub := newMockLocator(t)
m := internal.NewSotWSubscriptionManager(testutils.Context(t), l, typeURL, h)
// The first call, if empty should always implicit create a wildcard subscription.
m.ProcessSubscriptions(newSotWReq())
requireSelect(t, wildcardSub, false)
// Subsequent requests can ACK the previous wildcard request but not change the subscriptions and not
// provide an explicit resource to subscribe to, in which case the wildcard should persist.
m.ProcessSubscriptions(newSotWReq())
require.Empty(t, wildcardSub)
// However once a resource name is explicitly provided, the implicit wildcard should disappear.
m.ProcessSubscriptions(newSotWReq(foo))
requireSelect(t, wildcardSub, true)
requireSelect(t, fooSub, false)
})
t.Run("non-empty first call", func(t *testing.T) {
l, wildcardSub, fooSub := newMockLocator(t)
m := internal.NewSotWSubscriptionManager(testutils.Context(t), l, typeURL, h)
// If the first call isn't empty, the implicit wildcard subscription should not be present.
m.ProcessSubscriptions(newSotWReq(foo))
requireSelect(t, fooSub, false)
require.Empty(t, wildcardSub)
})
t.Run("explicit wildcard", func(t *testing.T) {
l, wildcardSub, fooSub := newMockLocator(t)
m := internal.NewSotWSubscriptionManager(testutils.Context(t), l, typeURL, h)
m.ProcessSubscriptions(newSotWReq(ads.WildcardSubscription))
requireSelect(t, wildcardSub, false)
require.Empty(t, fooSub)
m.ProcessSubscriptions(newSotWReq())
requireSelect(t, wildcardSub, true)
})
})
t.Run("Delta", func(t *testing.T) {
t.Run("empty first call", func(t *testing.T) {
l, wildcardSub, fooSub := newMockLocator(t)
m := internal.NewDeltaSubscriptionManager(testutils.Context(t), l, typeURL, h)
// The first call, if empty should always implicit create a wildcard subscription.
m.ProcessSubscriptions(newDeltaReq(nil, nil))
requireSelect(t, wildcardSub, false)
// Subsequent requests can ACK the previous wildcard request but not change the subscriptions and not
// provide an explicit resource to subscribe to, in which case the wildcard should persist.
m.ProcessSubscriptions(newDeltaReq(nil, nil))
// However, unlike SotW, it should not resubscribe because it was not explicit.
require.Empty(t, wildcardSub)
// In Delta, the implicit wildcard subscription created by the first message must be explicitly
// removed.
m.ProcessSubscriptions(newDeltaReq([]string{foo}, nil))
// Since there was no explicit change to the wildcard subscription, no notification is expected
require.Empty(t, wildcardSub)
requireSelect(t, fooSub, false)
m.ProcessSubscriptions(newDeltaReq(nil, []string{ads.WildcardSubscription}))
require.Empty(t, fooSub)
requireSelect(t, wildcardSub, true)
})
t.Run("non-empty first call", func(t *testing.T) {
l, wildcardSub, fooSub := newMockLocator(t)
m := internal.NewDeltaSubscriptionManager(testutils.Context(t), l, typeURL, h)
// If the first call isn't empty, the implicit wildcard subscription should not be present.
m.ProcessSubscriptions(newDeltaReq([]string{foo}, nil))
require.Empty(t, wildcardSub)
requireSelect(t, fooSub, false)
})
t.Run("explicit wildcard", func(t *testing.T) {
l, wildcardSub, fooSub := newMockLocator(t)
m := internal.NewDeltaSubscriptionManager(testutils.Context(t), l, typeURL, h)
m.ProcessSubscriptions(newDeltaReq([]string{ads.WildcardSubscription}, nil))
requireSelect(t, wildcardSub, false)
require.Empty(t, fooSub)
m.ProcessSubscriptions(newDeltaReq(nil, []string{ads.WildcardSubscription}))
requireSelect(t, wildcardSub, true)
require.Empty(t, fooSub)
})
})
}
// batchFuncHandler the equivalent of funcHandler but for the BatchSubscriptionHandler interface.
type batchFuncHandler struct {
t *testing.T
start func()
notify func(name string, r *ads.RawResource, metadata ads.SubscriptionMetadata)
end func()
}
func (b *batchFuncHandler) StartNotificationBatch() {
b.start()
}
func (b *batchFuncHandler) Notify(name string, r *ads.RawResource, metadata ads.SubscriptionMetadata) {
b.notify(name, r, metadata)
}
func (b *batchFuncHandler) ResourceMarshalError(name string, resource proto.Message, err error) {
b.t.Fatalf("Unexpected resource marshal error for %q: %v\n%v", name, err, resource)
}
func (b *batchFuncHandler) EndNotificationBatch() {
b.end()
}
func NewBatchSubscriptionHandler(
t *testing.T,
start func(),
notify func(name string, r *ads.RawResource, metadata ads.SubscriptionMetadata),
end func(),
) internal.BatchSubscriptionHandler {
return &batchFuncHandler{
t: t,
start: start,
notify: notify,
end: end,
}
}
func NewNoopBatchSubscriptionHandler(t *testing.T) internal.BatchSubscriptionHandler {
return NewBatchSubscriptionHandler(
t,
func() {}, func(string, *ads.RawResource, ads.SubscriptionMetadata) {}, func() {},
)
}
func TestSubscriptionManagerUnsubscribeAll(t *testing.T) {
typeURL := TypeOf[*ads.Secret]().URL()
h := NewNoopBatchSubscriptionHandler(t)
t.Run("explicit", func(t *testing.T) {
const foo = "foo"
var wg sync.WaitGroup
l := &mockResourceLocator{
isTypeSupported: func(string) bool { return true },
subscribe: func(_, resourceName string) func() {
wg.Done()
return func() {
wg.Done()
}
},
}
m := internal.NewDeltaSubscriptionManager(context.Background(), l, typeURL, h)
wg.Add(2)
m.ProcessSubscriptions(&ads.DeltaDiscoveryRequest{
ResourceNamesSubscribe: []string{ads.WildcardSubscription, foo},
})
wg.Wait()
wg.Add(2)
m.UnsubscribeAll()
wg.Wait()
})
t.Run("on context expiry", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
l := &mockResourceLocator{
isTypeSupported: func(string) bool { return true },
subscribe: func(_, _ string) func() {
wg.Done()
return func() {
wg.Done()
}
},
}
m := internal.NewDeltaSubscriptionManager(ctx, l, typeURL, h)
wg.Add(1)
m.ProcessSubscriptions(&ads.DeltaDiscoveryRequest{
ResourceNamesSubscribe: []string{ads.WildcardSubscription},
})
wg.Wait()
wg.Add(1)
cancel()
wg.Wait()
})
}