你能否让这个“不正确同步”的测试失败? [英] Can you make this 'incorrectly synchronized' test fail?

查看:92
本文介绍了你能否让这个“不正确同步”的测试失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果是这样,是否有可能使它失败(在某种程度上而不用改变测试代码本身?

  1. Can this test ever fail on x86, x64, ARM?
  2. If so, is it possible to make it fail (in some sort of setup) and without changing the test code itself?

func Test_WaitGroup_Simple(t *testing.T) {
    var condition bool
    var wg sync.WaitGroup
    wg.Add(1)
    go func() {
        condition = true
        wg.Done()
    }()
    wg.Wait()
    if !condition {
        t.Error("Condition is false. But expected was true.")
    }
}



推荐答案

它不能。请参阅 WaitGroup.Wait()意味着在这种情况下的内存屏障? 。感谢@ OlegGolovanov看看去的ASM。

It can't. See Does WaitGroup.Wait() imply memory barrier in this case?. Thanks a lot to @OlegGolovanov for looking at go's ASM.

这篇关于你能否让这个“不正确同步”的测试失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆