test(config): make HasLoadedConfigurationBeenModified deterministic (#1786)

* test(config): make HasLoadedConfigurationBeenModified deterministic

The two subtests each slept a full second so a rewritten file's mod time
(second precision) would advance past the load time. Advance the mod time
explicitly with os.Chtimes instead, and give the directory subtest its own
temp dir so the file subtest's future-dated config.yaml no longer leaks
into it. Removes ~2s of wall-clock sleep and the coarse-clock dependence;
the test is now deterministic.

* test(config): trim comments to the forward-facing rationale

---------

Co-authored-by: TwiN <twin@linux.com>
This commit is contained in:
Nikolaus Schuetz
2026-09-21 21:08:52 -04:00
committed by GitHub
co-authored by TwiN
parent 7f3873d0b4
commit 374de23cdd
+22 -7
View File
@@ -313,20 +313,32 @@ func TestConfig_HasLoadedConfigurationBeenModified(t *testing.T) {
if config.HasLoadedConfigurationBeenModified() {
t.Errorf("expected config.HasLoadedConfigurationBeenModified() to return false because nothing has happened since it was created")
}
time.Sleep(time.Second) // Because the file mod time only has second precision, we have to wait for a second
// Update the config file
if err = os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`endpoints:
if err = os.WriteFile(configFilePath, []byte(`endpoints:
- name: website
url: https://twin.sh/health
conditions:
- "[STATUS] == 200"`), 0o644); err != nil {
t.Fatalf("failed to overwrite config file: %v", err)
}
// File mod times have second precision, so advance it explicitly.
future := time.Now().Add(2 * time.Second)
if err = os.Chtimes(configFilePath, future, future); err != nil {
t.Fatalf("failed to advance config file mod time: %v", err)
}
if !config.HasLoadedConfigurationBeenModified() {
t.Errorf("expected config.HasLoadedConfigurationBeenModified() to return true because a new file has been added in the directory")
t.Errorf("expected config.HasLoadedConfigurationBeenModified() to return true because the config file has been modified")
}
})
t.Run("config-directory-as-config-path", func(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "config.yaml"), []byte(`endpoints:
- name: website
url: https://twin.sh/health
conditions:
- "[STATUS] == 200"
`), 0o644); err != nil {
t.Fatalf("failed to write config file: %v", err)
}
config, err := LoadConfiguration(dir)
if err != nil {
t.Fatalf("failed to load configuration: %v", err)
@@ -334,10 +346,13 @@ func TestConfig_HasLoadedConfigurationBeenModified(t *testing.T) {
if config.HasLoadedConfigurationBeenModified() {
t.Errorf("expected config.HasLoadedConfigurationBeenModified() to return false because nothing has happened since it was created")
}
time.Sleep(time.Second) // Because the file mod time only has second precision, we have to wait for a second
// Update the config file
if err = os.WriteFile(filepath.Join(dir, "metrics.yaml"), []byte(`metrics: true`), 0o644); err != nil {
t.Fatalf("failed to overwrite config file: %v", err)
t.Fatalf("failed to add config file: %v", err)
}
// File mod times have second precision, so advance it explicitly.
future := time.Now().Add(2 * time.Second)
if err = os.Chtimes(filepath.Join(dir, "metrics.yaml"), future, future); err != nil {
t.Fatalf("failed to advance config file mod time: %v", err)
}
if !config.HasLoadedConfigurationBeenModified() {
t.Errorf("expected config.HasLoadedConfigurationBeenModified() to return true because a new file has been added in the directory")