Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing/synctest: blocks forever when use time.Tick #70857

Closed
rschio opened this issue Dec 15, 2024 · 2 comments
Closed

testing/synctest: blocks forever when use time.Tick #70857

rschio opened this issue Dec 15, 2024 · 2 comments

Comments

@rschio
Copy link

rschio commented Dec 15, 2024

Go version

1.24rc1

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/rschio/Library/Caches/go-build'
GODEBUG=''
GOENV='/Users/rschio/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/rq/0fx96ptn7w5357fdm_qzflhh0000gn/T/go-build2099632737=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/private/tmp/learn_synctest/go.mod'
GOMODCACHE='/Users/rschio/go/pkg/mod'
GONOPROXY='github.com/rschio/*,buf.build/gen/go'
GONOSUMDB='github.com/rschio/*,buf.build/gen/go'
GOOS='darwin'
GOPATH='/Users/rschio/go'
GOPRIVATE='github.com/rschio/*,buf.build/gen/go'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/rschio/go/pkg/mod/golang.org/[email protected]'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/Users/rschio/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='go1.24rc1+auto'
GOTOOLDIR='/Users/rschio/go/pkg/mod/golang.org/[email protected]/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.24rc1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I was testing the new experimental package synctest with an adaptation of the code from https://danp.net/posts/synctest-experiment/ and observed that the test runs forever.

And I noticed that the commented code runs correctly (runs fast).

My code:
GOEXPERIMENT=synctest go test -v .

package main

import (
	"context"
	"sync/atomic"
	"testing"
	"testing/synctest"
	"time"
)

func Test(t *testing.T) {
	synctest.Run(func() {
		ctx := context.Background()
		ctx, cancel := context.WithCancel(ctx)

		var hits atomic.Int32
		go func() {
			t := time.Tick(time.Millisecond)
			//tick := time.NewTicker(time.Millisecond)
			//defer tick.Stop()
			for {
				select {
				case <-ctx.Done():
					return
				case <-t:
					//case <-tick.C:
					hits.Add(1)
				}
			}
		}()

		time.Sleep(4 * time.Millisecond)
		cancel()

		got := int(hits.Load())
		if want := 3; got != want {
			t.Fatalf("got %v, want %v", got, want)
		}
	})
}

What did you see happen?

The code provided runs forever or at least take too long.

What did you expect to see?

I expected to see the test complete in a reasonable time and I expected that time.NewTicker() to be equivalent to time.Tick() in the provided context.

@rschio
Copy link
Author

rschio commented Dec 15, 2024

Sorry I think this is a duplicate from #67434 (comment)

@rschio rschio closed this as completed Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants