Skip to content

Commit

Permalink
remove race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Spehlmann committed Jun 5, 2021
1 parent 332818b commit bfb5efa
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions internal/driver/config/namespace_watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"github.com/ghodss/yaml"
"github.com/ory/x/logrusx"
Expand All @@ -21,22 +20,6 @@ import (
"github.com/ory/keto/internal/namespace"
)

func runWithTimeOut(t *testing.T, duration time.Duration, testfn func(*testing.T)) {
timeout := time.After(duration)
done := make(chan bool)

go func() {
testfn(t)
done <- true
}()

select {
case <-timeout:
t.Fatalf("test did not complete after %s\n", duration)
case <-done:
}
}

func TestNamespaceProvider(t *testing.T) {
setup := func(t *testing.T, target string) (*NamespaceWatcher, *test.Hook) {
ctx, cancel := context.WithCancel(context.Background())
Expand Down Expand Up @@ -223,15 +206,13 @@ func TestNamespaceProvider(t *testing.T) {
// write malformed, force file update
require.NoError(t, ioutil.WriteFile(fn, []byte(`{"name":123,"id":"malformed"}`), 0600))

runWithTimeOut(t, 100*time.Millisecond, func(t *testing.T) {
for {
files := ws.NamespaceFiles()
assert.Equal(t, 1, len(files))
assert.NotNil(t, files[0].namespace)
if strings.Contains(string(files[0].Contents), "malformed") { // recieved update
break
}
}
})
done, err := ws.w.DispatchNow()
require.NoError(t, err)
<-done

files := ws.NamespaceFiles()
assert.Equal(t, 1, len(files))
assert.NotNil(t, files[0].namespace)
assert.Contains(t, string(files[0].Contents), "malformed")
})
}

0 comments on commit bfb5efa

Please sign in to comment.