rust-async-testing
Rust Async Testing Patterns
Best practices for testing async Rust code with Tokio.
Core Patterns
Basic Async Test
#[tokio::test]
async fn test_episode_creation() {
let memory = SelfLearningMemory::new(Default::default()).await?;
let id = memory.start_episode("Test", ctx, TaskType::CodeGen).await;
assert!(!id.is_empty());
}
Time-Based Testing
#[tokio::test(start_paused = true)]
async fn test_timeout_behavior() {
// Time advances only when awaited
let start = tokio::time::Instant::now();
tokio::time::sleep(Duration::from_secs(5)).await;
assert!(start.elapsed().as_millis() < 100);
}
Concurrent Operations
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn test_concurrent_episodes() {
let memory = Arc::new(setup_memory().await);
let handles: Vec<_> = (0..10).map(|i| {
let mem = memory.clone();
tokio::spawn(async move {
mem.start_episode(format!("Task {}", i), ctx, type_).await
})
}).collect();
let results = futures::future::join_all(handles).await;
assert_eq!(results.len(), 10);
}
Timeout Testing
#[tokio::test]
async fn test_operation_timeout() {
let result = tokio::time::timeout(
Duration::from_secs(2),
slow_operation()
).await;
assert!(result.is_err());
}
Best Practices
- Use
#[tokio::test]instead ofblock_on - Enable
start_paused = truefor time tests - Use
multi_threadfor concurrency tests - Mock external dependencies
- Test error paths
Common Pitfalls
| Bad | Good |
|---|---|
std::thread::sleep() |
tokio::time::sleep().await |
memory.start_episode() |
memory.start_episode().await |
| Single-threaded for concurrency | multi_thread runtime |
Memory-Specific Pattern
#[tokio::test]
async fn test_complete_lifecycle() {
let memory = setup_memory().await;
// Start → Log Steps → Complete → Verify
let id = memory.start_episode("test", ctx, type_).await;
memory.log_execution_step(id.clone(), step).await;
memory.complete_episode(id.clone(), outcome, None).await?;
let episode = memory.get_episode(&id).await?;
assert_eq!(episode.outcome, outcome);
}
More from d-o-hub/rust-self-learning-memory
loop-agent
Execute workflow agents iteratively for refinement and progressive improvement until quality criteria are met. Use when tasks require repetitive refinement, multi-iteration improvements, progressive optimization, or feedback loops until convergence.
51web-search-researcher
Research topics using web search and content fetching to find accurate, current information. Use when you need modern information, official documentation, best practices, technical solutions, or comparisons beyond your training data.
46perplexity-researcher-reasoning-pro
Highest level of research and reasoning capabilities for complex decision-making with significant consequences, strategic planning, technical architecture decisions, multi-stakeholder problems, or high-complexity troubleshooting requiring expert-level judgment and sophisticated reasoning chains. Prioritizes actively maintained repositories and validates website sources for 2025 relevance.
44context-retrieval
Retrieve relevant episodic context from memory for informed decision-making. Use when you need past episodes, patterns, or solutions to similar tasks.
44rust-code-quality
Perform comprehensive Rust code quality reviews against best practices for async Rust, error handling, testing, and project structure
43codebase-analyzer
Analyze implementation details, trace data flow, explain technical workings, locate files, and consolidate codebases. Use when you need to understand HOW code works, find file locations, or assess technical debt.
40