55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// postgres's own part of the moved path (issue 849): the shared driving of it is proven in the SDK's harness package.
|
|
|
|
// Where the file is not there, postgres takes the grant directory, as before; where it is, the helper.
|
|
func TestTheGrantPathIsTakenWhereTheConsumersFileIsAbsent(t *testing.T) {
|
|
dir := t.TempDir()
|
|
at := filepath.Join(dir, "consumers.json")
|
|
env := func(k string) string {
|
|
if k == "MESH_CONSUMERS_FILE" {
|
|
return at
|
|
}
|
|
return ""
|
|
}
|
|
if got := handedConsumersFile(env, os.Stat); got != "" {
|
|
t.Fatalf("took %q for a file that is not there", got)
|
|
}
|
|
if got := handedConsumersFile(func(string) string { return "" }, os.Stat); got != "" {
|
|
t.Fatalf("took %q with no environment", got)
|
|
}
|
|
if err := os.WriteFile(at, []byte("[]"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := handedConsumersFile(env, os.Stat); got != at {
|
|
t.Fatalf("took %q, want %q", got, at)
|
|
}
|
|
}
|
|
|
|
// The grant path restarts itself when the consumers file is first filled.
|
|
func TestTheGrantPathLeavesWhenTheConsumersFileIsFilled(t *testing.T) {
|
|
at := filepath.Join(t.TempDir(), "consumers.json")
|
|
done := make(chan struct{})
|
|
go exitWhenFilled(at, os.Stat, time.Millisecond, func() { close(done) })
|
|
select {
|
|
case <-done:
|
|
t.Fatal("left before the file was there")
|
|
case <-time.After(20 * time.Millisecond):
|
|
}
|
|
if err := os.WriteFile(at, []byte("[]"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
select {
|
|
case <-done:
|
|
case <-time.After(2 * time.Second):
|
|
t.Fatal("did not leave when the file was filled")
|
|
}
|
|
}
|