From 5a391414caedebc0adb2d96b103fca14e0f8d9ec Mon Sep 17 00:00:00 2001 From: Owais Date: Tue, 17 Sep 2019 17:10:32 +0500 Subject: [PATCH 1/7] tests: fix intermittent failure --- tests/test_config_manager.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_config_manager.py b/tests/test_config_manager.py index a607009d..822143fd 100644 --- a/tests/test_config_manager.py +++ b/tests/test_config_manager.py @@ -14,6 +14,7 @@ import json import mock import requests +from six.moves import queue from optimizely import config_manager from optimizely import exceptions as optimizely_exceptions @@ -278,7 +279,10 @@ def test_fetch_datafile(self, _): def test_is_running(self, _): """ Test that polling thread is running after instance of PollingConfigManager is created. """ - with mock.patch('optimizely.config_manager.PollingConfigManager.fetch_datafile') as mock_fetch_datafile: + blocking_queue = queue.Queue() + with mock.patch('optimizely.config_manager.PollingConfigManager.fetch_datafile', + side_effect=lambda: blocking_queue.put_nowait('fetch_datafile called')) as mock_fetch_datafile: project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key') self.assertTrue(project_config_manager.is_running) - mock_fetch_datafile.assert_called_with() + blocking_queue.get(True) + mock_fetch_datafile.assert_called_with() From 7c30eb12016710c94bb75acbcb9a0ec0fb2b8ae1 Mon Sep 17 00:00:00 2001 From: Owais Date: Tue, 17 Sep 2019 17:56:05 +0500 Subject: [PATCH 2/7] add timeout --- tests/test_config_manager.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/tests/test_config_manager.py b/tests/test_config_manager.py index 822143fd..58a6510e 100644 --- a/tests/test_config_manager.py +++ b/tests/test_config_manager.py @@ -283,6 +283,10 @@ def test_is_running(self, _): with mock.patch('optimizely.config_manager.PollingConfigManager.fetch_datafile', side_effect=lambda: blocking_queue.put_nowait('fetch_datafile called')) as mock_fetch_datafile: project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key') - self.assertTrue(project_config_manager.is_running) - blocking_queue.get(True) + + self.assertTrue(project_config_manager.is_running) + # Wait for 2 seconds before asserting mock + try: + blocking_queue.get(True, 2) + except queue.Empty: mock_fetch_datafile.assert_called_with() From a0b74609d03ef360aeb702b6ef27b7403a259baa Mon Sep 17 00:00:00 2001 From: Owais Date: Tue, 17 Sep 2019 18:32:59 +0500 Subject: [PATCH 3/7] Wait for 5 seconds --- tests/test_config_manager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_config_manager.py b/tests/test_config_manager.py index 58a6510e..e22421d0 100644 --- a/tests/test_config_manager.py +++ b/tests/test_config_manager.py @@ -285,8 +285,8 @@ def test_is_running(self, _): project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key') self.assertTrue(project_config_manager.is_running) - # Wait for 2 seconds before asserting mock + # Wait for 5 seconds before asserting mock try: - blocking_queue.get(True, 2) + blocking_queue.get(True, 5) except queue.Empty: mock_fetch_datafile.assert_called_with() From d9d9f2d684ba7fbf820474b9bac1d8d8c7262461 Mon Sep 17 00:00:00 2001 From: Owais Date: Wed, 18 Sep 2019 09:57:29 +0500 Subject: [PATCH 4/7] make it wait for 10 to confirm that it's not the delay issue --- tests/test_config_manager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_config_manager.py b/tests/test_config_manager.py index e22421d0..14bbbdeb 100644 --- a/tests/test_config_manager.py +++ b/tests/test_config_manager.py @@ -285,8 +285,8 @@ def test_is_running(self, _): project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key') self.assertTrue(project_config_manager.is_running) - # Wait for 5 seconds before asserting mock + # Wait for 10 seconds before asserting mock try: - blocking_queue.get(True, 5) + blocking_queue.get(True, 10) except queue.Empty: mock_fetch_datafile.assert_called_with() From daafa4be864bd6e8ee01925c2c35299d9a4199b1 Mon Sep 17 00:00:00 2001 From: Owais Date: Wed, 18 Sep 2019 10:24:10 +0500 Subject: [PATCH 5/7] make update interval small --- optimizely/config_manager.py | 1 + tests/test_config_manager.py | 7 ++++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/optimizely/config_manager.py b/optimizely/config_manager.py index 091bdca9..22000e45 100644 --- a/optimizely/config_manager.py +++ b/optimizely/config_manager.py @@ -299,6 +299,7 @@ def _run(self): try: while self.is_running: self.fetch_datafile() + self.logger.debug("This thread is running") time.sleep(self.update_interval) except (OSError, OverflowError) as err: self.logger.error('Error in time.sleep. ' diff --git a/tests/test_config_manager.py b/tests/test_config_manager.py index 14bbbdeb..2336208c 100644 --- a/tests/test_config_manager.py +++ b/tests/test_config_manager.py @@ -282,11 +282,12 @@ def test_is_running(self, _): blocking_queue = queue.Queue() with mock.patch('optimizely.config_manager.PollingConfigManager.fetch_datafile', side_effect=lambda: blocking_queue.put_nowait('fetch_datafile called')) as mock_fetch_datafile: - project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key') + project_config_manager = config_manager.PollingConfigManager( + sdk_key='some_key', update_interval=1) self.assertTrue(project_config_manager.is_running) - # Wait for 10 seconds before asserting mock + # Wait for 5 seconds before asserting mock try: - blocking_queue.get(True, 10) + blocking_queue.get(True, 5) except queue.Empty: mock_fetch_datafile.assert_called_with() From e80aa16f093c745a9ac25e8bbba0e0efa049b348 Mon Sep 17 00:00:00 2001 From: Owais Date: Wed, 18 Sep 2019 11:20:00 +0500 Subject: [PATCH 6/7] Revert "tests: fix intermittent failure" This reverts commit 5a391414caedebc0adb2d96b103fca14e0f8d9ec. --- optimizely/config_manager.py | 1 - tests/test_config_manager.py | 17 ++++------------- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/optimizely/config_manager.py b/optimizely/config_manager.py index 22000e45..091bdca9 100644 --- a/optimizely/config_manager.py +++ b/optimizely/config_manager.py @@ -299,7 +299,6 @@ def _run(self): try: while self.is_running: self.fetch_datafile() - self.logger.debug("This thread is running") time.sleep(self.update_interval) except (OSError, OverflowError) as err: self.logger.error('Error in time.sleep. ' diff --git a/tests/test_config_manager.py b/tests/test_config_manager.py index 2336208c..a607009d 100644 --- a/tests/test_config_manager.py +++ b/tests/test_config_manager.py @@ -14,7 +14,6 @@ import json import mock import requests -from six.moves import queue from optimizely import config_manager from optimizely import exceptions as optimizely_exceptions @@ -279,15 +278,7 @@ def test_fetch_datafile(self, _): def test_is_running(self, _): """ Test that polling thread is running after instance of PollingConfigManager is created. """ - blocking_queue = queue.Queue() - with mock.patch('optimizely.config_manager.PollingConfigManager.fetch_datafile', - side_effect=lambda: blocking_queue.put_nowait('fetch_datafile called')) as mock_fetch_datafile: - project_config_manager = config_manager.PollingConfigManager( - sdk_key='some_key', update_interval=1) - - self.assertTrue(project_config_manager.is_running) - # Wait for 5 seconds before asserting mock - try: - blocking_queue.get(True, 5) - except queue.Empty: - mock_fetch_datafile.assert_called_with() + with mock.patch('optimizely.config_manager.PollingConfigManager.fetch_datafile') as mock_fetch_datafile: + project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key') + self.assertTrue(project_config_manager.is_running) + mock_fetch_datafile.assert_called_with() From b48f7fbabe36470d0a07a5f23e8c8a5d5860351c Mon Sep 17 00:00:00 2001 From: Owais Date: Wed, 18 Sep 2019 11:21:21 +0500 Subject: [PATCH 7/7] do not assert mocked object --- tests/test_config_manager.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/test_config_manager.py b/tests/test_config_manager.py index a607009d..040ba7b3 100644 --- a/tests/test_config_manager.py +++ b/tests/test_config_manager.py @@ -278,7 +278,6 @@ def test_fetch_datafile(self, _): def test_is_running(self, _): """ Test that polling thread is running after instance of PollingConfigManager is created. """ - with mock.patch('optimizely.config_manager.PollingConfigManager.fetch_datafile') as mock_fetch_datafile: + with mock.patch('optimizely.config_manager.PollingConfigManager.fetch_datafile'): project_config_manager = config_manager.PollingConfigManager(sdk_key='some_key') self.assertTrue(project_config_manager.is_running) - mock_fetch_datafile.assert_called_with()