Skip to content

testing/synctest: add convenience function for Sleep then Wait #77169

Description

@rittneje

Proposal Details

In our experimentation with testing/synctest (which is really helpful!), we have found that a surprisingly common mistake is to call time.Sleep in the test function but forget to call synctest.Wait afterwards, which causes the test to be racy. These tests are similar in nature to this example.

To that end, I would like to propose a helper function (I don't care much about the exact name) that internally makes these two calls.

package synctest

func Sleep(d time.Duration) {
    time.Sleep(d)
    Wait()
}

Obviously this function isn't strictly needed, and we could of course make that wrapper ourselves. The hope is that by having it within synctest and replacing two calls with one, the likelihood of the aforementioned mistake will reduce significantly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LibraryProposalIssues describing a requested change to the Go standard library or x/ libraries, but not to a toolProposalProposal-Accepted

    Type

    No type

    Projects

    Status
    Accepted

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions