Skip to content

Commit 75fbc02

Browse files
committed
attempt to make the token referesh retry backoff test more readable
1 parent e83fbee commit 75fbc02

1 file changed

Lines changed: 28 additions & 18 deletions

File tree

credentials/jwt/jwt_token_file_call_creds_test.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ func (s) TestTokenFileCallCreds_BackoffBehavior(t *testing.T) {
374374
})
375375

376376
// First call should fail with UNAVAILABLE.
377+
beforeCallRetryTime := time.Now()
377378
_, err = creds.GetRequestMetadata(ctx)
378379
if err == nil {
379380
t.Fatal("Expected error from nonexistent file")
@@ -392,11 +393,16 @@ func (s) TestTokenFileCallCreds_BackoffBehavior(t *testing.T) {
392393
if retryAttempt != 1 {
393394
t.Errorf("Expected retry attempt to be 1, got %d", retryAttempt)
394395
}
395-
if nextRetryTime.IsZero() || nextRetryTime.Before(time.Now()) {
396-
t.Error("Next retry time should be set to future time")
396+
if !nextRetryTime.After(beforeCallRetryTime) {
397+
t.Error("Next retry time should be set to a time after the first call")
397398
}
398399

399-
// Second call should still return cached error.
400+
// Second call should still return cached error and not retry.
401+
// Set nextRetryTime far enough in the future to ensure that's the case.
402+
impl.mu.Lock()
403+
impl.nextRetryTime = time.Now().Add(1 * time.Minute)
404+
wantNextRetryTime := impl.nextRetryTime
405+
impl.mu.Unlock()
400406
_, err = creds.GetRequestMetadata(ctx)
401407
if err == nil {
402408
t.Fatalf("creds.GetRequestMetadata() = %v, want non-nil", err)
@@ -409,20 +415,19 @@ func (s) TestTokenFileCallCreds_BackoffBehavior(t *testing.T) {
409415
retryAttempt2 := impl.retryAttempt
410416
nextRetryTime2 := impl.nextRetryTime
411417
impl.mu.Unlock()
412-
413-
if !nextRetryTime2.Equal(nextRetryTime) {
414-
t.Errorf("nextRetryTime should not change due to backoff. Got: %v, Want: %v", nextRetryTime2, nextRetryTime)
418+
if !nextRetryTime2.Equal(wantNextRetryTime) {
419+
t.Errorf("nextRetryTime should not change due to backoff. Got: %v, Want: %v", nextRetryTime2, wantNextRetryTime)
415420
}
416421
if retryAttempt2 != 1 {
417422
t.Error("Retry attempt should not change due to backoff")
418423
}
419424

420-
// Fast-forward the backoff retry time to allow next retry attempt.
425+
// Third call should retry but still fail with UNAVAILABLE.
426+
// Set the backoff retry time in the past to allow next retry attempt.
421427
impl.mu.Lock()
422428
impl.nextRetryTime = time.Now().Add(-1 * time.Minute)
429+
beforeCallRetryTime = impl.nextRetryTime
423430
impl.mu.Unlock()
424-
425-
// Third call should retry but still fail with UNAVAILABLE.
426431
_, err = creds.GetRequestMetadata(ctx)
427432
if err == nil {
428433
t.Fatalf("creds.GetRequestMetadata() = %v, want non-nil", err)
@@ -436,11 +441,11 @@ func (s) TestTokenFileCallCreds_BackoffBehavior(t *testing.T) {
436441
nextRetryTime3 := impl.nextRetryTime
437442
impl.mu.Unlock()
438443

439-
if !nextRetryTime3.After(nextRetryTime2) {
440-
t.Error("nextRetryTime should not change due to backoff")
444+
if !nextRetryTime3.After(beforeCallRetryTime) {
445+
t.Error("nextRetryTime3 should have been updated after third call")
441446
}
442447
if retryAttempt3 != 2 {
443-
t.Error("Retry attempt should not change due to backoff")
448+
t.Error("Expected retry attempt to increase after retry")
444449
}
445450

446451
// Create valid token file.
@@ -449,8 +454,13 @@ func (s) TestTokenFileCallCreds_BackoffBehavior(t *testing.T) {
449454
t.Fatalf("Failed to create valid token file: %v", err)
450455
}
451456

452-
// Fourth call should still fail even though the file now exists.
457+
// Fourth call should still fail even though the file now exists due to backoff.
458+
// Set nextRetryTime far enough in the future to ensure that's the case.
453459
_, err = creds.GetRequestMetadata(ctx)
460+
impl.mu.Lock()
461+
impl.nextRetryTime = time.Now().Add(1 * time.Minute)
462+
wantNextRetryTime = impl.nextRetryTime
463+
impl.mu.Unlock()
454464
if err == nil {
455465
t.Fatalf("creds.GetRequestMetadata() = %v, want non-nil", err)
456466
}
@@ -463,19 +473,19 @@ func (s) TestTokenFileCallCreds_BackoffBehavior(t *testing.T) {
463473
nextRetryTime4 := impl.nextRetryTime
464474
impl.mu.Unlock()
465475

466-
if !nextRetryTime4.Equal(nextRetryTime3) {
467-
t.Errorf("nextRetryTime should not change due to backoff. Got: %v, Want: %v", nextRetryTime4, nextRetryTime3)
476+
if !nextRetryTime4.Equal(wantNextRetryTime) {
477+
t.Errorf("nextRetryTime should not change due to backoff. Got: %v, Want: %v", nextRetryTime4, wantNextRetryTime)
468478
}
469479
if retryAttempt4 != retryAttempt3 {
470480
t.Error("Retry attempt should not change due to backoff")
471481
}
472482

473-
// Fast-forward the backoff retry time to allow next retry attempt.
483+
// Fifth call should succeed since the file now exists
484+
// and the backoff has expired.
485+
// Set the backoff retry time in the past to allow next retry attempt.
474486
impl.mu.Lock()
475487
impl.nextRetryTime = time.Now().Add(-1 * time.Minute)
476488
impl.mu.Unlock()
477-
// Fifth call should succeed since the file now exists
478-
// and the backoff has expired.
479489
_, err = creds.GetRequestMetadata(ctx)
480490
if err != nil {
481491
t.Fatalf("After creating valid token file, backoff should expire and trigger a token reload on the next RPC. GetRequestMetadata() should eventually succeed, but got: %v", err)

0 commit comments

Comments
 (0)