Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix race tests #112

Merged
merged 1 commit into from
Feb 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
})
}
}
Loading