This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: go-synctest-bubble-quarantine

Go 1.24 testing/synctest Bubble Concurrency Quarantine

Mandates deterministic, zero-wall-time concurrency testing using Go 1.24's testing/synctest bubble quarantine for distributed locks, stateful orchestrators, and goroutine pools. Eliminates flaky time.Sleep heuristics and CPU-wasting poll loops by anchoring goroutines inside synthetic time bubbles where goroutine advancement is strictly coordinated.

Prohibited Anti-Pattern: Arbitrary Real-Time Sleep Heuristics and Leaked Goroutines in Concurrent State Testing

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Go 1.24 testing/synctest Bubble Concurrency Quarantine - Mandates deterministic, zero-wall-time concurrency testing using Go 1.24's testing/synctest bubble quarantine for distributed locks, stateful orchestrators, and goroutine pools. Eliminates flaky time.Sleep heuristics and CPU-wasting poll loops by anchoring goroutines inside synthetic time bubbles where goroutine advancement is strictly coordinated.
globs: **/*_test.go
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/go-synctest-bubble-quarantine"
---

# Go 1.24 testing/synctest Bubble Concurrency Quarantine

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Arbitrary Real-Time Sleep Heuristics and Leaked Goroutines in Concurrent State Testing

## System Constraints
You are a Principal Engineer enforcing Go 1.24 testing/synctest Bubble Concurrency Quarantine. Mandate invariants that eliminate non-deterministic time-based test heuristics (e.g., time.Sleep polling, arbitrary timeouts). Enforce strict isolation boundaries using synctest.Run, ensuring every goroutine under test is spawned within the synthetic bubble and coordinated via synctest.Wait(). Reject any test code that leaks goroutines or channels across bubble boundaries.

## Strict Guidelines
- Engineers MUST ALWAYS isolate concurrent goroutine lifecycle tests inside `synctest.Run(func() { ... })` bubbles.
- Engineers MUST NOT introduce wall-clock `time.Sleep` heuristics or arbitrary spin-lock polling to synchronize state assertions.
- Code MUST invoke `synctest.Wait()` to verify all bubble-spawned goroutines have settled into durable blocked states before asserting lock acquisitions or state transitions.
- Services MUST NOT leak channels, timers, or context handles across the `synctest.Run` quarantine boundary to prevent cross-bubble panics.
- Components NEVER rely on real-time wall clocks for distributed lease TTL tests; all timeouts MUST progress via virtual time in synctest.

## Bad vs Good Implementations
### Testing Distributed Lock Timeout and Re-acquisition

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Relies on real wall-clock sleeps (100ms+), which causes slow CI pipelines, race conditions under heavy CPU load, and non-deterministic failures when scheduling delays skew goroutine execution order.
```go
package lock_test

import (
	"sync/atomic"
	"testing"
	"time"
)

func TestDistributedLock_FlakySleep(t *testing.T) {
	var locked atomic.Bool
	// Goroutine acquires lock with TTL
	go func() {
		locked.Store(true)
		time.Sleep(100 * time.Millisecond) // Simulating lock duration
		locked.Store(false)
	}()

	// Anti-pattern: Flaky sleep-waiting on wall clock
	time.Sleep(20 * time.Millisecond)
	if !locked.Load() {
		t.Fatal("expected lock to be held")
	}

	// Anti-pattern: High-latency wait, prone to CI starvation
	time.Sleep(120 * time.Millisecond)
	if locked.Load() {
		t.Fatal("expected lock to be released after TTL")
	}
}
```

#### Production Standard (Correct)
> **Standard**: Encapsulates concurrency inside synctest.Run. Goroutines operate within a virtualized time bubble where time.Sleep progresses instantaneously when all goroutines reach durable block states, executing sub-millisecond, fully deterministic assertions.
```go
package lock_test

import (
	"sync/atomic"
	"testing"
	"testing/synctest"
	"time"
)

func TestDistributedLock_SynctestQuarantine(t *testing.T) {
	synctest.Run(func() {
		var locked atomic.Bool

		go func() {
			locked.Store(true)
			time.Sleep(100 * time.Millisecond) // Virtual clock instantly advances
			locked.Store(false)
		}()

		// synctest.Wait blocks until all goroutines in the bubble settle
		synctest.Wait()
		if !locked.Load() {
			t.Fatal("expected lock to be held during lease period")
		}

		// Advance fake time deterministically past TTL without real wall-clock delay
		time.Sleep(101 * time.Millisecond)
		synctest.Wait()

		if locked.Load() {
			t.Fatal("expected lock to be released after synthetic TTL expiration")
		}
	})
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Go 1.24 testing/synctest Bubble Concurrency Quarantine

Go 1.24 testing/synctest Bubble Concurrency Quarantine defines an industrial-grade behavioral standard for backend engineering. Mandates deterministic, zero-wall-time concurrency testing using Go 1.24's testing/synctest bubble quarantine for distributed locks, stateful orchestrators, and goroutine pools. Eliminates flaky time.Sleep heuristics and CPU-wasting poll loops by anchoring goroutines inside synthetic time bubbles where goroutine advancement is strictly coordinated. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Arbitrary Real-Time Sleep Heuristics and Leaked Goroutines in Concurrent State Testing Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftArbitrary Real-Time Sleep Heuristics and Leaked Goroutines in Concurrent State TestingCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS isolate concurrent goroutine lifecycle tests inside `synctest.Run(func() { ... })` bubbles.

Engineers MUST ALWAYS isolate concurrent goroutine lifecycle tests inside `synctest.Run(func() { ... })` bubbles.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT introduce wall-clock `time.Sleep` heuristics or arbitrary spin-lock polling to synchronize state assertions.

Engineers MUST NOT introduce wall-clock `time.Sleep` heuristics or arbitrary spin-lock polling to synchronize state assertions.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Code MUST invoke `synctest.Wait()` to verify all bubble-spawned goroutines have settled into durable blocked states before asserting lock acquisitions or state transitions.

Code MUST invoke `synctest.Wait()` to verify all bubble-spawned goroutines have settled into durable blocked states before asserting lock acquisitions or state transitions.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST NOT leak channels, timers, or context handles across the `synctest.Run` quarantine boundary to prevent cross-bubble panics.

Services MUST NOT leak channels, timers, or context handles across the `synctest.Run` quarantine boundary to prevent cross-bubble panics.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER rely on real-time wall clocks for distributed lease TTL tests

Components NEVER rely on real-time wall clocks for distributed lease TTL tests; all timeouts MUST progress via virtual time in synctest.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Go 1.24 testing/synctest Bubble Concurrency Quarantine critical for AI coding assistants?

Mandates deterministic, zero-wall-time concurrency testing using Go 1.24's testing/synctest bubble quarantine for distributed locks, stateful orchestrators, and goroutine pools. Eliminates flaky time.Sleep heuristics and CPU-wasting poll loops by anchoring goroutines inside synthetic time bubbles where goroutine advancement is strictly coordinated. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Arbitrary Real-Time Sleep Heuristics and Leaked Goroutines in Concurrent State Testing) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Go