func TestEventStreamCancellationAfterDebouncing(t *testing.T) { origDebounceTime := cfgDebounceTime cfgDebounceTime = 1 * time.Millisecond defer func() { cfgDebounceTime = origDebounceTime }() expCfg := map[string]string{ "key1": "value1", "key2": "value2", } defaults.Set("foo", expCfg) cfgChan, unsubChan := EventStream("foo") <-time.After(100 * time.Millisecond) close(unsubChan) <-time.After(100 * time.Millisecond) select { case <-cfgChan: t.Fatal("received config after cancelling subscription") case <-time.After(1 * time.Second): break } }
func TestFlagCancelDynamicUpdates(t *testing.T) { defaults.Set("generic-flag-1", map[string]string{"_": "hello"}) f := &flagImpl{} f.init(stringMapper, []string{"generic-flag-1"}) f.CancelDynamicUpdates() // Calling cancel a second time should be a no-op f.CancelDynamicUpdates() expValue := "test" f.set(expValue) select { case <-f.ChangeChan(): case <-time.After(1 * time.Second): t.Fatal("timeout waiting for change evnet") } value := f.get().(string) if value != expValue { t.Fatalf("expected value %q; got %q", expValue, value) } }
func TestInt32FlagUpdateWithInvalidValue(t *testing.T) { defaults.Set("int32-flag-invalid", map[string]string{"flag": "foo"}) f := Int32("int32-flag-invalid") select { case <-f.ChangeChan(): t.Fatal("unexpected configuration change event") case <-time.After(1000 * time.Millisecond): } }
func TestFlagValueMapperError(t *testing.T) { defaults.Set("generic-flag-1", map[string]string{"_": "hello"}) f := &flagImpl{} f.init(func(_ string) (interface{}, error) { return nil, errors.New("") }, []string{"generic-flag-1"}) select { case <-f.ChangeChan(): t.Fatal("unexpected change event") case <-time.After(1 * time.Second): } }
func TestStringFlagDynamicUpdate(t *testing.T) { defaults.Set("string-flag", map[string]string{"flag": "123"}) f := String("string-flag") select { case <-f.ChangeChan(): case <-time.After(1000 * time.Millisecond): t.Fatal("timeout waiting for flag change event") } expVal := "123" val := f.Get() if val != expVal { t.Fatalf("expected val to be %q; got %q", expVal, val) } }
func TestFlagDynamicChange(t *testing.T) { defaults.Set("generic-flag-1", map[string]string{"_": "hello"}) f := &flagImpl{} f.init(stringMapper, []string{"generic-flag-1"}) select { case <-f.ChangeChan(): case <-time.After(1 * time.Second): t.Fatal("timeout waiting for change evnet") } expValue := "hello" value := f.get().(string) if value != expValue { t.Fatalf("expected value %q; got %q", expValue, value) } }
func TestEventStream(t *testing.T) { expCfg := map[string]string{ "key1": "value1", "key2": "value2", } defaults.Set("foo", expCfg) cfgChan, unsubChan := EventStream("foo") defer close(unsubChan) select { case cfg := <-cfgChan: if !reflect.DeepEqual(cfg, expCfg) { t.Fatalf("expected received config to be:\n%v\n\ngot:\n%v\n", expCfg, cfg) } case <-time.After(1 * time.Second): t.Fatal("failed to receive config after 100ms") } }
func TestUint32FlagUpdate(t *testing.T) { defaults.Set("uint32-flag", map[string]string{"flag": "123"}) f := Uint32("uint32-flag") select { case <-f.ChangeChan(): case <-time.After(1000 * time.Millisecond): t.Fatal("timeout waiting for flag change event") } var expVal uint32 = 123 val := f.Get() if val != expVal { t.Fatalf("expected val to be %d; got %d", expVal, val) } expVal = 999 f.Set(expVal) val = f.Get() if val != expVal { t.Fatalf("expected val to be %d; got %d", expVal, val) } }
func TestBoolFlagUpdate(t *testing.T) { defaults.Set("bool-flag", map[string]string{"flag": "true"}) f := Bool("bool-flag") select { case <-f.ChangeChan(): case <-time.After(1000 * time.Millisecond): t.Fatal("timeout waiting for flag change event") } expVal := true val := f.Get() if val != expVal { t.Fatalf("expected val to be %v; got %v", expVal, val) } expVal = false f.Set(expVal) val = f.Get() if val != expVal { t.Fatalf("expected val to be %v; got %v", expVal, val) } }
func TestFloat64FlagUpdate(t *testing.T) { defaults.Set("float64-flag", map[string]string{"flag": "123.0"}) f := Float64("float64-flag") select { case <-f.ChangeChan(): case <-time.After(1000 * time.Millisecond): t.Fatal("timeout waiting for flag change event") } var expVal float64 = 123.0 val := f.Get() if val != expVal { t.Fatalf("expected val to be %f; got %f", expVal, val) } expVal = 999.0 f.Set(expVal) val = f.Get() if val != expVal { t.Fatalf("expected val to be %f; got %f", expVal, val) } }