Skip to content

Commit

Permalink
feat(tests): refactor connection state
Browse files Browse the repository at this point in the history
  • Loading branch information
mandrewcito committed Jul 26, 2024
1 parent e628bf4 commit 595e5fa
Show file tree
Hide file tree
Showing 8 changed files with 399 additions and 158 deletions.
2 changes: 1 addition & 1 deletion signalrcore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def configure_logger(level=logging.INFO, handler=None):
handler = logging.StreamHandler()
handler.setFormatter(
logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
'%(asctime)s [%(filename)s:%(lineno)d] %(name)s - %(levelname)s - %(message)s'))
handler.setLevel(level)
logger.addHandler(handler)
logger.setLevel(level)
Expand Down
8 changes: 8 additions & 0 deletions signalrcore/hub/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,11 @@ class HubConnectionError(ValueError):
"""Hub connection error
"""
pass


class HubReconnectingError(HubError):
pass


class HandShakeError(HubError):
pass
7 changes: 3 additions & 4 deletions signalrcore/transport/websockets/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@


class ConnectionState(Enum):
connecting = 0
connected = 1
reconnecting = 2
disconnected = 4
disconnected = 0
connecting = 1
connected = 2
9 changes: 9 additions & 0 deletions signalrcore/transport/websockets/reconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def __init__(self):
self.reconnecting = False
self.attempt_number = 0
self.last_attempt = time.time()

def can_reconnect(self) -> bool:
raise NotImplementedError()

def next(self):
raise NotImplementedError()
Expand All @@ -58,6 +61,9 @@ def __init__(self, sleep_time, max_attempts):
self.sleep_time = sleep_time
self.max_reconnection_attempts = max_attempts

def can_reconnect(self) -> bool:
return False

def next(self):
self.reconnecting = True
if self.max_reconnection_attempts is not None:
Expand All @@ -76,6 +82,9 @@ class IntervalReconnectionHandler(ReconnectionHandler):
def __init__(self, intervals):
super(IntervalReconnectionHandler, self).__init__()
self._intervals = intervals

def can_reconnect(self) -> bool:
return self.attempt_number >= len(self._intervals)

def next(self):
self.reconnecting = True
Expand Down
Loading

0 comments on commit 595e5fa

Please sign in to comment.