Files
Memoh/internal/agent/guard_state_test.go
T
Fodesu db777b98ac fix(agent): stream loop abort, mid-stream retry parity, collector cleanup (#376)
* fix(agent): align stream retry abort and event collection

* fix(agent): cancel stream on loop detect, harden retry and tool events

* fix(agent): drain previous stream before retry

* fix(lint): ctx ci lint

---------

Co-authored-by: 晨苒 <16112591+chen-ran@users.noreply.github.com>
2026-04-18 03:19:58 +08:00

66 lines
1.4 KiB
Go

package agent
import (
"fmt"
"sync"
"sync/atomic"
"testing"
"github.com/memohai/memoh/internal/agent/tools"
)
func TestToolAbortRegistryConcurrentAccess(t *testing.T) {
registry := newToolAbortRegistry()
var wg sync.WaitGroup
for i := 0; i < 32; i++ {
wg.Add(1)
go func(i int) {
defer wg.Done()
id := fmt.Sprintf("call-%d", i)
registry.Add(id)
if !registry.Any() {
t.Error("expected registry to report pending aborts")
}
if !registry.Take(id) {
t.Errorf("expected to take %s", id)
}
}(i)
}
wg.Wait()
if registry.Any() {
t.Fatal("expected registry to be empty")
}
}
func TestToolEventCollectorCloseIgnoresLateAdds(t *testing.T) {
collector := newToolEventCollector()
for i := 0; i < 5; i++ {
if !collector.Add(tools.ToolStreamEvent{Type: tools.StreamEventSpawnHeartbeat}) {
t.Fatalf("unexpected add failure before close, i=%d", i)
}
}
snapshot := collector.CloseAndSnapshot()
if len(snapshot) != 5 {
t.Fatalf("expected snapshot len 5, got %d", len(snapshot))
}
var wg sync.WaitGroup
var postCloseAdds atomic.Int32
for i := 0; i < 16; i++ {
wg.Add(1)
go func() {
defer wg.Done()
if collector.Add(tools.ToolStreamEvent{Type: tools.StreamEventSpawnHeartbeat}) {
postCloseAdds.Add(1)
}
}()
}
wg.Wait()
if postCloseAdds.Load() != 0 {
t.Fatalf("expected 0 successful adds after close, got %d", postCloseAdds.Load())
}
}