From a78bcf5c388eedc940190e07c1250248a81595b2 Mon Sep 17 00:00:00 2001 From: billyrrr Date: Sat, 4 Apr 2020 13:33:39 -0700 Subject: [PATCH] Speed up build on travis by reducing time.sleep time --- Makefile | 1 + examples/binding_example.py | 5 ++-- examples/distributed_counter_example.py | 7 +++--- examples/fluttergram/tests/test_delta_dav.py | 4 +-- examples/meeting_room/tests/test_dav.py | 14 +++++------ .../meeting_room/tests/test_view_model.py | 8 +++--- flask_boiler/testing_utils.py | 25 +++++++++++++++++++ tests/test_view_model.py | 6 ++--- tests/test_ws.py | 4 ++- 9 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 flask_boiler/testing_utils.py diff --git a/Makefile b/Makefile index aa1ff7b..9188c55 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ ALLSPHINXOPTS = -b $(BUILDER) -d build/doctrees -D latex_paper_size=$(PAPER) \ # Propagates to child process export FB_CONFIG_FILENAME = boiler-testing.yaml +export WAIT_TIME = 2 # waits 2 seconds by default for firestore to refresh after change; testing only .PHONY: help checkout update build html htmlhelp latex text changes linkcheck \ suspicious coverage doctest pydoc-topics htmlview clean dist check serve \ diff --git a/examples/binding_example.py b/examples/binding_example.py index 41f4a4f..30923db 100644 --- a/examples/binding_example.py +++ b/examples/binding_example.py @@ -15,6 +15,7 @@ from flask_boiler.view_model import ViewModel from flask_boiler.domain_model import DomainModel from google.cloud import firestore +from flask_boiler import testing_utils from functools import partial from examples.luggage_models import LuggageItem, Luggages @@ -61,7 +62,7 @@ vm.register_listener() # Takes time to propagate changes - time.sleep(2) + testing_utils._wait(factor=.5) assert vm.to_dict() == { "luggages": [ @@ -87,7 +88,7 @@ # and local copies of a ViewModel should not be used to read # updated values - time.sleep(2) + testing_utils._wait(factor=.3) # # Test that the view model now has updated values assert vm.to_dict() == { diff --git a/examples/distributed_counter_example.py b/examples/distributed_counter_example.py index 96780d5..575623f 100644 --- a/examples/distributed_counter_example.py +++ b/examples/distributed_counter_example.py @@ -8,6 +8,7 @@ from flask_boiler.struct import Struct from flask_boiler.view_mediator_dav import ViewMediatorDAV from flask_boiler.view_model import ViewModel +from flask_boiler import testing_utils config = Config( app_name="flask-boiler-testing", @@ -104,7 +105,7 @@ def test_counter(): mediator = CounterMediator(shard_size=10) mediator.start() - time.sleep(5) + testing_utils._wait() ref = CTX.db.collection("counters").document("counter_0") @@ -126,7 +127,7 @@ def test_increment(CTX): mediator.start() - time.sleep(3) + testing_utils._wait(factor=.7) doc_ref = CTX.db.collection("counters").document("counter_0") @@ -145,7 +146,7 @@ def test_increment(CTX): merge=True ) - time.sleep(1) + testing_utils._wait(factor=.3) assert doc_ref.get().to_dict() == { "doc_ref": "counters/counter_0", diff --git a/examples/fluttergram/tests/test_delta_dav.py b/examples/fluttergram/tests/test_delta_dav.py index 6d39bec..6d9de20 100644 --- a/examples/fluttergram/tests/test_delta_dav.py +++ b/examples/fluttergram/tests/test_delta_dav.py @@ -12,7 +12,7 @@ from flask_boiler.view_mediator_dav import ViewMediatorDAV, \ ViewMediatorDeltaDAV from flask_boiler.view_model import ViewModel -from flask_boiler import view_mediator, utils, schema, fields +from flask_boiler import view_mediator, utils, schema, fields, testing_utils # Import the fixtures used by fixtures from tests.fixtures import CTX, setup_app from .fixtures import users, posts @@ -93,7 +93,7 @@ def test_start(users, posts): mediator.start() - time.sleep(10) + testing_utils._wait(factor=.2) ref = User._get_collection().document("thomasina").collection("feed") \ .document(posts[0].doc_id) diff --git a/examples/meeting_room/tests/test_dav.py b/examples/meeting_room/tests/test_dav.py index a9455f1..6490510 100644 --- a/examples/meeting_room/tests/test_dav.py +++ b/examples/meeting_room/tests/test_dav.py @@ -16,7 +16,7 @@ ViewMediatorDeltaDAV from flask_boiler.view_model import ViewModel from ..views import meeting_session_ops -from flask_boiler import view_mediator, utils +from flask_boiler import view_mediator, utils, testing_utils # Import the fixtures used by fixtures from tests.fixtures import CTX, setup_app from .fixtures import users, tickets, location, meeting @@ -101,7 +101,7 @@ def test_start(users, tickets, location, meeting): mediator.start() - time.sleep(5) + testing_utils._wait(factor=.7) ref = Context.db.collection("users").document(users[0].doc_id) \ .collection(MeetingSessionDAV.__name__).document(meeting.doc_id) @@ -145,7 +145,7 @@ def test_mutate(users, tickets, location, meeting): mediator.start() - time.sleep(5) + testing_utils._wait(factor=.7) ref = Context.db.collection("users").document(users[0].doc_id) \ .collection(MeetingSessionDAV.__name__).document(meeting.doc_id) @@ -185,7 +185,7 @@ def test_mutate(users, tickets, location, meeting): ) ) - time.sleep(5) + testing_utils._wait(factor=.7) m = Meeting.get(doc_id="meeting_1") assert m.status == "closed" @@ -231,7 +231,7 @@ def test_domain_model_changes(users, tickets, location, meeting): mediator.start() - time.sleep(5) + testing_utils._wait(factor=.7) # ref = Context.db.collection("users").document(users[0].doc_id) \ .collection(MeetingSessionDAV.__name__).document(meeting.doc_id) @@ -257,7 +257,7 @@ def test_domain_model_changes(users, tickets, location, meeting): tickets[0].attendance = False tickets[0].save() - time.sleep(5) + testing_utils._wait(factor=.7) # """ Expect the document to be updated to exclude Tijuana Furlong from a @@ -443,7 +443,7 @@ def test_mutation(users, tickets, location, meeting): "lastName": "Manes-Kennedy" }) - time.sleep(3) + testing_utils._wait(factor=.7) updated_user = User.get(doc_id=user_id) diff --git a/examples/meeting_room/tests/test_view_model.py b/examples/meeting_room/tests/test_view_model.py index 19d5d14..02c61a6 100644 --- a/examples/meeting_room/tests/test_view_model.py +++ b/examples/meeting_room/tests/test_view_model.py @@ -4,7 +4,7 @@ from examples.meeting_room.view_models.meeting_session import \ MeetingSessionMutation from ..views import meeting_session_ops -from flask_boiler import view_mediator +from flask_boiler import view_mediator, testing_utils from .. import view_models # Import the fixtures used by fixtures from tests.fixtures import CTX, setup_app @@ -50,7 +50,7 @@ def test_view(users, tickets, location, meeting, setup_app): list_get_view=meeting_session_ops.ListGet ) - time.sleep(2) # TODO: delete after implementing sync + testing_utils._wait(factor=.7) test_client = app.test_client() @@ -110,7 +110,7 @@ def test_view(users, tickets, location, meeting, setup_app): "inSession": False }) - time.sleep(3) + testing_utils._wait(factor=.7) res = test_client.get( path='meeting_sessions/meeting_1') @@ -176,7 +176,7 @@ def test_view_model_update(users, tickets, location, meeting): tickets[0].attendance = False tickets[0].save() - time.sleep(5) + testing_utils._wait() assert meeting_session._export_as_view_dict() == {'inSession': True, 'longitude': -117.242929, diff --git a/flask_boiler/testing_utils.py b/flask_boiler/testing_utils.py new file mode 100644 index 0000000..43376d7 --- /dev/null +++ b/flask_boiler/testing_utils.py @@ -0,0 +1,25 @@ +import time +DEFAULT_WAIT_TIME = 5 + +import logging +root_log = logging.getLogger() +stats = {"total": 0} + +try: + import os + wt = os.getenv("WAIT_TIME") + if wt is not None: + DEFAULT_WAIT_TIME = wt +except Exception as e: + """ + TODO: find the best practice to handle a "black swan" error + """ + raise e + + +def _wait(secs=DEFAULT_WAIT_TIME, factor=1): + secs = int(secs * factor) + time.sleep(secs) + stats["total"] += secs + logging.debug(msg=f"waited for {secs} seconds, " + f"a total of {stats['total']} seconds in this session") diff --git a/tests/test_view_model.py b/tests/test_view_model.py index 15975a1..02db9bf 100644 --- a/tests/test_view_model.py +++ b/tests/test_view_model.py @@ -3,7 +3,7 @@ from google.cloud.firestore import DocumentReference from examples.luggage_models import LuggageItem, Luggages -from flask_boiler import fields +from flask_boiler import fields, testing_utils from flask_boiler.schema import Schema from flask_boiler.domain_model import DomainModel from flask_boiler.view_model import ViewModel @@ -94,7 +94,7 @@ def test_binding(CTX): vm.register_listener() # Takes time to propagate changes - time.sleep(2) + testing_utils._wait(factor=.7) assert vm.to_dict() == { "luggages": [ @@ -120,7 +120,7 @@ def test_binding(CTX): # and local copies of a ViewModel should not be used to read # updated values - time.sleep(2) + testing_utils._wait(factor=.7) # # Test that the view model now has updated values assert vm.to_dict() == { diff --git a/tests/test_ws.py b/tests/test_ws.py index cab263b..213ec2b 100644 --- a/tests/test_ws.py +++ b/tests/test_ws.py @@ -5,6 +5,8 @@ import pytest from unittest.mock import MagicMock, patch + +from flask_boiler import testing_utils from flask_boiler.view_mediator_websocket import ViewMediatorWebsocket from .color_fixtures import Color, PaletteViewModel, rainbow_vm, color_refs from .fixtures import setup_app, CTX @@ -46,7 +48,7 @@ class Demo(ViewMediatorWebsocket): color_refs[0].update({"name": "cyan"}) - time.sleep(1) + testing_utils._wait(factor=.7) assert client.get_received(namespace="/palette") == [{'name': 'updated', 'args': [{'colors': ['cyan', 'magenta', 'yellow'], 'rainbowName': 'cyan-magenta-yellow'}], 'namespace': '/palette'}]