Skip to content

Commit f36ab95

Browse files
author
Takashi Matsuo
authored
[firestore] fix: fix flaky tests (#3437)
fixes #2999 * Use threading.Event for notifying from the callback.
1 parent 38af56e commit f36ab95

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

firestore/cloud-client/snippets.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# limitations under the License.
1313

1414
import datetime
15+
import threading
1516
from time import sleep
1617

1718
from google.cloud import firestore
@@ -660,10 +661,14 @@ def listen_document():
660661
db = firestore.Client()
661662
# [START listen_document]
662663

664+
# Create an Event for notifying main thread.
665+
callback_done = threading.Event()
666+
663667
# Create a callback on_snapshot function to capture changes
664668
def on_snapshot(doc_snapshot, changes, read_time):
665669
for doc in doc_snapshot:
666670
print(u'Received document snapshot: {}'.format(doc.id))
671+
callback_done.set()
667672

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

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

700+
# Create an Event for notifying main thread.
701+
callback_done = threading.Event()
702+
694703
# Create a callback on_snapshot function to capture changes
695704
def on_snapshot(col_snapshot, changes, read_time):
696705
print(u'Callback received query snapshot.')
697706
print(u'Current cities in California:')
698707
for doc in col_snapshot:
699708
print(u'{}'.format(doc.id))
709+
callback_done.set()
700710

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

@@ -713,15 +723,18 @@ def on_snapshot(col_snapshot, changes, read_time):
713723
u'population': 860000
714724
}
715725
db.collection(u'cities').document(u'SF').set(data)
716-
sleep(1)
717-
726+
# Wait for the callback.
727+
callback_done.wait(timeout=60)
718728
query_watch.unsubscribe()
719729

720730

721731
def listen_for_changes():
722732
db = firestore.Client()
723733
# [START listen_for_changes]
724734

735+
# Create an Event for notifying main thread.
736+
delete_done = threading.Event()
737+
725738
# Create a callback on_snapshot function to capture changes
726739
def on_snapshot(col_snapshot, changes, read_time):
727740
print(u'Callback received query snapshot.')
@@ -733,6 +746,7 @@ def on_snapshot(col_snapshot, changes, read_time):
733746
print(u'Modified city: {}'.format(change.document.id))
734747
elif change.type.name == 'REMOVED':
735748
print(u'Removed city: {}'.format(change.document.id))
749+
delete_done.set()
736750

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

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

764778
# Delete document
765779
mtv_document.delete()
766-
sleep(1)
780+
781+
# Wait for the callback captures the deletion.
782+
delete_done.wait(timeout=60)
767783
query_watch.unsubscribe()
768784

769785

0 commit comments

Comments
 (0)