Skip to content

Commit c063e34

Browse files
committed
more tests and some polishing
1 parent 1069ad3 commit c063e34

File tree

7 files changed

+27
-36
lines changed

7 files changed

+27
-36
lines changed

splitio/push/manager.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -159,23 +159,6 @@ def _setup_next_token_refresh(self, token):
159159
self._next_refresh.setName('TokenRefresh')
160160
self._next_refresh.start()
161161

162-
def _handle_connection_ready(self):
163-
"""Handle a successful connection to SSE."""
164-
self._feedback_loop.put(Status.PUSH_SUBSYSTEM_UP)
165-
_LOGGER.info('sse initial event received. enabling')
166-
167-
def _handle_connection_end(self, shutdown_requested):
168-
"""
169-
Handle a connection ending.
170-
171-
If the connection shutdown was not requested, trigger a restart.
172-
173-
:param shutdown_requested: whether the shutdown was requested or unexpected.
174-
:type shutdown_requested: True
175-
"""
176-
if not shutdown_requested:
177-
self._feedback_loop.put(Status.PUSH_RETRYABLE_ERROR)
178-
179162
def _handle_message(self, event):
180163
"""
181164
Handle incoming update message.
@@ -237,3 +220,18 @@ def _handle_error(self, event):
237220
feedback = self._status_tracker.handle_ably_error(event)
238221
if feedback is not None:
239222
self._feedback_loop.put(feedback)
223+
224+
def _handle_connection_ready(self):
225+
"""Handle a successful connection to SSE."""
226+
self._feedback_loop.put(Status.PUSH_SUBSYSTEM_UP)
227+
_LOGGER.info('sse initial event received. enabling')
228+
229+
def _handle_connection_end(self):
230+
"""
231+
Handle a connection ending.
232+
233+
If the connection shutdown was not requested, trigger a restart.
234+
"""
235+
feedback = self._status_tracker.handle_disconnect()
236+
if feedback is not None:
237+
self._feedback_loop.put(feedback)

splitio/push/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""SSE Notification definitions."""
22
import abc
33
import json
4-
import time
54
from enum import Enum
65

76
from future.utils import raise_from
87
from six import add_metaclass
98

109
from splitio.util.decorators import abstract_property
10+
from splitio.util import utctime_ms
1111
from splitio.push.sse import SSE_EVENT_ERROR, SSE_EVENT_MESSAGE
1212

1313

@@ -89,7 +89,7 @@ def __init__(self, code, status_code, message, href):
8989
self._status_code = status_code
9090
self._message = message
9191
self._href = href
92-
self._timestamp = int(time.time() * 1000) # TODO: replace with UTC function after merge
92+
self._timestamp = utctime_ms()
9393

9494
@property
9595
def event_type(self): #pylint:disable=no-self-use

splitio/push/splitsse.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,12 @@ def start(self, token):
117117

118118
def connect(url):
119119
"""Connect to sse in a blocking manner."""
120-
shutdown_requested = False
121120
try:
122-
shutdown_requested = self._client.start(url, timeout=self.KEEPALIVE_TIMEOUT)
121+
self._client.start(url, timeout=self.KEEPALIVE_TIMEOUT)
123122
finally:
124123
self._status = SplitSSEClient._Status.IDLE
125124
self._sse_connection_closed.set()
126-
self._on_disconnected(shutdown_requested)
125+
self._on_disconnected()
127126

128127
url = self._build_url(token)
129128
task = threading.Thread(target=connect, name='SSEConnection', args=(url,))

splitio/push/status_tracker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Status(Enum):
1717
PUSH_NONRETRYABLE_ERROR = 3
1818

1919

20-
class LastEventTimestamps(object):
20+
class LastEventTimestamps(object): # pylint:disable=too-few-public-methods
2121
"""Simple class to keep track of the last time multiple events occurred."""
2222

2323
def __init__(self):
@@ -110,10 +110,10 @@ def handle_ably_error(self, event):
110110
:returns: A new status if required. None otherwise
111111
:rtype: Optional[Status]
112112
"""
113-
if self._shutdown_expected: # we don't care about occupancy if a disconnection is expected
113+
if self._shutdown_expected: # we don't care about an incoming error if a shutdown is expected
114114
return None
115115

116-
_LOGGER.debug('handling update event: %s', str(event))
116+
_LOGGER.debug('handling ably error event: %s', str(event))
117117
if event.should_be_ignored():
118118
_LOGGER.debug('ignoring sse error message: %s', event)
119119
return None
@@ -160,7 +160,7 @@ def _update_status(self):
160160

161161
return None
162162

163-
def _handle_disconnect(self):
163+
def handle_disconnect(self):
164164
"""
165165
Handle non-requested SSE disconnection.
166166

splitio/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '8.3.0-rc3'
1+
__version__ = '8.3.0-rc5'

tests/push/test_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_connection_failure(self, mocker):
6767

6868
def new_start(*args, **kwargs): # pylint: disable=unused-argument
6969
"""splitsse.start mock."""
70-
thread = Thread(target=manager._handle_connection_end, args=(False,))
70+
thread = Thread(target=manager._handle_connection_end)
7171
thread.setDaemon(True)
7272
thread.start()
7373
return False

tests/push/test_splitsse.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,15 @@ def handler(event):
2222
status = {
2323
'on_connect': False,
2424
'on_disconnect': False,
25-
'requested': False
2625
}
2726

2827
def on_connect():
2928
"""On connect handler."""
3029
status['on_connect'] = True
3130

32-
def on_disconnect(requested):
31+
def on_disconnect():
3332
"""On disconnect handler."""
3433
status['on_disconnect'] = True
35-
status['requested'] = requested
3634

3735
request_queue = Queue()
3836
server = SSEMockServer(request_queue)
@@ -68,7 +66,6 @@ def on_disconnect(requested):
6866

6967
assert status['on_connect']
7068
assert status['on_disconnect']
71-
assert status['requested']
7269

7370
def test_split_sse_error(self):
7471
"""Test correct initialization. Client ends the connection."""
@@ -84,17 +81,15 @@ def handler(event):
8481
status = {
8582
'on_connect': False,
8683
'on_disconnect': False,
87-
'requested': False
8884
}
8985

9086
def on_connect():
9187
"""On connect handler."""
9288
status['on_connect'] = True
9389

94-
def on_disconnect(requested):
90+
def on_disconnect():
9591
"""On disconnect handler."""
9692
status['on_disconnect'] = True
97-
status['requested'] = requested
9893

9994
client = SplitSSEClient(handler, on_connect, on_disconnect,
10095
base_url='http://localhost:' + str(server.port()))
@@ -116,4 +111,3 @@ def on_disconnect(requested):
116111

117112
assert status['on_connect']
118113
assert status['on_disconnect']
119-
assert not status['requested']

0 commit comments

Comments
 (0)