def test_publish(monkeypatch): mock_connection(monkeypatch) connection = Connection(connection_params) connection.connect() connection.publish(exchange="exchange", key="key", msg="message") assert (published_message == json.dumps("message")) connection.disconnect()
def test_publish(monkeypatch): mock_connection(monkeypatch) connection = Connection(connection_params) assert connection._params == {"load_message": True, "prefetch_count": 1, "stream_contents": False} connection.connect() connection.publish(exchange="exchange", key="key", msg="message") assert(published_message == json.dumps("message")) connection.disconnect()
class LogPublisher(): _connection = None _connection_lock = threading.Lock() _auto_disconnect_thread = None _auto_disconnect_timeout = 0 def __init__(self, connection_params=CONNECTION_PARAMS, queue_name=LOG_QUEUE): # Register the connection params and log queue self._connection_params = connection_params self._queue = get_queue(queue_name) def publish(self, level, msg): # Acquire a lock for the connection with self._connection_lock: # Connect to the message broker, auto disconnect after timeout seconds self._auto_connect(timeout=2) # Publish the message self._connection.publish(self._queue, level, msg) def _auto_connect(self, timeout): self._auto_disconnect_timeout = timeout if self._connection is None: # Start a connection if no connection is active self._connection = Connection(self._connection_params) self._connection.connect() # Start auto disconnect thread if self._auto_disconnect_thread is not None: # Join any previously ended threads self._auto_disconnect_thread.join() self._auto_disconnect_thread = threading.Thread( target=self._auto_disconnect) self._auto_disconnect_thread.start() def _disconnect(self): if self._connection is not None: self._connection.disconnect() self._connection = None def _auto_disconnect(self): while True: time.sleep(1) with self._connection_lock: if self._auto_disconnect_timeout > 0: self._auto_disconnect_timeout -= 1 else: self._disconnect() break
def test_publish(monkeypatch): # Test publish message mock_connection(monkeypatch) connection = Connection(connection_params) connection.connect() queue = { "exchange": "exchange", "name": "name", "key": "key" } connection.publish(queue, "key", "message") assert(published_message == json.dumps("message")) connection.disconnect()
def test_disconnect(): # Test if a disconnect can be called without an earlier connect connection = Connection('') assert (connection.disconnect() is None)
def test_idempotent_disconnect(): # Test disconnect repeated execution connection = Connection('') assert (connection.disconnect() is None) assert (connection.disconnect() is None)