用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/gar-ai/mallorn --skill rust-async-testing命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
Manage Git worktrees for parallel feature development. Use when the user wants to work on multiple branches simultaneously, create worktrees, switch between features, open terminals/editors for worktrees, or manage parallel development workflows.
Unified linting scripts for all services in the monorepo. Use when the user wants to lint code, check formatting, or run type checks.
Build mithril-cache for torch.compile caching. Use when implementing content-addressable storage, cache keys, eviction, or framework hooks.
基于 SOC 职业分类
正在显示 SKILL.md
| name | rust-async-testing |
| description | Write async tests with tokio::test and configure multi-threaded test runtimes. Use when testing async code. |
Patterns for testing async Rust code with Tokio.
#[tokio::test]
async fn test_fetch_data() {
let result = fetch_data().await;
assert!(result.is_ok());
assert_eq!(result.unwrap().len(), 10);
}
// Test with multiple worker threads
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_concurrent_operations() {
let (a, b) = tokio::join!(
operation_a(),
operation_b(),
);
assert!(a.is_ok());
assert!(b.is_ok());
}
// Current-thread runtime (simpler, deterministic)
#[tokio::test(flavor = "current_thread")]
async fn test_sequential() {
let result = sequential_operation().await;
assert!(result.is_ok());
}
use tokio::time::{timeout, Duration};
#[tokio::test]
async fn test_with_timeout() {
let result = timeout(Duration::from_secs(5), slow_operation())
.await
.expect("Operation timed out")
.expect("Operation failed");
assert_eq!(result, expected_value);
}
struct TestContext {
pool: PgPool,
repo: VideoRepository,
}
impl TestContext {
async fn new() -> Self {
let pool = create_test_pool().await;
let repo = VideoRepository::new(pool.clone());
Self { pool, repo }
}
async fn cleanup(&self) {
sqlx::query("DELETE FROM test_videos")
.execute(&self.pool)
.await
.unwrap();
}
}
#[tokio::test]
async fn test_with_fixture() {
let ctx = TestContext::new().await;
// Test code
let result = ctx.repo.create_video("test-1").await;
assert!(result.is_ok());
// Cleanup
ctx.cleanup().await;
}
use tokio::sync::mpsc;
#[tokio::test]
async fn test_channel_communication() {
let (tx, mut rx) = mpsc::channel(10);
// Spawn producer
tokio::spawn(async move {
for i in 0..5 {
tx.send(i).await.unwrap();
}
});
// Collect results
let mut results = vec![];
while let Some(value) = rx.recv().await {
results.push(value);
}
assert_eq!(results, vec![0, 1, 2, 3, 4]);
}
use tokio::time::{self, Duration, Instant};
#[tokio::test]
async fn test_with_paused_time() {
time::pause(); // Pause time
let start = Instant::now();
// This completes instantly with paused time
time::sleep(Duration::from_secs(100)).await;
// Advance time manually
time::advance(Duration::from_secs(50)).await;
assert!(start.elapsed() >= Duration::from_secs(150));
}
#[tokio::test]
async fn test_spawned_task() {
let handle = tokio::spawn(async {
compute_result().await
});
let result = handle.await.expect("Task panicked");
assert_eq!(result, expected_value);
}
#[tokio::test]
async fn test_multiple_tasks() {
let handles: Vec<_> = (0..10)
.map(|i| tokio::spawn(async move { process(i).await }))
.collect();
let results: Vec<_> = futures::future::join_all(handles)
.await
.into_iter()
.map(|r| r.expect("Task panicked"))
.collect();
assert_eq!(results.len(), 10);
}
#[tokio::test]
async fn test_error_handling() {
let result = operation_that_fails().await;
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
ProcessingError::NotFound(_)
));
}
#[tokio::test]
#[should_panic(expected = "connection refused")]
async fn test_panic_case() {
connect_to_invalid_server().await;
}
// tests/integration_test.rs
use my_crate::{Config, Service};
async fn setup() -> Service {
let config = Config::test_default();
Service::new(config).await.unwrap()
}
#[tokio::test]
async fn test_full_workflow() {
let service = setup().await;
// Step 1
let id = service.create_item("test").await.unwrap();
// Step 2
let item = service.get_item(&id).await.unwrap();
assert_eq!(item.name, "test");
// Step 3
service.delete_item(&id).await.unwrap();
assert!(service.get_item(&id).await.is_err());
}
#[cfg(test)]
mod tests {
use super::*;
// Unit tests for specific functions
mod parse_tests {
use super::*;
#[test]
fn test_parse_valid() { ... }
#[test]
fn test_parse_invalid() { ... }
}
// Async tests
mod async_tests {
use super::*;
#[tokio::test]
async fn test_async_operation() { ... }
}
// Helper functions for tests
fn create_test_data() -> TestData { ... }
}
#[tokio::test] for async testsflavor = "multi_thread" for concurrent teststime::pause() for deterministic time-based tests#[should_panic] for panic testsSee hercules-local-algo/src/ for inline test modules.