@@ -23,6 +23,8 @@ import (
23
23
"github.com/pkg/errors"
24
24
)
25
25
26
+ const maxSettings = 128
27
+
26
28
type dummy struct {
27
29
msg1 string
28
30
growsbyone string
@@ -612,3 +614,61 @@ func TestHide(t *testing.T) {
612
614
t .Errorf ("expected 'sekretz' to be hidden" )
613
615
}
614
616
}
617
+
618
+ func TestOnChangeWithMaxSettings (t * testing.T ) {
619
+ // Register maxSettings settings to ensure that no errors occur.
620
+ maxName , err := batchRegisterSettings (t , t .Name (), maxSettings - 1 - len (settings .Keys ()))
621
+ if err != nil {
622
+ t .Errorf ("expected no error to register 128 settings, but get error : %s" , err )
623
+ }
624
+
625
+ // Change the max slotIdx setting to ensure that no errors occur.
626
+ sv := & settings.Values {}
627
+ sv .Init (settings .TestOpaque )
628
+ var changes int
629
+ intSetting , ok := settings .Lookup (maxName )
630
+ if ! ok {
631
+ t .Errorf ("expected lookup of %s to succeed" , maxName )
632
+ }
633
+ intSetting .SetOnChange (sv , func () { changes ++ })
634
+
635
+ u := settings .NewUpdater (sv )
636
+ if err := u .Set (maxName , settings .EncodeInt (9 ), "i" ); err != nil {
637
+ t .Fatal (err )
638
+ }
639
+
640
+ if changes != 1 {
641
+ t .Errorf ("expected the max slot setting changed" )
642
+ }
643
+ }
644
+
645
+ func TestMaxSettingsPanics (t * testing.T ) {
646
+ // Register too many settings which will cause a panic which is caught and converted to an error.
647
+ _ , err := batchRegisterSettings (t , t .Name (), maxSettings - len (settings .Keys ()))
648
+ expectedErr := "too many settings; increase maxSettings"
649
+ if err == nil || err .Error () != expectedErr {
650
+ t .Errorf ("expected error %v, but got %v" , expectedErr , err )
651
+ }
652
+ }
653
+
654
+ func batchRegisterSettings (t * testing.T , keyPrefix string , count int ) (name string , err error ) {
655
+ defer func () {
656
+ // Catch panic and convert it to an error.
657
+ if r := recover (); r != nil {
658
+ // Check exactly what the panic was and create error.
659
+ switch x := r .(type ) {
660
+ case string :
661
+ err = errors .New (x )
662
+ case error :
663
+ err = x
664
+ default :
665
+ err = errors .Errorf ("unknown panic: %v" , x )
666
+ }
667
+ }
668
+ }()
669
+ for i := 0 ; i < count ; i ++ {
670
+ name = fmt .Sprintf ("%s_%3d" , keyPrefix , i )
671
+ settings .RegisterValidatedIntSetting (name , "desc" , 0 , nil )
672
+ }
673
+ return name , err
674
+ }
0 commit comments