Skip to content

Commit

Permalink
feat(tests): improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mandrewcito committed Sep 2, 2024
1 parent d7d5f08 commit 9b1cf61
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 48 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ upload:
coverage:
coverage run -m unittest discover -s test/ -p "*_test.py"
coverage html --omit="venv/*" -d coverage_html
firefox coverage_html/index.html

clean:
@find . -name "*.pyc" -exec rm -f '{}' +
Expand Down
30 changes: 0 additions & 30 deletions signalrcore/transport/websockets/reconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,6 @@
from enum import Enum


class ConnectionStateChecker(object):
def __init__(
self,
ping_function,
keep_alive_interval,
sleep=1):
self.sleep = sleep
self.keep_alive_interval = keep_alive_interval
self.last_message = time.time()
self.ping_function = ping_function
self.running = False
self._thread = None

def start(self):
self.running = True
self._thread = threading.Thread(target=self.run)
self._thread.daemon = True
self._thread.start()

def run(self):
while self.running:
time.sleep(self.sleep)
time_without_messages = time.time() - self.last_message
if self.keep_alive_interval < time_without_messages:
self.ping_function()

def stop(self):
self.running = False


class ReconnectionType(Enum):
raw = 0 # Reconnection with max reconnections and constant sleep time
interval = 1 # variable sleep time
Expand Down
15 changes: 1 addition & 14 deletions signalrcore/transport/websockets/websocket_transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@
import time
import queue
import ssl
from signal import pthread_kill, SIGKILL
from typing import Callable, List, Any, Tuple
from .reconnection import ConnectionStateChecker
from typing import List, Any, Tuple
from .connection import ConnectionState
from ...messages.ping_message import PingMessage
from ...hub.errors import HubError, HubConnectionError, UnAuthorizedHubError
from ...protocol.messagepack_protocol import MessagePackHubProtocol
from ..base_transport import BaseTransport
from ...helpers import Helpers


class WebsocketTransport(BaseTransport):
connection_checker: ConnectionStateChecker

def __init__(
self,
url="",
Expand Down Expand Up @@ -121,13 +116,6 @@ def _start(self) -> bool:
self.logger.error(ex)
return False
return True

def init_state_check(self):
self.connection_checker = ConnectionStateChecker(
lambda: self.state == ConnectionState.connected
and self.send(PingMessage()),
self.keep_alive_interval
)

def negotiate(self):
negotiate_url = Helpers.get_negotiate_url(self.url)
Expand Down Expand Up @@ -215,7 +203,6 @@ def _send(self, message):
opcode=0x2
if type(self.protocol) is MessagePackHubProtocol else
0x1)
#self.connection_checker.last_message = time.time()
if self.reconnection_handler is not None:
self.reconnection_handler.reset()
except websocket._exceptions.WebSocketConnectionClosedException as ex:
Expand Down
4 changes: 0 additions & 4 deletions test/reconnection_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ def test_no_reconnect(self):

connection.transport._ws.close(status=1000, reason="Closed".encode("utf-8"))

self.assertRaises(
HubConnectionError,
lambda: connection.send("DisconnectMe", []))

self.assertTrue(self._locks[identifier].acquire(timeout=40)) # Released on close

# reconnecting not configured must not enqueue message
Expand Down

0 comments on commit 9b1cf61

Please sign in to comment.