Skip to content

Commit

Permalink
fix TestClientOnResponseError parallel tests (#710)
Browse files Browse the repository at this point in the history
In case of using `t.Parallel()` inside a for loop, the loop variables must be copied, otherwise only the last test will be executed.

```go
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
```
changed to
```go
	for _, test := range tests {
		test := test
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
```

also the AuthServer must be created within the test, otherwise some tests fail.

`t.Log(test.Name)` can be used to verify the issue:
```go
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			t.Parallel()
			t.Log("Test name:", test.name)
```
  • Loading branch information
SVilgelm committed Sep 25, 2023
1 parent 106e689 commit b852413
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions client_test.go
Expand Up @@ -780,9 +780,6 @@ func TestNewWithLocalAddr(t *testing.T) {
}

func TestClientOnResponseError(t *testing.T) {
ts := createAuthServer(t)
defer ts.Close()

tests := []struct {
name string
setup func(*Client)
Expand Down Expand Up @@ -862,8 +859,12 @@ func TestClientOnResponseError(t *testing.T) {
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ts := createAuthServer(t)
defer ts.Close()

var assertErrorHook = func(r *Request, err error) {
assertNotNil(t, r)
v, ok := err.(*ResponseError)
Expand Down

0 comments on commit b852413

Please sign in to comment.