使用WaitGroup和Channel控制并发测试,避免竞态条件。通过传入*sync.WaitGroup同步协程完成,用channel替代time.Sleep控制执行时机,结合select与超时确保测试可靠。

Go语言的并发编程能力是其核心优势之一,但这也给单元测试带来了挑战。如何对包含
goroutine
channel
sync
当被测函数启动多个
goroutine
sync.WaitGroup
*sync.WaitGroup
示例:
func ProcessTasks(tasks []string, wg *sync.WaitGroup) {
for _, task := range tasks {
wg.Add(1)
go func(t string) {
defer wg.Done()
// 模拟处理
fmt.Println("Processed:", t)
}(task)
}
}测试中可以这样验证:
立即学习“go语言免费学习笔记(深入)”;
func TestProcessTasks(t *testing.T) {
var wg sync.WaitGroup
tasks := []string{"a", "b", "c"}
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">ProcessTasks(tasks, &wg)
wg.Wait() // 等待所有协程结束
// 此处可断言预期结果(如共享变量状态)}
对于依赖消息传递的并发函数,可通过注入
chan
time.Sleep
建议做法:
select
time.After
示例:
func Monitor(stopCh <-chan struct{}, resultCh chan<- int) {
count := 0
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">for {
select {
case <-ticker.C:
count++
case <-stopCh:
resultCh <- count
return
}
}}
测试:
func TestMonitor_StopsGracefully(t *testing.T) {
stopCh := make(chan struct{})
resultCh := make(chan int, 1)
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">go Monitor(stopCh, resultCh)
time.Sleep(50 * time.Millisecond)
close(stopCh)
select {
case count := <-resultCh:
if count == 0 {
t.Fatal("expected non-zero count")
}
case <-time.After(100 * time.Millisecond):
t.Fatal("timeout waiting for result")
}}
当并发函数依赖外部服务或复杂接口时,使用testify/mock创建可控的模拟对象。这样可以隔离并发逻辑,专注于测试目标函数的行为。
例如,一个并发调用API的worker:
type APIClient interface {
Fetch(id string) (Data, error)
}在测试中mock该接口,并设定返回值与延迟,验证并发请求是否正确处理错误、超时或重试。
Go内置的竞态检测器是并发测试的重要工具。运行测试时加上
-race
go test -race ./...
它能自动发现大多数读写冲突。虽然会降低性能,但在CI中定期运行能有效捕捉潜在问题。
基本上就这些。关键是让并发逻辑可观察、可控制,避免依赖时间顺序,同时善用工具保障安全性。测试不是要复制真实并发环境,而是验证关键路径的正确性与鲁棒性。
以上就是Golang并发函数单元测试实践技巧的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号