feat: centralize Barkfile schema
This commit is contained in:
75
watch_test.go
Normal file
75
watch_test.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package barkfile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestWatchDeliversInitialAndChangedConfiguration(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "Barkfile")
|
||||
writeWatchConfig(t, path, ":5432")
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
changes, err := Watch(ctx, path, WatchOptions{PollInterval: 10 * time.Millisecond})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if change := nextChange(t, changes); change.Err != nil || change.Config.Listen != ":5432" {
|
||||
t.Fatalf("initial change = %#v", change)
|
||||
}
|
||||
|
||||
writeWatchConfig(t, path, ":6543")
|
||||
if change := nextChange(t, changes); change.Err != nil || change.Config.Listen != ":6543" {
|
||||
t.Fatalf("changed configuration = %#v", change)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWatchReportsInvalidRevisionAndRecovers(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "Barkfile")
|
||||
writeWatchConfig(t, path, ":5432")
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
changes, err := Watch(ctx, path, WatchOptions{PollInterval: 10 * time.Millisecond})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = nextChange(t, changes)
|
||||
if err := os.WriteFile(path, []byte("pawsql {\n listen :5432\n}"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if change := nextChange(t, changes); change.Err == nil {
|
||||
t.Fatal("invalid revision did not report an error")
|
||||
}
|
||||
|
||||
writeWatchConfig(t, path, ":7654")
|
||||
if change := nextChange(t, changes); change.Err != nil || change.Config.Listen != ":7654" {
|
||||
t.Fatalf("recovered configuration = %#v", change)
|
||||
}
|
||||
}
|
||||
|
||||
func nextChange(t *testing.T, changes <-chan Change) Change {
|
||||
t.Helper()
|
||||
select {
|
||||
case change, open := <-changes:
|
||||
if !open {
|
||||
t.Fatal("watch channel closed before delivering a change")
|
||||
}
|
||||
return change
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("timed out waiting for Barkfile change")
|
||||
return Change{}
|
||||
}
|
||||
}
|
||||
|
||||
func writeWatchConfig(t *testing.T, path, listen string) {
|
||||
t.Helper()
|
||||
contents := "pawsql {\n listen " + listen + "\n tls {\n cert cert.pem\n key key.pem\n }\n database app {\n upstream 127.0.0.1:5432\n }\n}\n"
|
||||
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user