diff --git a/utils/configobserver.go b/utils/configobserver.go index cd5856a4..6df6dc83 100644 --- a/utils/configobserver.go +++ b/utils/configobserver.go @@ -16,7 +16,7 @@ type ConfigBuilder[T any] interface { } type ConfigDefaulter[T any] interface { - InitDefaults(*T) + InitDefaults(*T) error } type ConfigObserver[T any] struct { diff --git a/utils/configobserver_test.go b/utils/configobserver_test.go new file mode 100644 index 00000000..932411c0 --- /dev/null +++ b/utils/configobserver_test.go @@ -0,0 +1,60 @@ +package utils + +import ( + "os" + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +const testConfig0 = `foo: a` +const testConfig1 = `foo: b` + +type TestConfig struct { + Foo string `yaml:"foo"` + Bar string `yaml:"bar"` +} + +type testConfigBuilder struct{} + +func (testConfigBuilder) New() (*TestConfig, error) { + return &TestConfig{}, nil +} + +func (testConfigBuilder) InitDefaults(c *TestConfig) error { + c.Bar = "c" + return nil +} + +func TestConfigObserver(t *testing.T) { + f, err := os.CreateTemp(os.TempDir(), "lk-test-*.yaml") + t.Cleanup(func() { + _ = f.Close() + }) + require.NoError(t, err) + _, err = f.WriteString(testConfig0) + require.NoError(t, err) + + obs, conf, err := NewConfigObserver(f.Name(), testConfigBuilder{}) + require.NoError(t, err) + + require.Equal(t, "a", conf.Foo) + require.Equal(t, "c", conf.Bar) + + done := make(chan struct{}) + obs.Observe(func(c *TestConfig) { + require.Equal(t, "b", c.Foo) + require.Equal(t, "c", c.Bar) + close(done) + }) + + _, err = f.WriteAt([]byte(testConfig1), 0) + require.NoError(t, err) + + select { + case <-done: + case <-time.After(100 * time.Millisecond): + require.FailNow(t, "timed out waiting for config update") + } +}