Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as opaque submodule gitlinks on GitHub) into fully-tracked first-party content. AITM now merges its 7 vendored repos as first-party engines: - memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts - git: vendored C git (bundled binary) + gitnexus (in-process code intelligence) - agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess) Includes the full session's product build: Gitgram web/mobile/backend features, AITM website/CLI/MCP, the unified memory layer, engine build tooling (scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md), and extensive test suites. Build artifacts, node_modules, external/, and gemini-cli are gitignored. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
177 lines
4.6 KiB
Go
177 lines
4.6 KiB
Go
package lockfile
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestFlockFunctions(t *testing.T) {
|
|
t.Run("FlockExclusiveBlocking and FlockUnlock", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
lockPath := filepath.Join(tmpDir, "test.lock")
|
|
|
|
if err := os.WriteFile(lockPath, []byte("test"), 0644); err != nil {
|
|
t.Fatalf("failed to create lock file: %v", err)
|
|
}
|
|
|
|
f, err := os.OpenFile(lockPath, os.O_RDWR, 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to open lock file: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
if err := FlockExclusiveBlocking(f); err != nil {
|
|
t.Errorf("FlockExclusiveBlocking failed: %v", err)
|
|
}
|
|
|
|
if err := FlockUnlock(f); err != nil {
|
|
t.Errorf("FlockUnlock failed: %v", err)
|
|
}
|
|
})
|
|
|
|
t.Run("flockExclusive non-blocking succeeds on unlocked file", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
lockPath := filepath.Join(tmpDir, "test.lock")
|
|
|
|
if err := os.WriteFile(lockPath, []byte("test"), 0644); err != nil {
|
|
t.Fatalf("failed to create lock file: %v", err)
|
|
}
|
|
|
|
f, err := os.OpenFile(lockPath, os.O_RDWR, 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to open lock file: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
if err := flockExclusive(f); err != nil {
|
|
t.Errorf("flockExclusive should succeed on unlocked file: %v", err)
|
|
}
|
|
|
|
FlockUnlock(f)
|
|
})
|
|
|
|
t.Run("flockExclusive returns errProcessLocked when already locked", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
lockPath := filepath.Join(tmpDir, "test.lock")
|
|
|
|
if err := os.WriteFile(lockPath, []byte("test"), 0644); err != nil {
|
|
t.Fatalf("failed to create lock file: %v", err)
|
|
}
|
|
|
|
f1, err := os.OpenFile(lockPath, os.O_RDWR, 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to open lock file: %v", err)
|
|
}
|
|
defer f1.Close()
|
|
|
|
if err := FlockExclusiveBlocking(f1); err != nil {
|
|
t.Fatalf("failed to acquire first lock: %v", err)
|
|
}
|
|
defer FlockUnlock(f1)
|
|
|
|
f2, err := os.OpenFile(lockPath, os.O_RDWR, 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to open second lock file handle: %v", err)
|
|
}
|
|
defer f2.Close()
|
|
|
|
err = flockExclusive(f2)
|
|
if err != errProcessLocked {
|
|
t.Errorf("expected errProcessLocked, got %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestFlockExclusiveNonBlocking(t *testing.T) {
|
|
t.Run("succeeds on unlocked file", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
lockPath := filepath.Join(tmpDir, "test.lock")
|
|
|
|
if err := os.WriteFile(lockPath, []byte("test"), 0644); err != nil {
|
|
t.Fatalf("failed to create lock file: %v", err)
|
|
}
|
|
|
|
f, err := os.OpenFile(lockPath, os.O_RDWR, 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to open lock file: %v", err)
|
|
}
|
|
defer f.Close()
|
|
|
|
err = FlockExclusiveNonBlocking(f)
|
|
if err != nil {
|
|
t.Errorf("FlockExclusiveNonBlocking should succeed on unlocked file: %v", err)
|
|
}
|
|
|
|
FlockUnlock(f)
|
|
})
|
|
|
|
t.Run("returns ErrLocked when already locked", func(t *testing.T) {
|
|
tmpDir := t.TempDir()
|
|
lockPath := filepath.Join(tmpDir, "test.lock")
|
|
|
|
if err := os.WriteFile(lockPath, []byte("test"), 0644); err != nil {
|
|
t.Fatalf("failed to create lock file: %v", err)
|
|
}
|
|
|
|
// Acquire lock with first handle
|
|
f1, err := os.OpenFile(lockPath, os.O_RDWR, 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to open lock file: %v", err)
|
|
}
|
|
defer f1.Close()
|
|
|
|
if err := FlockExclusiveBlocking(f1); err != nil {
|
|
t.Fatalf("failed to acquire first lock: %v", err)
|
|
}
|
|
defer FlockUnlock(f1)
|
|
|
|
// Try non-blocking lock with second handle
|
|
f2, err := os.OpenFile(lockPath, os.O_RDWR, 0644)
|
|
if err != nil {
|
|
t.Fatalf("failed to open second handle: %v", err)
|
|
}
|
|
defer f2.Close()
|
|
|
|
err = FlockExclusiveNonBlocking(f2)
|
|
if !IsLocked(err) {
|
|
t.Errorf("expected IsLocked to be true, got error: %v", err)
|
|
}
|
|
if err != ErrLocked {
|
|
t.Errorf("expected ErrLocked, got: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestIsLocked(t *testing.T) {
|
|
t.Run("true for ErrLocked", func(t *testing.T) {
|
|
if !IsLocked(ErrLocked) {
|
|
t.Error("IsLocked should return true for ErrLocked")
|
|
}
|
|
})
|
|
|
|
t.Run("false for nil", func(t *testing.T) {
|
|
if IsLocked(nil) {
|
|
t.Error("IsLocked should return false for nil")
|
|
}
|
|
})
|
|
|
|
t.Run("false for other errors", func(t *testing.T) {
|
|
if IsLocked(fmt.Errorf("some other error")) {
|
|
t.Error("IsLocked should return false for non-lock errors")
|
|
}
|
|
})
|
|
|
|
// born-failing guard for the errors.Is fix (commit 1a83cb08c): pre-fix
|
|
// pointer-equality (err == errProcessLocked) returned false for a wrapped
|
|
// ErrLocked, so callers using fmt.Errorf("...: %w", ErrLocked) lost
|
|
// lock-contention detection.
|
|
t.Run("true for wrapped ErrLocked", func(t *testing.T) {
|
|
wrapped := fmt.Errorf("acquire lock: %w", ErrLocked)
|
|
if !IsLocked(wrapped) {
|
|
t.Errorf("IsLocked should return true for wrapped ErrLocked, got false for: %v", wrapped)
|
|
}
|
|
})
|
|
}
|