multiple agents
This commit is contained in:
1 parent
ec391bd7ed
commit
a399e1f365
6 files changed
+499
-658
No files matched your search
@@ -0,0 +1,169 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/go-rod/rod"
|
||||
)
|
||||
|
||||
// newZeroDaemon builds a Daemon with only the in-memory maps initialized and no
|
||||
// browser connection. It is sufficient to exercise the entry points that
|
||||
// validate their tab id before any Chrome DevTools Protocol call.
|
||||
func newZeroDaemon() *Daemon {
|
||||
return &Daemon{
|
||||
tabs: make(map[string]*rod.Page),
|
||||
iframePages: make(map[string]*rod.Page),
|
||||
consoleLogs: make(map[string][]ConsoleLog),
|
||||
}
|
||||
}
|
||||
|
||||
// TestTabRequired verifies the core multi-agent invariant: the daemon no longer
|
||||
// keeps a "current tab", so every operation that takes a tab id must reject an
|
||||
// empty one up front instead of silently falling back to shared state. Each of
|
||||
// these returns before touching the browser, so they can be tested without
|
||||
// Chrome.
|
||||
func TestTabRequired(t *testing.T) {
|
||||
d := newZeroDaemon()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
fn func() error
|
||||
}{
|
||||
{"getTab", func() error { _, err := d.getTab(""); return err }},
|
||||
{"closeTab", func() error { return d.closeTab("", 0) }},
|
||||
{"switchToMain", func() error { return d.switchToMain("") }},
|
||||
{"switchToIframe", func() error { return d.switchToIframe("", "#i", 0) }},
|
||||
{"getConsoleLogs", func() error { _, err := d.getConsoleLogs("", false); return err }},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := tc.fn()
|
||||
if err == nil {
|
||||
t.Fatalf("%s: expected an error for empty tab id, got nil", tc.name)
|
||||
}
|
||||
if !strings.Contains(strings.ToLower(err.Error()), "required") {
|
||||
t.Fatalf("%s: error should mention 'required', got: %v", tc.name, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestConsoleLogsConcurrent hammers getConsoleLogs from many goroutines (some
|
||||
// clearing, some reading) to confirm the per-tab log storage is race-free under
|
||||
// the stateMu discipline. Run with -race to catch any unprotected access.
|
||||
func TestConsoleLogsConcurrent(t *testing.T) {
|
||||
d := newZeroDaemon()
|
||||
const tabs = 8
|
||||
for i := 0; i < tabs; i++ {
|
||||
id := fmt.Sprintf("tab-%d", i)
|
||||
// getConsoleLogs only checks tab existence via d.tabs, so a nil page
|
||||
// entry is enough to let it reach the consoleLogs storage.
|
||||
d.tabs[id] = nil
|
||||
logs := make([]ConsoleLog, 0, 64)
|
||||
for j := 0; j < 64; j++ {
|
||||
logs = append(logs, ConsoleLog{Level: "info", Message: fmt.Sprintf("m-%d-%d", i, j)})
|
||||
}
|
||||
d.consoleLogs[id] = logs
|
||||
}
|
||||
|
||||
const goroutines = 64
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(goroutines)
|
||||
for g := 0; g < goroutines; g++ {
|
||||
go func(g int) {
|
||||
defer wg.Done()
|
||||
id := fmt.Sprintf("tab-%d", g%tabs)
|
||||
_, err := d.getConsoleLogs(id, g%2 == 0) // every other call clears
|
||||
if err != nil {
|
||||
t.Errorf("getConsoleLogs(%q): %v", id, err)
|
||||
}
|
||||
}(g)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
// TestMultiAgentIsolation is an integration test that requires a Chrome running
|
||||
// with remote debugging on port 9222. It is gated behind CREMOTE_INTEGRATION=1
|
||||
// so that `go test` does not open tabs in the host browser by default. When
|
||||
// enabled, run with -race to verify concurrent multi-agent access is race-free
|
||||
// and that each agent's tab content is isolated:
|
||||
//
|
||||
// CREMOTE_INTEGRATION=1 go test -race -run TestMultiAgentIsolation ./daemon/...
|
||||
func TestMultiAgentIsolation(t *testing.T) {
|
||||
if os.Getenv("CREMOTE_INTEGRATION") != "1" {
|
||||
t.Skip("set CREMOTE_INTEGRATION=1 to run the Chrome-backed integration test")
|
||||
}
|
||||
if !checkChromeDevTools(9222) {
|
||||
t.Skip("chrome remote debugging not available on :9222")
|
||||
}
|
||||
|
||||
d, err := NewDaemon("127.0.0.1", freePort(t), false)
|
||||
if err != nil {
|
||||
t.Fatalf("NewDaemon: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = d.Stop() })
|
||||
go func() { _ = d.Start() }()
|
||||
|
||||
const agents = 6
|
||||
var wg sync.WaitGroup
|
||||
errs := make([]error, agents)
|
||||
tabs := make([]string, agents)
|
||||
wg.Add(agents)
|
||||
for a := 0; a < agents; a++ {
|
||||
go func(agent int) {
|
||||
defer wg.Done()
|
||||
tab, err := d.openTab(30)
|
||||
if err != nil {
|
||||
errs[agent] = fmt.Errorf("openTab: %w", err)
|
||||
return
|
||||
}
|
||||
tabs[agent] = tab
|
||||
|
||||
marker := fmt.Sprintf("AGENT-MARKER-%d", agent)
|
||||
url := "data:text/html,<html><body><h1>" + marker + "</h1></body></html>"
|
||||
if err := d.loadURL(tab, url, 30); err != nil {
|
||||
errs[agent] = fmt.Errorf("loadURL: %w", err)
|
||||
return
|
||||
}
|
||||
src, err := d.getPageSource(tab, 30)
|
||||
if err != nil {
|
||||
errs[agent] = fmt.Errorf("getPageSource: %w", err)
|
||||
return
|
||||
}
|
||||
if !strings.Contains(src, marker) {
|
||||
errs[agent] = fmt.Errorf("agent %d: page source missing %q (possible cross-agent leak)", agent, marker)
|
||||
}
|
||||
}(a)
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
// Clean up any tabs that were opened.
|
||||
for _, tab := range tabs {
|
||||
if tab != "" {
|
||||
_ = d.closeTab(tab, 30)
|
||||
}
|
||||
}
|
||||
|
||||
for i, err := range errs {
|
||||
if err != nil {
|
||||
t.Errorf("agent %d failed: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// freePort returns an available TCP port for the daemon HTTP listener.
|
||||
func freePort(t *testing.T) int {
|
||||
t.Helper()
|
||||
l, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
port := l.Addr().(*net.TCPAddr).Port
|
||||
l.Close()
|
||||
return port
|
||||
}
|
||||
Reference in new issue
Block a user