@@ -540,3 +540,136 @@ func Test_concurrentCache_Set_Fetch_Failure(t *testing.T) {
540
540
}
541
541
}
542
542
}
543
+
544
+ func Test_hostCache (t * testing.T ) {
545
+ base := NewCache ()
546
+
547
+ // no entry in the cache
548
+ ctx := context .Background ()
549
+
550
+ hc := hostCache {base }
551
+
552
+ fetch := func (i int ) func (context.Context ) (string , error ) {
553
+ return func (context.Context ) (string , error ) {
554
+ return strconv .Itoa (i ), nil
555
+ }
556
+ }
557
+
558
+ // The key is ignored in the hostCache implementation.
559
+
560
+ { // Set the token to 100
561
+ gotToken , err := hc .Set (ctx , "reg.example.com" , SchemeBearer , "key1" , fetch (100 ))
562
+ if err != nil {
563
+ t .Fatalf ("hostCache.Set() error = %v" , err )
564
+ }
565
+ if want := strconv .Itoa (100 ); gotToken != want {
566
+ t .Errorf ("hostCache.Set() = %v, want %v" , gotToken , want )
567
+ }
568
+ }
569
+
570
+ { // Overwrite the token entry to 101
571
+ gotToken , err := hc .Set (ctx , "reg.example.com" , SchemeBearer , "key2" , fetch (101 ))
572
+ if err != nil {
573
+ t .Fatalf ("hostCache.Set() error = %v" , err )
574
+ }
575
+ if want := strconv .Itoa (101 ); gotToken != want {
576
+ t .Errorf ("hostCache.Set() = %v, want %v" , gotToken , want )
577
+ }
578
+ }
579
+
580
+ { // Add entry for another host
581
+ gotToken , err := hc .Set (ctx , "reg2.example.com" , SchemeBearer , "key3" , fetch (102 ))
582
+ if err != nil {
583
+ t .Fatalf ("hostCache.Set() error = %v" , err )
584
+ }
585
+ if want := strconv .Itoa (102 ); gotToken != want {
586
+ t .Errorf ("hostCache.Set() = %v, want %v" , gotToken , want )
587
+ }
588
+ }
589
+
590
+ { // Ensure the token for key1 is 101 now
591
+ gotToken , err := hc .GetToken (ctx , "reg.example.com" , SchemeBearer , "key1" )
592
+ if err != nil {
593
+ t .Fatalf ("hostCache.GetToken() error = %v" , err )
594
+ }
595
+ if want := strconv .Itoa (101 ); gotToken != want {
596
+ t .Errorf ("hostCache.GetToken() = %v, want %v" , gotToken , want )
597
+ }
598
+ }
599
+
600
+ { // Make sure GetScheme still works
601
+ gotScheme , err := hc .GetScheme (ctx , "reg.example.com" )
602
+ if err != nil {
603
+ t .Fatalf ("hostCache.GetScheme() error = %v" , err )
604
+ }
605
+ if want := SchemeBearer ; gotScheme != want {
606
+ t .Errorf ("hostCache.GetScheme() = %v, want %v" , gotScheme , want )
607
+ }
608
+ }
609
+ }
610
+
611
+ func Test_fallbackCache (t * testing.T ) {
612
+ // no entry in the cache
613
+ ctx := context .Background ()
614
+
615
+ scc := NewSingleContextCache ()
616
+
617
+ fetch := func (i int ) func (context.Context ) (string , error ) {
618
+ return func (context.Context ) (string , error ) {
619
+ return strconv .Itoa (i ), nil
620
+ }
621
+ }
622
+
623
+ // Test that fallback works
624
+
625
+ { // Set the token to 100
626
+ gotToken , err := scc .Set (ctx , "reg.example.com" , SchemeBearer , "key1" , fetch (100 ))
627
+ if err != nil {
628
+ t .Fatalf ("hostCache.Set() error = %v" , err )
629
+ }
630
+ if want := strconv .Itoa (100 ); gotToken != want {
631
+ t .Errorf ("hostCache.Set() = %v, want %v" , gotToken , want )
632
+ }
633
+ }
634
+
635
+ { // Ensure the token for key2 falls back to 100
636
+ gotToken , err := scc .GetToken (ctx , "reg.example.com" , SchemeBearer , "key2" )
637
+ if err != nil {
638
+ t .Fatalf ("hostCache.GetToken() error = %v" , err )
639
+ }
640
+ if want := strconv .Itoa (100 ); gotToken != want {
641
+ t .Errorf ("hostCache.GetToken() = %v, want %v" , gotToken , want )
642
+ }
643
+ }
644
+
645
+ { // Make sure GetScheme works as expected
646
+ gotScheme , err := scc .GetScheme (ctx , "reg.example.com" )
647
+ if err != nil {
648
+ t .Fatalf ("hostCache.GetScheme() error = %v" , err )
649
+ }
650
+ if want := SchemeBearer ; gotScheme != want {
651
+ t .Errorf ("hostCache.GetScheme() = %v, want %v" , gotScheme , want )
652
+ }
653
+ }
654
+
655
+ { // Make sure GetScheme falls back
656
+ gotScheme , err := scc .GetScheme (ctx , "reg.example.com" )
657
+ if err != nil {
658
+ t .Fatalf ("hostCache.GetScheme() error = %v" , err )
659
+ }
660
+ if want := SchemeBearer ; gotScheme != want {
661
+ t .Errorf ("hostCache.GetScheme() = %v, want %v" , gotScheme , want )
662
+ }
663
+ }
664
+
665
+ { // Check GetScheme fallback
666
+ // scc.(*fallbackCache).primary = NewCache()
667
+ gotScheme , err := scc .GetScheme (ctx , "reg2.example.com" )
668
+ if ! errors .Is (err , errdef .ErrNotFound ) {
669
+ t .Fatalf ("hostCache.GetScheme() error = %v" , err )
670
+ }
671
+ if want := SchemeUnknown ; gotScheme != want {
672
+ t .Errorf ("hostCache.GetScheme() = %v, want %v" , gotScheme , want )
673
+ }
674
+ }
675
+ }
0 commit comments