fixup!: use bounded context instead of time.After

This commit is contained in:
Elim Tsiagbey committed 2025-01-07 01:31:22 -05:00
1 parent 8c3c432408
commit b4e486854d
2 files changed
+7 -5

No files matched your search

+5 -3
View File
@@ -161,14 +161,16 @@ func (c *Config) ValidateServiceAccountKey(
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
timeout := time.After(c.validateServiceAccountKeyTimeout)
ctx, cancel := context.WithTimeout(ctx, c.validateServiceAccountKeyTimeout)
defer cancel()
for {
select {
case <-ctx.Done():
if ctx.Err() == context.DeadlineExceeded {
return fmt.Errorf("failed to validate service account key before timeout: %w", errors.Join(validatePermissionsErr, validationCallbackErr))
}
return ctx.Err()
case <-timeout:
return errors.Join(validatePermissionsErr, validationCallbackErr)
case <-ticker.C:
_, validatePermissionsErr = c.ValidateIamPermissions(ctx, permissions, opts...)
+2 -2
View File
@@ -269,7 +269,7 @@ func TestValidateIamPermissions(t *testing.T) {
func TestValidateServiceAccountKey(t *testing.T) {
ctx := context.Background()
testResourceServer := &testResourceServer{}
testValidateTimeout := 200 * time.Millisecond
testValidateTimeout := 1 * time.Second
tests := []struct {
name string
@@ -329,7 +329,7 @@ func TestValidateServiceAccountKey(t *testing.T) {
IAMServiceAccountKeysDeletePermission,
},
testIamPermissionsError: errors.New("IAM permissions error"),
expectedError: errors.New("IAM permissions error"),
expectedError: errors.New("failed to validate IAM permissions"),
},
}