-
Notifications
You must be signed in to change notification settings - Fork 180
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
why patches.Reset not effective? #145
Comments
Note that gomonkey is not concurrent safe. |
+1. and idea now? |
try this var wg sync.WaitGroup
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wg.Add(1)
go func() {
patches := gomonkey.NewPatches()
defer patches.Reset()
fmt.Println(tt.name, "tt.mockHttpGetFunc is nil:", tt.mockHttpGetFunc == nil)
fmt.Println(tt.name, "tt.mockReadAllFunc is nil:", tt.mockReadAllFunc == nil)
patches.ApplyFunc(io.ReadAll, tt.mockReadAllFunc)
patches.ApplyFunc(http.Get, tt.mockHttpGetFunc)
ip := GetIp(tt.url)
if !reflect.DeepEqual(ip, tt.expectIp) {
t.Errorf("GetIp() = %v, expect %v", ip, tt.expectIp)
}
wg.Done()
}()
wg.Wait() //block to run one by one
})
} |
Am seeing possible similar behavior, even we running out of parallel, go run gotest.tools/gotestsum@latest --junitfile report.xml --format testname -- -count=1 -p 1 ./... the tests still randomly fails every time (even though all work alone). When I add time.sleep(time.Sleep(100 * time.Millisecond)) to the end of each test, then all tests work. |
I found that adding runtime.GC() at the end of the t.Run function can also fix it. Like this:
|
Wow, your solution is the same as mine |
Behaviors:
file1: http_request.go
file2:http_request_test.go
test command
result unexpect
result expect
The text was updated successfully, but these errors were encountered: