Skip to content

Commit

Permalink
fix race tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Feb 10, 2024
1 parent ce69ab9 commit f663fca
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions provider/file/watch_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
// Copyright (c) 2024 The konf authors
// Use of this source code is governed by a MIT license found in the LICENSE file.

//go:build !race

package file_test

import (
"context"
"os"
"path/filepath"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -48,24 +47,25 @@ func TestFile_Watch(t *testing.T) {
assert.NoError(t, os.WriteFile(tmpFile, []byte(`{"p": {"k": "v"}}`), 0o600))

loader := file.New(tmpFile)
var values map[string]any
var values atomic.Pointer[map[string]any]
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

var waitGroup sync.WaitGroup
waitGroup.Add(1)
go func() {
waitGroup.Done()
err := loader.Watch(ctx, func(changed map[string]any) {
defer waitGroup.Done()
values = changed
values.Store(&changed)
})
assert.NoError(t, err)
}()
waitGroup.Wait()

time.Sleep(time.Second)
time.Sleep(100 * time.Millisecond)
assert.NoError(t, testcase.action(tmpFile))
waitGroup.Wait()
assert.Equal(t, testcase.expected, values)
time.Sleep(100 * time.Millisecond)
assert.Equal(t, testcase.expected, *values.Load())
})
}
}

0 comments on commit f663fca

Please sign in to comment.