Skip to content

Data race when calling assert.Eventually() #865

Closed
@snebel29

Description

@snebel29

Hi,
Today I realize that one of my tests is flaky, working most of times but failing others with a data race being reported.

I'm using github.com/stretchr/testify v1.4.0

I have the impression that the following return causes the function to exit and close checkPassed channel.

testify/assert/assertions.go

Lines 1486 to 1487 in 221dbe5

case <-timer.C:
return Fail(t, "Condition never satisfied", msgAndArgs...)

Which is still being targeted by the following goroutine.

testify/assert/assertions.go

Lines 1493 to 1495 in 221dbe5

go func() {
checkPassed <- condition()
}()

This would only happen when the goroutine with condition() is running but not finish yet, and the timer hits causing the return close.

Assuming I'm right I'm not sure yet what's the best way to fix it, may be decoupling those two into different select and using more channels, or may be there is some trick to not try to send if the channel was closed/deallocated, I would happily create a PR once/if in agreement...

Here is the data race trace.

WARNING: DATA RACE
Write at 0x00c00011a190 by goroutine 9:
  runtime.closechan()
      /usr/local/go/src/runtime/chan.go:334 +0x0
  github.com/stretchr/testify/assert.Eventually()
      /home/user/workspace/project/vendor/github.com/stretchr/testify/assert/assertions.go:1487 +0x3d5
  github.com/stretchr/testify/assert.Eventuallyf()
      /home/user/workspace/project/vendor/github.com/stretchr/testify/assert/assertion_format.go:124 +0x197
  example.com/group/project/pkg/configwatcher.TestFileWatcherReturnErrorOnTimeoutWhenFileNotExist_Start()
      /home/user/workspace/project/pkg/configwatcher/file_watcher_test.go:55 +0x3d7
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:909 +0x199

Previous read at 0x00c00011a190 by goroutine 25:
  runtime.chansend()
      /usr/local/go/src/runtime/chan.go:142 +0x0
  github.com/stretchr/testify/assert.Eventually.func1()
      /home/user/workspace/project/vendor/github.com/stretchr/testify/assert/assertions.go:1494 +0x54
Data 
Goroutine 9 (running) created at:
  testing.(*T).Run()
      /usr/local/go/src/testing/testing.go:960 +0x651
  testing.runTests.func1()
      /usr/local/go/src/testing/testing.go:1202 +0xa6
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:909 +0x199
  testing.runTests()
      /usr/local/go/src/testing/testing.go:1200 +0x521
  testing.(*M).Run()
      /usr/local/go/src/testing/testing.go:1117 +0x2ff
  main.main()
      _testmain.go:52 +0x223

Goroutine 25 (running) created at:
  github.com/stretchr/testify/assert.Eventually()
      /home/user/workspace/project/vendor/github.com/stretchr/testify/assert/assertions.go:1493 +0x369
  github.com/stretchr/testify/assert.Eventuallyf()
      /home/user/workspace/project/vendor/github.com/stretchr/testify/assert/assertion_format.go:124 +0x197
  example.com/group/project/pkg/configwatcher.TestFileWatcherReturnErrorOnTimeoutWhenFileNotExist_Start()
      /home/user/workspace/project/pkg/configwatcher/file_watcher_test.go:55 +0x3d7
  testing.tRunner()
      /usr/local/go/src/testing/testing.go:909 +0x199
==================
--- FAIL: TestFileWatcherReturnErrorOnTimeoutWhenFileNotExist_Start (0.14s)
    file_watcher_test.go:55: 
                Error Trace:    file_watcher_test.go:55
                Error:          Condition never satisfied
                Test:           TestFileWatcherReturnErrorOnTimeoutWhenFileNotExist_Start
                Messages:       Start() never returned an error before timeout 90ms
    testing.go:853: race detected during execution of test
panic: send on closed channel

goroutine 26 [running]:
github.com/stretchr/testify/assert.Eventually.func1(0xc00011a180, 0xc000118040)
        /home/user/workspace/project/vendor/github.com/stretchr/testify/assert/assertions.go:1494 +0x55
created by github.com/stretchr/testify/assert.Eventually
        /home/user/workspace/project/vendor/github.com/stretchr/testify/assert/assertions.go:1493 +0x36a
FAIL    example.com/group/project/pkg/configwatcher        0.650s
FAIL

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    assert.EventuallyAbout assert.Eventually/EventuallyWithTbugpkg-assertChange related to package testify/assert

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions