-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter_test.go
781 lines (633 loc) · 18.6 KB
/
adapter_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
package discovery
import (
"context"
"errors"
"slices"
"testing"
"time"
"github.com/overmindtech/sdp-go"
)
func TestEngineAddAdapters(t *testing.T) {
ec := EngineConfig{}
e, err := NewEngine(&ec)
if err != nil {
t.Fatalf("Error initializing Engine: %v", err)
}
adapter := TestAdapter{}
if err := e.AddAdapters(&adapter); err != nil {
t.Fatalf("Error adding adapter: %v", err)
}
if x := len(e.sh.Adapters()); x != 4 {
t.Fatalf("Expected 4 adapters, got %v", x)
}
}
func TestGet(t *testing.T) {
adapter := TestAdapter{
ReturnName: "orange",
ReturnType: "person",
ReturnScopes: []string{
"test",
"empty",
},
}
e := newStartedEngine(t, "TestGet", nil, &adapter)
t.Run("Basic test", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
_, _, err := e.executeQuerySync(context.Background(), &sdp.Query{
Type: "person",
Scope: "test",
Query: "three",
Method: sdp.QueryMethod_GET,
})
if err != nil {
t.Fatal(err)
}
if x := len(adapter.GetCalls); x != 1 {
t.Fatalf("Expected 1 get call, got %v", x)
}
firstCall := adapter.GetCalls[0]
if firstCall[0] != "test" || firstCall[1] != "three" {
t.Fatalf("First get call parameters unexpected: %v", firstCall)
}
})
t.Run("not found error", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
items, errs, err := e.executeQuerySync(context.Background(), &sdp.Query{
Type: "person",
Scope: "empty",
Query: "three",
Method: sdp.QueryMethod_GET,
})
if err != nil {
t.Fatal(err)
}
if len(errs) == 1 {
if errs[0].GetErrorType() != sdp.QueryError_NOTFOUND {
t.Errorf("expected ErrorType to be %v, got %v", sdp.QueryError_NOTFOUND, errs[0].GetErrorType())
}
if errs[0].GetErrorString() != "no items found" {
t.Errorf("expected ErrorString to be '%v', got '%v'", "no items found", errs[0].GetErrorString())
}
if errs[0].GetScope() != "empty" {
t.Errorf("expected Scope to be '%v', got '%v'", "empty", errs[0].GetScope())
}
if errs[0].GetSourceName() != "testAdapter-orange" {
t.Errorf("expected Adapter name to be '%v', got '%v'", "testAdapter-orange", errs[0].GetSourceName())
}
if errs[0].GetItemType() != "person" {
t.Errorf("expected ItemType to be '%v', got '%v'", "person", errs[0].GetItemType())
}
if errs[0].GetResponderName() != "TestGet" {
t.Errorf("expected ResponderName to be '%v', got '%v'", "TestGet", errs[0].GetResponderName())
}
} else {
t.Errorf("expected 1 error, got %v", len(errs))
}
if len(items) != 0 {
t.Errorf("expected 0 items, got %v", len(items))
}
})
t.Run("Test caching", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
var list1 []*sdp.Item
var item2 []*sdp.Item
var item3 []*sdp.Item
var err error
req := sdp.Query{
Type: "person",
Scope: "test",
Query: "Dylan",
Method: sdp.QueryMethod_GET,
}
list1, _, err = e.executeQuerySync(context.Background(), &req)
if err != nil {
t.Error(err)
}
time.Sleep(10 * time.Millisecond)
item2, _, err = e.executeQuerySync(context.Background(), &req)
if err != nil {
t.Error(err)
}
if list1[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() != item2[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() {
t.Errorf("Get queries 10ms apart had different timestamps, caching not working. %v != %v", list1[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue(), item2[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue())
}
time.Sleep(10 * time.Millisecond)
e.sh.Purge()
item3, _, err = e.executeQuerySync(context.Background(), &req)
if err != nil {
t.Error(err)
}
if item2[0].GetMetadata().GetTimestamp().String() == item3[0].GetMetadata().GetTimestamp().String() {
t.Error("Get queries after purging had the same timestamps, cache not expiring")
}
})
t.Run("Test Get() caching errors", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
req := sdp.Query{
Type: "person",
Scope: "empty",
Query: "query",
Method: sdp.QueryMethod_GET,
}
_, errs, err := e.executeQuerySync(context.Background(), &req)
if err != nil {
t.Fatal(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
_, errs, err = e.executeQuerySync(context.Background(), &req)
if err != nil {
t.Fatal(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
if l := len(adapter.GetCalls); l != 1 {
t.Errorf("Expected 1 Get call due to caching og NOTFOUND errors, got %v", l)
}
})
t.Run("Hidden items", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
adapter.IsHidden = true
t.Run("Get", func(t *testing.T) {
item, _, err := e.executeQuerySync(context.Background(), &sdp.Query{
Type: "person",
Scope: "test",
Query: "three",
Method: sdp.QueryMethod_GET,
})
if err != nil {
t.Fatal(err)
}
if !item[0].GetMetadata().GetHidden() {
t.Fatal("Item was not marked as hidden in metadata")
}
})
t.Run("List", func(t *testing.T) {
items, _, err := e.executeQuerySync(context.Background(), &sdp.Query{
Type: "person",
Scope: "test",
Method: sdp.QueryMethod_LIST,
})
if err != nil {
t.Fatal(err)
}
if !items[0].GetMetadata().GetHidden() {
t.Fatal("Item was not marked as hidden in metadata")
}
})
t.Run("Search", func(t *testing.T) {
items, _, err := e.executeQuerySync(context.Background(), &sdp.Query{
Type: "person",
Scope: "test",
Query: "three",
Method: sdp.QueryMethod_SEARCH,
})
if err != nil {
t.Fatal(err)
}
if !items[0].GetMetadata().GetHidden() {
t.Fatal("Item was not marked as hidden in metadata")
}
})
})
}
func TestList(t *testing.T) {
adapter := TestAdapter{}
e := newStartedEngine(t, "TestList", nil, &adapter)
_, _, err := e.executeQuerySync(context.Background(), &sdp.Query{
Type: "person",
Scope: "test",
Method: sdp.QueryMethod_LIST,
})
if err != nil {
t.Fatal(err)
}
if x := len(adapter.ListCalls); x != 1 {
t.Fatalf("Expected 1 find call, got %v", x)
}
firstCall := adapter.ListCalls[0]
if firstCall[0] != "test" {
t.Fatalf("First find call parameters unexpected: %v", firstCall)
}
}
func TestSearch(t *testing.T) {
adapter := TestAdapter{}
e := newStartedEngine(t, "TestSearch", nil, &adapter)
_, _, err := e.executeQuerySync(context.Background(), &sdp.Query{
Type: "person",
Scope: "test",
Query: "query",
Method: sdp.QueryMethod_SEARCH,
})
if err != nil {
t.Fatal(err)
}
if x := len(adapter.SearchCalls); x != 1 {
t.Fatalf("Expected 1 Search call, got %v", x)
}
firstCall := adapter.SearchCalls[0]
if firstCall[0] != "test" || firstCall[1] != "query" {
t.Fatalf("First Search call parameters unexpected: %v", firstCall)
}
}
func TestListSearchCaching(t *testing.T) {
adapter := TestAdapter{
ReturnScopes: []string{
"test",
"empty",
"error",
},
}
e := newStartedEngine(t, "TestListSearchCaching", nil, &adapter)
t.Run("caching with successful list", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
var list1 []*sdp.Item
var list2 []*sdp.Item
var list3 []*sdp.Item
var err error
q := sdp.Query{
Type: "person",
Scope: "test",
Method: sdp.QueryMethod_LIST,
}
list1, _, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
time.Sleep(10 * time.Millisecond)
list2, _, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Fatal(err)
}
if list1[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() != list2[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() {
t.Errorf("List queries had different generations, caching not working. %v != %v", list1[0].GetAttributes().GetAttrStruct().GetFields()["generation"], list2[0].GetAttributes().GetAttrStruct().GetFields()["generation"])
}
time.Sleep(10 * time.Millisecond)
e.sh.Purge()
list3, _, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Fatal(err)
}
if list2[0].GetAttributes().GetAttrStruct().GetFields()["generation"] == list3[0].GetAttributes().GetAttrStruct().GetFields()["generation"] {
t.Errorf("List queries after purging had the same generation, caching not working. %v == %v", list2[0].GetAttributes().GetAttrStruct().GetFields()["generation"], list3[0].GetAttributes().GetAttrStruct().GetFields()["generation"])
}
})
t.Run("empty list", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
var err error
q := sdp.Query{
Type: "person",
Scope: "empty",
Method: sdp.QueryMethod_LIST,
}
_, errs, err := e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Fatal(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
time.Sleep(10 * time.Millisecond)
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Fatal(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
if l := len(adapter.ListCalls); l != 1 {
t.Errorf("Expected only 1 list call, got %v, cache not working: %v", l, adapter.ListCalls)
}
time.Sleep(200 * time.Millisecond)
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Fatal(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
if l := len(adapter.ListCalls); l != 2 {
t.Errorf("Expected 2 list calls, got %v, cache not clearing: %v", l, adapter.ListCalls)
}
})
t.Run("caching with successful search", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
var list1 []*sdp.Item
var list2 []*sdp.Item
var list3 []*sdp.Item
var err error
q := sdp.Query{
Type: "person",
Scope: "test",
Query: "query",
Method: sdp.QueryMethod_SEARCH,
}
list1, _, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
time.Sleep(10 * time.Millisecond)
list2, _, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if list1[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() != list2[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() {
t.Errorf("List queries had different generations, caching not working. %v != %v", list1[0].GetAttributes().GetAttrStruct().GetFields()["generation"], list2[0].GetAttributes().GetAttrStruct().GetFields()["generation"])
}
time.Sleep(200 * time.Millisecond)
list3, _, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if list2[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() == list3[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() {
t.Errorf("List queries 200ms apart had the same generations, caching not working. %v == %v", list2[0].GetAttributes().GetAttrStruct().GetFields()["generation"], list3[0].GetAttributes().GetAttrStruct().GetFields()["generation"])
}
})
t.Run("empty search", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
var err error
q := sdp.Query{
Type: "person",
Scope: "empty",
Query: "query",
Method: sdp.QueryMethod_SEARCH,
}
_, errs, err := e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
time.Sleep(10 * time.Millisecond)
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
time.Sleep(200 * time.Millisecond)
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
if l := len(adapter.SearchCalls); l != 2 {
t.Errorf("Expected 2 find calls, got %v, cache not clearing", l)
}
})
t.Run("non-caching of OTHER errors", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
q := sdp.Query{
Type: "person",
Scope: "error",
Query: "query",
Method: sdp.QueryMethod_GET,
}
_, errs, err := e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
if l := len(adapter.GetCalls); l != 2 {
t.Errorf("Expected 2 get calls, got %v, OTHER errors should not be cached", l)
}
})
t.Run("non-caching when ignoreCache is specified", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
q := sdp.Query{
Type: "person",
Scope: "error",
Query: "query",
Method: sdp.QueryMethod_GET,
}
_, errs, err := e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
q.Method = sdp.QueryMethod_LIST
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
q.Method = sdp.QueryMethod_SEARCH
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
_, errs, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(errs) != 1 {
t.Fatalf("Expected 1 error, got %v", len(errs))
}
if l := len(adapter.GetCalls); l != 2 {
t.Errorf("Expected 2 get calls, got %v", l)
}
if l := len(adapter.ListCalls); l != 2 {
t.Errorf("Expected 2 List calls, got %v", l)
}
if l := len(adapter.SearchCalls); l != 2 {
t.Errorf("Expected 2 Search calls, got %v", l)
}
})
}
func TestSearchGetCaching(t *testing.T) {
// We want to be sure that if an item has been found via a search and
// cached, the cache will be hit if a Get is run for that particular item
adapter := TestAdapter{
ReturnScopes: []string{
"test",
},
}
e := newStartedEngine(t, "TestSearchGetCaching", nil, &adapter)
t.Run("caching with successful search", func(t *testing.T) {
t.Cleanup(func() {
adapter.ClearCalls()
})
var searchResult []*sdp.Item
var searchErrors []*sdp.QueryError
var getResult []*sdp.Item
var getErrors []*sdp.QueryError
var err error
q := sdp.Query{
Type: "person",
Scope: "test",
Query: "Dylan",
Method: sdp.QueryMethod_SEARCH,
}
t.Logf("Searching for %v", q.GetQuery())
searchResult, searchErrors, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(searchErrors) != 0 {
for _, err := range searchErrors {
t.Error(err)
}
}
if len(searchResult) == 0 {
t.Fatal("Got no results")
}
if len(searchResult) > 1 {
t.Fatalf("Got too many results: %v", searchResult)
}
time.Sleep(10 * time.Millisecond)
// Do a get query for that same item
q.Method = sdp.QueryMethod_GET
q.Query = searchResult[0].UniqueAttributeValue()
t.Logf("Getting %v from cache", q.GetQuery())
getResult, getErrors, err = e.executeQuerySync(context.Background(), &q)
if err != nil {
t.Error(err)
}
if len(getErrors) != 0 {
for _, err := range getErrors {
t.Error(err)
}
}
if len(getResult) == 0 {
t.Error("No result from GET")
}
if searchResult[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() != getResult[0].GetAttributes().GetAttrStruct().GetFields()["generation"].GetNumberValue() {
t.Errorf("Search and Get queries had different generations, caching not working. %v != %v", searchResult[0].GetAttributes().GetAttrStruct().GetFields()["generation"], getResult[0].GetAttributes().GetAttrStruct().GetFields()["generation"])
}
})
}
func TestNewQueryResultStream(t *testing.T) {
items := make(chan *sdp.Item)
errs := make(chan error)
itemHandler := func(item *sdp.Item) {
time.Sleep(10 * time.Millisecond)
items <- item
}
errHandler := func(err error) {
time.Sleep(10 * time.Millisecond)
errs <- err
}
stream := NewQueryResultStream(itemHandler, errHandler)
// Test Initialization
if stream == nil {
t.Fatal("Expected stream to be initialized, got nil")
}
if stream.itemHandler == nil || stream.errHandler == nil {
t.Fatal("Expected handlers to be set")
}
if stream.items == nil || stream.errs == nil {
t.Fatal("Expected channels to be initialized")
}
if !stream.open {
t.Fatal("Expected stream to be open")
}
// Test SendItem
testItem := &sdp.Item{}
stream.SendItem(testItem)
// Due to the fact that the handlers are executed in a goroutine it
// essentially gives us a buffered channel with a buffer depth of 1 since
// the item can be pulled off the internal items channel immediately then
// wait on the handler in parallel. That's what allows this test to work
// without extra synchronization
if x := <-items; x != testItem {
t.Fatalf("Expected item to be %v, got %v", testItem, x)
}
// Test SendError
testErr := errors.New("test error")
stream.SendError(testErr)
if x := <-errs; x.Error() != testErr.Error() {
t.Fatalf("Expected error to be %v, got %v", testErr, x)
}
// Now I want to test that the Close() function actually blocks as expected
// and that the handlers are executed *before* the Close() function returns
// and the stream is marked as closed
closed := make(chan struct{})
go func() {
stream.Close()
close(closed)
}()
stream.SendItem(testItem)
stream.SendError(testErr)
order := make([]string, 0)
for {
select {
case <-items:
order = append(order, "item")
case <-errs:
order = append(order, "err")
case <-closed:
order = append(order, "closed")
break
}
if len(order) == 3 {
break
}
}
if !slices.Contains(order, "item") {
t.Errorf("Expected item to be handled. Results: %v", order)
}
if !slices.Contains(order, "err") {
t.Errorf("Expected error to be handled. Results: %v", order)
}
if order[2] != "closed" {
t.Errorf("Expected stream to be closed last. Results: %v", order)
}
}