Skip to content

Commit aa2eca3

Browse files
oakbanialiabbasrizvi
authored andcommitted
fix: avoid type error for Python 3 (#214)
1 parent f57d5bc commit aa2eca3

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

optimizely/event/event_processor.py

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

1414
import abc
15+
import numbers
1516
import threading
1617
import time
1718

@@ -100,13 +101,14 @@ def __init__(self,
100101
self.logger.error(enums.Errors.INVALID_INPUT.format('notification_center'))
101102
self.notification_center = _notification_center.NotificationCenter()
102103

104+
self.executor = None
103105
if start_on_init is True:
104106
self.start()
105107

106108
@property
107109
def is_running(self):
108110
""" Property to check if consumer thread is alive or not. """
109-
return self.executor.isAlive()
111+
return self.executor.isAlive() if self.executor else False
110112

111113
def _validate_intantiation_props(self, prop, prop_name):
112114
""" Method to determine if instantiation properties like batch_size, flush_interval
@@ -121,12 +123,18 @@ def _validate_intantiation_props(self, prop, prop_name):
121123
False if property name is batch_size and value is a floating point number.
122124
True otherwise.
123125
"""
124-
if (prop_name == 'batch_size' and not isinstance(prop, int)) or prop is None or prop <= 0 or \
125-
not validator.is_finite_number(prop):
126+
is_valid = True
127+
128+
if prop is None or not validator.is_finite_number(prop) or prop <= 0:
129+
is_valid = False
130+
131+
if prop_name == 'batch_size' and not isinstance(prop, numbers.Integral):
132+
is_valid = False
133+
134+
if is_valid is False:
126135
self.logger.info('Using default value for {}.'.format(prop_name))
127-
return False
128136

129-
return True
137+
return is_valid
130138

131139
def _get_time(self, _time=None):
132140
""" Method to return rounded off time as integer in seconds. If _time is None, uses current time.
@@ -279,7 +287,8 @@ def stop(self):
279287
self.event_queue.put(self._SHUTDOWN_SIGNAL)
280288
self.logger.warning('Stopping Scheduler.')
281289

282-
self.executor.join(self.timeout_interval.total_seconds())
290+
if self.executor:
291+
self.executor.join(self.timeout_interval.total_seconds())
283292

284293
if self.is_running:
285294
self.logger.error('Timeout exceeded while attempting to close for ' + str(self.timeout_interval) + ' ms.')

tests/test_event_processor.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def test_init__invalid_flush_interval(self):
326326
self.assertEqual(self._event_processor.flush_interval, timedelta(seconds=30))
327327
mock_config_logging.info.assert_called_with('Using default value for flush_interval.')
328328

329-
def test_init__NaN_flush_interval(self):
329+
def test_init__bool_flush_interval(self):
330330
event_dispatcher = TestEventDispatcher()
331331

332332
with mock.patch.object(self.optimizely, 'logger') as mock_config_logging:
@@ -343,6 +343,23 @@ def test_init__NaN_flush_interval(self):
343343
self.assertEqual(self._event_processor.flush_interval, timedelta(seconds=30))
344344
mock_config_logging.info.assert_called_with('Using default value for flush_interval.')
345345

346+
def test_init__string_flush_interval(self):
347+
event_dispatcher = TestEventDispatcher()
348+
349+
with mock.patch.object(self.optimizely, 'logger') as mock_config_logging:
350+
self._event_processor = BatchEventProcessor(event_dispatcher,
351+
self.optimizely.logger,
352+
True,
353+
self.event_queue,
354+
self.MAX_BATCH_SIZE,
355+
"True",
356+
self.MAX_TIMEOUT_INTERVAL_SEC
357+
)
358+
359+
# default flush interval is 30s.
360+
self.assertEqual(self._event_processor.flush_interval, timedelta(seconds=30))
361+
mock_config_logging.info.assert_called_with('Using default value for flush_interval.')
362+
346363
def test_init__invalid_timeout_interval(self):
347364
event_dispatcher = TestEventDispatcher()
348365

0 commit comments

Comments
 (0)