Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6fb4cc6

Browse files
authored
fix: add retry logic to OAuth2 metadata tests to avoid race conditions (#19813)
This PR adds a readiness wait to OAuth2 metadata endpoint tests to avoid rare races with server startup. Instead of immediately making HTTP requests, the tests now use `testutil.Eventually` to retry the requests until they succeed, with a short interval between attempts. This helps prevent flaky tests that might fail due to timing issues during server initialization.Fixes:coder/internal#996
1 parent98213d7 commit6fb4cc6

File tree

2 files changed

+45
-26
lines changed

2 files changed

+45
-26
lines changed

‎coderd/oauth2provider/metadata_test.go‎

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package oauth2provider_test
22

33
import (
44
"context"
5-
"encoding/json"
6-
"net/http"
75
"net/url"
86
"testing"
97

@@ -23,20 +21,11 @@ func TestOAuth2AuthorizationServerMetadata(t *testing.T) {
2321
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
2422
defercancel()
2523

26-
// Use a plain HTTP client since this endpoint doesn't require authentication
24+
// Use a plain HTTP client since this endpoint doesn't require authentication.
25+
// Add a short readiness wait to avoid rare races with server startup.
2726
endpoint:=serverURL.ResolveReference(&url.URL{Path:"/.well-known/oauth-authorization-server"}).String()
28-
req,err:=http.NewRequestWithContext(ctx,http.MethodGet,endpoint,nil)
29-
require.NoError(t,err)
30-
31-
resp,err:=http.DefaultClient.Do(req)
32-
require.NoError(t,err)
33-
deferresp.Body.Close()
34-
35-
require.Equal(t,http.StatusOK,resp.StatusCode)
36-
3727
varmetadata codersdk.OAuth2AuthorizationServerMetadata
38-
err=json.NewDecoder(resp.Body).Decode(&metadata)
39-
require.NoError(t,err)
28+
testutil.RequireEventuallyResponseOK(ctx,t,endpoint,&metadata)
4029

4130
// Verify the metadata
4231
require.NotEmpty(t,metadata.Issuer)
@@ -57,20 +46,11 @@ func TestOAuth2ProtectedResourceMetadata(t *testing.T) {
5746
ctx,cancel:=context.WithTimeout(context.Background(),testutil.WaitLong)
5847
defercancel()
5948

60-
// Use a plain HTTP client since this endpoint doesn't require authentication
49+
// Use a plain HTTP client since this endpoint doesn't require authentication.
50+
// Add a short readiness wait to avoid rare races with server startup.
6151
endpoint:=serverURL.ResolveReference(&url.URL{Path:"/.well-known/oauth-protected-resource"}).String()
62-
req,err:=http.NewRequestWithContext(ctx,http.MethodGet,endpoint,nil)
63-
require.NoError(t,err)
64-
65-
resp,err:=http.DefaultClient.Do(req)
66-
require.NoError(t,err)
67-
deferresp.Body.Close()
68-
69-
require.Equal(t,http.StatusOK,resp.StatusCode)
70-
7152
varmetadata codersdk.OAuth2ProtectedResourceMetadata
72-
err=json.NewDecoder(resp.Body).Decode(&metadata)
73-
require.NoError(t,err)
53+
testutil.RequireEventuallyResponseOK(ctx,t,endpoint,&metadata)
7454

7555
// Verify the metadata
7656
require.NotEmpty(t,metadata.Resource)

‎testutil/http.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package testutil
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"net/http"
7+
"testing"
8+
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
// RequireEventuallyResponseOK makes HTTP GET requests to the given endpoint until it returns
13+
// 200 OK with a valid JSON response that can be decoded into target, or until the context
14+
// times out. This is useful for waiting for HTTP servers to become ready during tests,
15+
// especially for metadata endpoints that may not be immediately available.
16+
funcRequireEventuallyResponseOK(ctx context.Context,t testing.TB,endpointstring,targetinterface{}) {
17+
t.Helper()
18+
19+
ok:=Eventually(ctx,t,func(ctx context.Context) (donebool) {
20+
req,err:=http.NewRequestWithContext(ctx,http.MethodGet,endpoint,nil)
21+
iferr!=nil {
22+
returnfalse
23+
}
24+
resp,err:=http.DefaultClient.Do(req)
25+
iferr!=nil {
26+
returnfalse
27+
}
28+
deferresp.Body.Close()
29+
ifresp.StatusCode!=http.StatusOK {
30+
returnfalse
31+
}
32+
iferr:=json.NewDecoder(resp.Body).Decode(target);err!=nil {
33+
returnfalse
34+
}
35+
returntrue
36+
},IntervalFast)
37+
38+
require.True(t,ok,"endpoint %s not ready in time",endpoint)
39+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp