def test_broadcast(self): self.add_two_sockets() self.sock2.write_message = Mock(side_effect=WebSocketClosedError()) self.under_test.broadcast(MESSAGE) self.sock1.write_message.assert_called_once_with(MESSAGE) self.sock2.write_message.assert_called_once_with(MESSAGE)
def write_message(self, message, binary=False): if self.ws_connection is None: raise WebSocketClosedError() # packet = msgpack.packb(message) logger.info('RSP[%d]: %s', self.uid, message) packet = json.dumps(message) return self.ws_connection.write_message(packet, binary=binary)
def test_remove_closed_socket_passes(self): self.add_two_sockets() self.sock2.write_message = Mock(side_effect=WebSocketClosedError()) self.under_test.remove_socket(self.sock2, MESSAGE) self.assert_list([self.sock1]) self.assert_closed_with_message(self.sock2, MESSAGE)
def test_broadcast_dead_peer(self): """Remove dead peers detected on broadcast.""" socket = self.get_socket() peer = self.get_socket() peer.write_message.side_effect = WebSocketClosedError('Already closed.') self.backend.add_subscriber('123', socket) self.backend.add_subscriber('123', peer) result = self.backend.get_subscribers(channel='123') self.assertEqual(set(result), set([socket, peer])) self.backend.broadcast(message='ping', channel='123', sender=socket.uuid) yield self.pause() result = self.backend.get_subscribers(channel='123') self.assertEqual(set(result), set([socket]))
def js(self, code=None, path=None, **kwargs): if self.conn is None: try: self.conn = yield websocket_connect( self.url, connect_timeout=self.timeout) except OSError as exc: import errno if exc.errno != errno.ECONNREFUSED: raise # TODO: node_path self.proc = yield daemon( [which('node'), self._path, '--port=%s' % self.port], first_line=re.compile('pynode: 1.\d+.\d+ port: %s' % self.port), cwd=self.cwd, ) self.conn = yield websocket_connect( self.url, connect_timeout=self.timeout) # code= takes preference over path= if code is not None: kwargs['code'] = code elif path is not None: kwargs['path'] = path # Send the commands. If node has died, clear the connection. try: yield self.conn.write_message(json.dumps(kwargs)) except WebSocketClosedError: self.conn = None raise # Receive the response. # Note: read_message() cannot be called again while a request is running. # (Yes, that's odd. Maybe Anand is missing something.) # So wait until the read_future is cleared. while self.conn.read_future is not None: yield sleep(self._delay) msg = yield self.conn.read_message() # If node has died, clear the connection to restart it. if msg is None: self.conn = None raise WebSocketClosedError() # Parse the result as JSON. Log errors if any result = json.loads(msg) if result['error']: app_log.error(result['error']['stack']) raise Return(result)
def on_connected(reconnected_future): """ 2) Send request. """ exception = reconnected_future.exception() if exception is not None: LOGGER.error( 'Fatal (re)connection error occurred while sending a request.' ) future.set_exception(exception) else: try: if self.connection is None: raise WebSocketClosedError() write_future = self.connection.write_message( request.json()) except (WebSocketClosedError, StreamClosedError) as exc: # We were disconnected. # Save request context as a request to send. # We will re-try to send it later once reconnected. self._register_to_send(request_context) # Transfer exception to returned future. future.set_exception(exc) else: write_future.add_done_callback(on_message_written)
def send(self, handler): raise WebSocketClosedError()
def write_socket_message(self, message): if self.socket_client: self.socket_client.write_message(message) else: raise WebSocketClosedError()
def send(self, data): if self.handler.ws_connection is None: raise WebSocketClosedError() return self.handler.write_message(data, binary=False)