@@ -246,58 +246,74 @@ type UserInfo struct {
246246func fetchUserInfo (t * testing.T , username string ) UserInfo {
247247 t .Helper ()
248248
249- const numAttempts = 2
249+ const (
250+ numAttempts = 10
251+ attemptDelay = 3 * time .Second
252+ )
253+
254+ httpClient := newHTTPClientForTestSite ()
255+ if httpClient == nil {
256+ httpClient = http .DefaultClient
257+ }
250258
251259 for attempt := range numAttempts {
252- url := fmt .Sprintf ("%s/api/v1/dev_fetch_api_key?username=%s" , TestSite , username )
260+ info , done := getUserInfo (t , httpClient , username , attempt )
261+ if done {
262+ return info
263+ }
264+ time .Sleep (attemptDelay )
265+ }
253266
254- req , reqErr := http .NewRequestWithContext (context .Background (), http .MethodPost , url , nil )
255- require .NoError (t , reqErr )
267+ t .Fatalf ("Failed to fetch API key for user %s" , username )
268+ return UserInfo {}
269+ }
256270
257- httpClient := newHTTPClientForTestSite ()
258- if httpClient == nil {
259- httpClient = http .DefaultClient
260- }
271+ func getUserInfo (t * testing.T , client * http.Client , username string , attempt int ) (UserInfo , bool ) {
272+ url := fmt .Sprintf ("%s/api/v1/dev_fetch_api_key?username=%s" , TestSite , username )
261273
262- resp , err := httpClient .Do (req )
263- if err != nil {
264- t .Fatalf ("Failed to fetch API key for user %s: %v" , username , err )
265- }
274+ req , reqErr := http .NewRequestWithContext (context .Background (), http .MethodPost , url , nil )
275+ require .NoError (t , reqErr )
266276
267- body , readErr := io . ReadAll ( resp . Body )
268- require . NoError ( t , resp . Body . Close ())
269- if readErr != nil {
270- t . Fatalf ( "Failed to read API key Response for user %s: %v" , username , readErr )
271- }
277+ resp , err := client . Do ( req )
278+ if err != nil {
279+ t . Logf ( "Failed to fetch API key for user %s (attempt %d): %v" , username , attempt + 1 , err )
280+ return UserInfo {}, false
281+ }
272282
273- if resp .StatusCode == http .StatusOK {
274- var result UserInfo
283+ body , readErr := io .ReadAll (resp .Body )
284+ require .NoError (t , resp .Body .Close ())
285+ if readErr != nil {
286+ t .Logf ("Failed to read API key response for user %s (attempt %d): %v" , username , attempt + 1 , readErr )
287+ return UserInfo {}, false
288+ }
275289
276- if unMarshalErr := json .Unmarshal (body , & result ); unMarshalErr != nil {
277- t .Fatalf ("Failed to decode API key Response for user %s: %v" , username , unMarshalErr )
278- }
279- if result .APIKey == "" {
280- t .Fatalf ("Empty API key received for user %s" , username )
281- }
282- if result .EMail != username {
283- t .Fatalf ("Unexpected email in API key Response: got %s, want %s" , result .EMail , username )
284- }
285- return result
286- }
290+ if resp .StatusCode == http .StatusOK {
291+ var result UserInfo
287292
288- if resp .StatusCode == http .StatusUnauthorized && attempt == 0 && tryReactivateUser (t , username , body ) {
289- continue
293+ if unMarshalErr := json .Unmarshal (body , & result ); unMarshalErr != nil {
294+ t .Logf (
295+ "Failed to unmarshal API key response for user %s (attempt %d): %v" ,
296+ username ,
297+ attempt + 1 ,
298+ unMarshalErr ,
299+ )
300+ return UserInfo {}, false
290301 }
291-
292- t .Fatalf (
293- "Failed to fetch API key for user %s: status code %d, Response: %s" ,
294- username ,
295- resp . StatusCode ,
296- string ( body ),
297- )
302+ if result . APIKey == "" {
303+ t .Fatalf ("Empty API key received for user %s" , username )
304+ }
305+ if result . EMail != username {
306+ t . Fatalf ( "Unexpected email in API key Response: got %s, want %s" , result . EMail , username )
307+ }
308+ return result , true
298309 }
299- t .Fatalf ("Failed to fetch API key for user %s after reactivation attempt" , username )
300- return UserInfo {}
310+ if resp .StatusCode == http .StatusUnauthorized && attempt == 0 {
311+ if ! tryReactivateUser (t , username , body ) {
312+ t .Logf ("Failed to reactivate user %s (attempt %d)" , username , attempt + 1 )
313+ }
314+ }
315+
316+ return UserInfo {}, false
301317}
302318
303319func CreateRandomChannel (t * testing.T , apiClient client.Client , subscribers ... int64 ) (string , int64 ) {
0 commit comments