def test_thread_str(): """ Test if the string representaion of a thread is correct """ thread = Thread(1) message_11 = Message(11, "Head of Message 1", "Content of Message 11") message_12 = Message(12, "Head of Message 2", "Content of Message 12") thread.add(message_11) thread.add(message_12) assert_equal(str(thread), "1(12, 11)")
def test_thread_add(): """ Test if a Thread object can add a message to its message queue. """ thread = Thread(1) message_11 = Message(11, "Head of Message 1", "Content of Message 1") message_12 = Message(12, "Head of Message 2", "Content of Message 2") thread.add(message_11) assert_equal(thread.messages, deque([message_11])) thread.add(message_12) assert_equal(thread.messages, deque([message_12, message_11]))
def add(self, message, thread_id): """ Adds a message to a thread, and put the thread to the front of the queue. Args: message (str): The new message to be added to a thread. thread_id (int): The is of the thread that should contain the new message. Raises: TypeError: If any input arg is not in correct type, TypeError will be raised. """ logging.info("Trying to add message_%s to threadlist ...", str(message) if message else "") if not isinstance(thread_id, int): raise TypeError("thread_id should be int type, got " + str(type(thread_id))) if not isinstance(message, Message): raise TypeError("message should be Message type, got " + str(type(message))) if thread_id in self._threads: thread = self._threads.pop(thread_id) thread.add(message) else: thread = Thread(thread_id) thread.add(message) self._threads[thread_id] = thread logging.info("Added message_%s to threalist.", str(message) if message else "")
def test_thread_add_type_error(): """ Test invalid input type for add method. """ thread = Thread(1) thread.add(1)