Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[kms] fix flaky test #3268

Merged
merged 16 commits into from
Apr 7, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions kms/api-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import time
from os import environ

from google.api_core.exceptions import GoogleAPICallError
from google.api_core.exceptions import Aborted, GoogleAPICallError
from google.cloud import kms_v1
from google.cloud.kms_v1 import enums
from google.iam.v1.policy_pb2 import Policy
Expand Down Expand Up @@ -68,6 +68,7 @@ class TestKMSSnippets:
parent = 'projects/{}/locations/{}'.format(project_id, location)
keyring_path = '{}/keyRings/{}'.format(parent, keyring_id)
version = '1'
try_limit = 10

symId = 'symmetric'

Expand Down Expand Up @@ -177,10 +178,18 @@ def test_key_policy(self):
self.symId,
self.member,
self.role)
policy = snippets.get_crypto_key_policy(self.project_id,
self.location,
self.keyring_id,
self.symId)
try_number = 0
policy = None
while policy is None and try_number < self.try_limit:
try:
time.sleep(2*try_number)
policy = snippets.get_crypto_key_policy(self.project_id,
self.location,
self.keyring_id,
self.symId)
except Aborted:
# aborted by backend. Try again
try_number += 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you want to use @eventually_consistent.call?

An example commit:
c6254e4

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to using eventually consistent if you can

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know about @eventually_consistent, that's much cleaner!

found = False
for b in list(policy.bindings):
if b.role == self.role and self.member in b.members:
Expand All @@ -193,10 +202,18 @@ def test_key_policy(self):
self.symId,
self.member,
self.role)
policy = snippets.get_crypto_key_policy(self.project_id,
self.location,
self.keyring_id,
self.symId)
try_number = 0
policy = None
while policy is None and try_number < self.try_limit:
try:
time.sleep(2*try_number)
policy = snippets.get_crypto_key_policy(self.project_id,
self.location,
self.keyring_id,
self.symId)
except Aborted:
# aborted by backend. Try again
try_number += 1
found = False
for b in list(policy.bindings):
if b.role == self.role and self.member in b.members:
Expand Down