Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

1.4 KB
Raw Blame
package instance_test

import (
	"sync"
	"testing"

	bridgepkg "github.com/pinchtab/pinchtab/internal/bridge"
	"github.com/pinchtab/pinchtab/internal/instance"
)

// Get/List/Running all return copies, so the repository is meant to own its
// instances. Add and Launch stored the caller's pointer instead, and the
// orchestrator keeps mutating that struct under its own lock — two unrelated
// mutexes over the same memory.
func TestRepositoryDoesNotAliasAddedInstance(t *testing.T) {
	repo := instance.NewRepository(newMockLauncher())

	inst := &bridgepkg.Instance{ID: "inst_1", Port: "9001", Status: "running"}
	repo.Add(inst)

	var wg sync.WaitGroup
	wg.Add(2)
	go func() {
		defer wg.Done()
		for i := 0; i < 500; i++ {
			inst.Status = "stopped"
			inst.Status = "running"
		}
	}()
	go func() {
		defer wg.Done()
		for i := 0; i < 500; i++ {
			_ = repo.Running()
			_ = repo.List()
		}
	}()
	wg.Wait()
}

// A snapshot taken through Add must not change when the caller mutates the
// struct it handed over.
func TestRepositoryAddSnapshotsInstance(t *testing.T) {
	repo := instance.NewRepository(newMockLauncher())

	inst := &bridgepkg.Instance{ID: "inst_1", Port: "9001", Status: "running"}
	repo.Add(inst)
	inst.Status = "stopped"

	got, ok := repo.Get("inst_1")
	if !ok {
		t.Fatal("instance missing from repository")
	}
	if got.Status != "running" {
		t.Errorf("Status = %q, want running — repository aliased the caller's struct", got.Status)
	}
}