Skip to content

[firestore] fix: fix flaky tests #3437

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

Merged
merged 3 commits into from
Apr 21, 2020
Merged
Changes from all 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
24 changes: 20 additions & 4 deletions firestore/cloud-client/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# limitations under the License.

import datetime
import threading
from time import sleep

from google.cloud import firestore
Expand Down Expand Up @@ -660,10 +661,14 @@ def listen_document():
db = firestore.Client()
# [START listen_document]

# Create an Event for notifying main thread.
callback_done = threading.Event()

# Create a callback on_snapshot function to capture changes
def on_snapshot(doc_snapshot, changes, read_time):
for doc in doc_snapshot:
print(u'Received document snapshot: {}'.format(doc.id))
callback_done.set()

doc_ref = db.collection(u'cities').document(u'SF')

Expand All @@ -680,7 +685,8 @@ def on_snapshot(doc_snapshot, changes, read_time):
u'population': 860000
}
doc_ref.set(data)
sleep(3)
# Wait for the callback.
callback_done.wait(timeout=60)
# [START detach_listener]
# Terminate watch on a document
doc_watch.unsubscribe()
Expand All @@ -691,12 +697,16 @@ def listen_multiple():
db = firestore.Client()
# [START listen_multiple]

# Create an Event for notifying main thread.
callback_done = threading.Event()

# Create a callback on_snapshot function to capture changes
def on_snapshot(col_snapshot, changes, read_time):
print(u'Callback received query snapshot.')
print(u'Current cities in California:')
for doc in col_snapshot:
print(u'{}'.format(doc.id))
callback_done.set()

col_query = db.collection(u'cities').where(u'state', u'==', u'CA')

Expand All @@ -713,15 +723,18 @@ def on_snapshot(col_snapshot, changes, read_time):
u'population': 860000
}
db.collection(u'cities').document(u'SF').set(data)
sleep(1)

# Wait for the callback.
callback_done.wait(timeout=60)
query_watch.unsubscribe()


def listen_for_changes():
db = firestore.Client()
# [START listen_for_changes]

# Create an Event for notifying main thread.
delete_done = threading.Event()

# Create a callback on_snapshot function to capture changes
def on_snapshot(col_snapshot, changes, read_time):
print(u'Callback received query snapshot.')
Expand All @@ -733,6 +746,7 @@ def on_snapshot(col_snapshot, changes, read_time):
print(u'Modified city: {}'.format(change.document.id))
elif change.type.name == 'REMOVED':
print(u'Removed city: {}'.format(change.document.id))
delete_done.set()

col_query = db.collection(u'cities').where(u'state', u'==', u'CA')

Expand Down Expand Up @@ -763,7 +777,9 @@ def on_snapshot(col_snapshot, changes, read_time):

# Delete document
mtv_document.delete()
sleep(1)

# Wait for the callback captures the deletion.
delete_done.wait(timeout=60)
query_watch.unsubscribe()


Expand Down