Skip to content

testing/synctest: wrong duration in doc example #73839

@kevinburke

Description

@kevinburke

Go version

tip

Output of go env in your module/workspace:

Not relevant

What did you do?

The documentation for testing/synctest at tip.golang.org says:

For example, this test runs immediately rather than taking two seconds:

func TestTime(t *testing.T) {
	synctest.Test(t, func(t *testing.T) {
		start := time.Now() // always midnight UTC 2001-01-01
		go func() {
			time.Sleep(1 * time.Nanosecond)
			t.Log(time.Since(start)) // always logs "1ns"
		}()
		time.Sleep(2 * time.Nanosecond) // the goroutine above will run before this Sleep returns
		t.Log(time.Since(start))        // always logs "2ns"
	})
}

It is possible I am not very smart, and just totally misreading this. But I am confused how this could ever take two seconds to run, under normal conditions - the maximum sleep introduced is 2 nanoseconds, and the rest of the code should complete reasonably quickly (on the order of microseconds or milliseconds).

As a result, I'm not sure how synctest modifies the typical behavior of the program.

What did you see happen?

See above

What did you expect to see?

See above

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FixPendingIssues that have a fix which has not yet been reviewed or submitted.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions