//go:build unix package hostguard import ( "context" "errors" "github.com/carlosframework/platform/internal/hostclaim" "os" "path/filepath" "testing" "time" ) func TestWithLocalSerializesWithoutClaimAuthority(t *testing.T) { dir := t.TempDir() locks := t.TempDir() if err := os.Mkdir(filepath.Join(dir, "hostguard"), 0770); err != nil { t.Fatal(err) } g := Guard{DataDir: dir, LockDir: locks} entered := make(chan struct{}) release := make(chan struct{}) done := make(chan error, 1) go func() { done <- g.WithLocal(t.Context(), "shop.example.com", func() error { close(entered); <-release; return nil }) }() <-entered ctx, cancel := context.WithTimeout(t.Context(), 25*time.Millisecond) defer cancel() called := false err := g.WithLocal(ctx, "shop.example.com", func() error { called = true; return nil }) close(release) if first := <-done; first != nil { t.Fatal(first) } if !errors.Is(err, context.DeadlineExceeded) || called { t.Fatalf("concurrent local write: %v %v", err, called) } // No Claims reader is configured: WithLocal may clean up, but With cannot // turn the same lock into installation authority. if err := g.With(t.Context(), "shop.example.com", Captured{}, func(_ *hostclaim.Claim) error { return nil }, func() error { return nil }); !errors.Is(err, ErrNoClaims) { t.Fatal(err) } }