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: Shouldn/t the world reset between tests? #68823

Closed
coreyogburn opened this issue Aug 9, 2024 · 2 comments
Closed

testing: Shouldn/t the world reset between tests? #68823

coreyogburn opened this issue Aug 9, 2024 · 2 comments

Comments

@coreyogburn
Copy link

Go version

go version go1.22.5 linux/amd64

Output of go env in your module/workspace:

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/corey/.cache/go-build'
GOENV='/home/corey/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/corey/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/corey/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.5'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD=''
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build3353005674=/tmp/go-build -gno-record-gcc-switches'

What did you do?

I have a function that pulls a git repo. I don't want to pull too frequently if this function is called multiple times so at the end of the function it stores time.Now() into a package level variable and won't attempt to re-pull again until a certain amount of time has passed. This could be another shining reason why not to use global variables, but the point I want to make here is that I have multiple go tests that end up calling this function. When I run go test ./... and run these tests together in one command, the repeated calls trip the "we just pulled, don't pull again" bit and skips a bunch of mocked function calls. The missed mocked calls will fail the test.

Somewhere I picked up the idea that each test ran more independently than that. One of my tests would call my git pull function and put the time in the cache and in the same test run a completely different test would then call the same function and see the state change from the previous test.

I have already rewritten my particular functions and tests to get around this but I'm surprised to find out that each test isn't fully isolated from changes other tests have made. Is running multiple tests at once supposed to persist package level variables from test to test?

What did you see happen?

A test early in the run will call my function and set a package-level variable that persisted in memory until an entirely different test acted on that change and failed.

What did you expect to see?

I expected each test to be run independently in a way that won't preserve memory from one test to another. If a test fails and leaves things in a bad state, it can cause other tests to fail. I thought this was minimized.

@gabyhelp
Copy link

gabyhelp commented Aug 9, 2024

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@seankhliao
Copy link
Member

This is working as intended, it's also not something that can be changed.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Aug 9, 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

3 participants