Skip to content

Commit

Permalink
fix race tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ktong committed Nov 12, 2023
1 parent 7c48f92 commit 7124ef4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
13 changes: 6 additions & 7 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package konf_test

import (
"context"
"sync/atomic"
"testing"

"github.com/ktong/konf"
Expand Down Expand Up @@ -55,15 +56,13 @@ func BenchmarkWatch(b *testing.B) {
}()
b.ResetTimer()

var cfg atomic.Value
konf.OnChange(func() {
cfg.Store(konf.Get[string]("config"))
})
for i := 0; i < b.N; i++ {
watcher.change(map[string]any{"config": "changed"})
}
b.StopTimer()

var cfg string
konf.OnChange(func() {
assert.NoError(b, konf.Unmarshal("config", &cfg))
})
watcher.change(map[string]any{"config": "changed"})
assert.Equal(b, "changed", cfg)
assert.Equal(b, "changed", cfg.Load())
}
6 changes: 5 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package konf_test
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -167,11 +168,14 @@ func TestConfig_Watch(t *testing.T) {
assert.NoError(t, config.Watch(ctx))
}()

var newCfg atomic.Value
config.OnChange(func(unmarshaler konf.Unmarshaler) {
var cfg string
assert.NoError(t, config.Unmarshal("config", &cfg))
newCfg.Store(cfg)
})
watcher.change(map[string]any{"config": "changed"})
assert.Equal(t, "changed", cfg)
assert.Equal(t, "changed", newCfg.Load())
}

type mapWatcher chan map[string]any
Expand Down
7 changes: 4 additions & 3 deletions global_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bytes"
"context"
"log"
"sync/atomic"
"testing"

"github.com/ktong/konf"
Expand Down Expand Up @@ -64,10 +65,10 @@ func TestWatch(t *testing.T) {
assert.NoError(t, konf.Watch(ctx))
}()

var cfg string
var cfg atomic.Value
konf.OnChange(func() {
assert.NoError(t, konf.Unmarshal("config", &cfg))
cfg.Store(konf.Get[string]("config"))
})
watcher.change(map[string]any{"config": "changed"})
assert.Equal(t, "changed", cfg)
assert.Equal(t, "changed", cfg.Load())
}

0 comments on commit 7124ef4

Please sign in to comment.