def testIRCClientTestConnectTimeout(self): channel = '#chromium' nick = 'findittest' # After a number of retries, the exception must be let through. self.mock_socket_obj.recv.side_effect = socket.timeout with self.assertRaises(socket.timeout): with IRCClient('irc.freenode.net', channel, nick, 'CulpritFinder') as _: # Unreachable by design, as we are testing the context manager's # __enter__ method. pass # pragma: no cover
def testIRCClientTestMessageTimeout(self): channel = '#chromium' nick = 'findittest' message = 'Foo bar baz\n\n' other_nick = 'someone' with self.assertRaises(IOError): with IRCClient('irc.freenode.net', channel, nick, 'CulpritFinder') as i: # The connection must have been successful, message sending should not. self.mock_socket_obj.sendall.side_effect = IOError( 'misc error') i.SendMessage(message, other_nick, retry_delay=0)
def SendMessageToIrc(revert_cl_url, commit_position, revision, culprit_key, commit_status): message = _GenerateMessage(revert_cl_url, commit_position, revision, culprit_key, commit_status) try: with IRCClient(_IRC_HOST, _IRC_CHANNEL, _IRC_NICK, _IRC_DISC) as i: i.SendMessage(message) return True # This is just in case any exception happens when sending message. except Exception, e: logging.error('Sending message to IRC failed with %s.' % e) return False
def testIRCClientTestChannelMessage(self): channel = '#chromium' nick = 'findittest' message = 'Foo bar baz\n\n' with IRCClient('irc.freenode.net', channel, nick, 'CulpritFinder') as i: i.SendMessage(message) self.assertEqual(self.mock_socket_obj.sendall.call_args_list, [ mock.call('USER %s %s %s : CulpritFinder\r\n' % (nick, nick, nick)), mock.call('NICK %s\r\n' % nick), mock.call('JOIN %s\r\n' % channel), mock.call('PRIVMSG %s :%s\r\n' % (channel, message)), mock.call('PART %s\r\n' % channel), mock.call('QUIT\r\n') ])
def testIRCClientTestLongIntro(self): channel = '#chromium' nick = 'findittest' message = 'Foo bar baz\n\n' # Force socket.recv to be called multiple times. self.mock_socket_obj.recv = mock.Mock( side_effect=['\n', '\n', SERVER_RESPONSE]) with IRCClient('irc.freenode.net', channel, nick, 'CulpritFinder') as i: i.SendMessage(message) self.assertEqual(self.mock_socket_obj.sendall.call_args_list, [ mock.call('USER %s %s %s : CulpritFinder\r\n' % (nick, nick, nick)), mock.call('NICK %s\r\n' % nick), mock.call('JOIN %s\r\n' % channel), mock.call('PRIVMSG %s :%s\r\n' % (channel, message)), mock.call('PART %s\r\n' % channel), mock.call('QUIT\r\n') ])
def testIRCClientTestTruncatedJoinMessage(self): channel = '#chromium' nick = 'findittest' message = 'Foo bar baz\n\n' # Force socket.recv to be called multiple times. self.mock_socket_obj.recv = mock.Mock(side_effect=[ 'Preamble', 'MOTD message\r\n', 'Foo\r\n:verne.freenode.net 353 findit', 'test @ #chromium :findittest @someone\r\nBar\r\n' ]) with IRCClient('irc.freenode.net', channel, nick, 'CulpritFinder') as i: i.SendMessage(message) self.assertEqual(self.mock_socket_obj.sendall.call_args_list, [ mock.call('USER %s %s %s : CulpritFinder\r\n' % (nick, nick, nick)), mock.call('NICK %s\r\n' % nick), mock.call('JOIN %s\r\n' % channel), mock.call('PRIVMSG %s :%s\r\n' % (channel, message)), mock.call('PART %s\r\n' % channel), mock.call('QUIT\r\n') ])