Esempio n. 1
0
    def test_compile_request(self, mock_compile, mock_get_banned, mock_sleep):
        test_comment_id = cb.CONFIG['tests']['integration']['reddit']['test_comment']
        accepted_substring = cb.CONFIG['tests']['integration']['reddit']['accepted_substring']
        mock_compile.return_value = {
            'cmpinfo': '',
            'error': 'OK',
            'input': "Hello World",
            'langId': 116,
            'langName': "Python 3",
            'output': "Hello World\n",
            'public': True,
            'result': 15,
            'signal': 0,
            'source': "x = input()\nprint(x)",
            'status': 0,
            'stderr': "",
            'link': 'jnImo8'
        }
        mock_get_banned.return_value = []
        test_r = praw.Reddit(cb.USER_AGENT)
        test_r.login(cb.R_USERNAME, cb.R_PASSWORD)
        test_comment = None
        for message in test_r.get_inbox():
            if message.id == test_comment_id:
                test_comment = message
                break
        if test_comment is None:
            self.skipTest("Test comment not found")

        cb.main()
        test_comment.mark_as_unread()
        self.assertTrue(mock_compile.called)
        user = test_r.get_redditor(cb.R_USERNAME)
        test_reply = user.get_comments(limit=1).next()
        self.assertIn(accepted_substring, test_reply.body)
Esempio n. 2
0
def main():
    errors = []
    try:
        bot.log("Initializing bot")
        while True:
            try:
                for error in errors:
                    bot.log(error, alert=True)
                bot.main()
            except HTTPError as e:
                # HTTP Errors may indicate reddit is overloaded.
                # Sleep for some extra time. 
                bot.log(str(e) + " ")
                time.sleep(SLEEP_TIME*2)
            except ConnectionError as e:
                bot.log(str(e) + " ")
                time.sleep(SLEEP_TIME*2)
            except Exception as e:
                # If another exception occurs, add the message to a buffer so
                # it can be sent to the admins in the try block above.
                # Otherwise the bot.log method may cause another error that
                # won't be caught.
                error_msg = "Error running bot.main: {error}".format(error=e)
                # Avoid adding duplicates.
                if len(errors) == 0 or errors[-1] != error_msg:
                    errors.append(error_msg)
            time.sleep(SLEEP_TIME)
    except KeyboardInterrupt:
        exit_msg = ''
        exit(0)
    except Exception as e:
        tb = traceback.format_exc()
        exit_msg = "Depoyment error: {traceback}\n".format(traceback=tb)
        bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True)
        exit(1)
Esempio n. 3
0
 def test_main(self, mock_reddit, mock_process_unread):
     r = mock_reddit.return_value
     mock_inbox = [Mock(), Mock(), Mock()]
     r.get_unread.return_value = mock_inbox
     cb.main()
     r.login.assert_called_with(cb.R_USERNAME, cb.R_PASSWORD)
     for new in mock_inbox:
         mock_process_unread.assert_any_call(new, r)
         new.mark_as_read.assert_called_with()
Esempio n. 4
0
 def test_main(self, mock_reddit, mock_process_unread):
     r = mock_reddit.return_value
     mock_inbox = [Mock(), Mock(), Mock()]
     r.get_unread.return_value = mock_inbox
     cb.main()
     self.assertTrue(r.login.called)
     for new in mock_inbox:
         mock_process_unread.assert_any_call(new, r)
         new.mark_as_read.assert_called_with()
Esempio n. 5
0
 def test_main(self, mock_reddit, mock_process_unread):
     r = mock_reddit.return_value
     cb.R_USERNAME = '******'
     cb.R_PASSWORD = '******'
     mock_inbox = [Mock(), Mock(), Mock()]
     r.get_unread.return_value = mock_inbox
     cb.main()
     r.login.assert_called_with('TestUser', 'hunter2')
     for new in mock_inbox:
         mock_process_unread.assert_any_call(new, r)
         new.mark_as_read.assert_called_with()
Esempio n. 6
0
def main():
    errors, log_buffer = {}, []
    try:
        bot.log("Initializing bot")
        while True:
            try:
                for error in log_buffer:
                    bot.log(error, alert=True)
                log_buffer = []
                bot.main()
                errors = {}
                time.sleep(SLEEP_TIME)
            except HTTPError as e:
                # HTTP Errors may indicate reddit is overloaded.
                # Sleep for some extra time.
                bot.log(str(e) + " ")
                time.sleep(ERROR_TIMEOUT)
            except ConnectionError as e:
                bot.log(str(e) + " ")
                time.sleep(ERROR_TIMEOUT)
            except Exception as e:
                error = str(e) or repr(e)
                if errors.get(error):
                    errors[error] += 1
                    if errors[error] >= ERROR_LIMIT:
                        bot.log(
                            "Encounted error of type \"{}\" {} times in a row, "
                            "bot shutting down".format(error, ERROR_LIMIT))
                        exit(1)
                    bot.log("Error running bot.main: {} ({}/{})".format(
                        error, errors[error], ERROR_LIMIT))
                else:
                    errors[error] = 1
                    # If another exception occurs, add the message to a buffer so
                    # it can be sent to the admins in the try block above.
                    # Otherwise the bot.log method may cause another error that
                    # won't be caught.
                    tb = traceback.format_exc()
                    error_msg = "Error running bot.main:\n{error}".format(
                        error=bot.code_block(tb))
                    # Avoid adding duplicates.
                    if len(log_buffer) == 0 or log_buffer[-1] != error_msg:
                        log_buffer.append(error_msg)
                time.sleep(ERROR_TIMEOUT)
    except KeyboardInterrupt:
        exit_msg = ''
        exit(0)
    except Exception as e:
        tb = traceback.format_exc()
        exit_msg = "Depoyment error:\n{traceback}\n".format(
            traceback=bot.code_block(tb))
        bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True)
        exit(1)
Esempio n. 7
0
def main():
    errors, log_buffer = {}, []
    try:
        bot.log("Initializing bot")
        while True:
            try:
                for error in log_buffer:
                    bot.log(error, alert=True)
                log_buffer = []
                bot.main()
                errors = {}
                time.sleep(SLEEP_TIME)
            except HTTPError as e:
                # HTTP Errors may indicate reddit is overloaded.
                # Sleep for some extra time.
                bot.log(str(e) + " ")
                time.sleep(ERROR_TIMEOUT)
            except ConnectionError as e:
                bot.log(str(e) + " ")
                time.sleep(ERROR_TIMEOUT)
            except Exception as e:
                error = str(e) or repr(e)
                if errors.get(error):
                    errors[error] += 1
                    if errors[error] >= ERROR_LIMIT:
                        bot.log(
                            'Encounted error of type "{}" {} times in a row, '
                            "bot shutting down".format(error, ERROR_LIMIT)
                        )
                        exit(1)
                    bot.log("Error running bot.main: {} ({}/{})".format(error, errors[error], ERROR_LIMIT))
                else:
                    errors[error] = 1
                    # If another exception occurs, add the message to a buffer so
                    # it can be sent to the admins in the try block above.
                    # Otherwise the bot.log method may cause another error that
                    # won't be caught.
                    tb = traceback.format_exc()
                    error_msg = "Error running bot.main:\n{error}".format(error=bot.code_block(tb))
                    # Avoid adding duplicates.
                    if len(log_buffer) == 0 or log_buffer[-1] != error_msg:
                        log_buffer.append(error_msg)
                time.sleep(ERROR_TIMEOUT)
    except KeyboardInterrupt:
        exit_msg = ""
        exit(0)
    except Exception as e:
        tb = traceback.format_exc()
        exit_msg = "Depoyment error:\n{traceback}\n".format(traceback=bot.code_block(tb))
        bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True)
        exit(1)
Esempio n. 8
0
def main():
    try:
        bot.log("Initializing bot")
        while True:
            try:
                bot.main()
            except HTTPError as e:
                # HTTP Errors may indicate reddit is overloaded.
                # Sleep for some extra time. 
                bot.log(str(e) + " ")
                time.sleep(SLEEP_TIME*2)
            except Exception as e:
                bot.log("Error running bot.main: {error}".format(
                        error=e), alert=True)
            time.sleep(SLEEP_TIME)
    except KeyboardInterrupt:
        exit_msg = ''
    except Exception as e:
        tb = traceback.format_exc()
        exit_msg = "Depoyment error: {traceback}\n".format(traceback=tb)
        bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True)
Esempio n. 9
0
def main():
    try:
        bot.log("Initializing bot")
        while True:
            try:
                bot.main()
            except HTTPError as e:
                # HTTP Errors may indicate reddit is overloaded.
                # Sleep for some extra time.
                bot.log(str(e) + " ")
                time.sleep(SLEEP_TIME * 2)
            except Exception as e:
                bot.log("Error running bot.main: {error}".format(error=e),
                        alert=True)
            time.sleep(SLEEP_TIME)
    except KeyboardInterrupt:
        exit_msg = ''
    except Exception as e:
        tb = traceback.format_exc()
        exit_msg = "Depoyment error: {traceback}\n".format(traceback=tb)
        bot.log("{msg}Bot shutting down".format(msg=exit_msg), alert=True)
Esempio n. 10
0
    def test_compile_request(self, mock_compile, mock_get_banned, mock_sleep):
        test_comment_id = cb.CONFIG['tests']['integration']['reddit'][
            'test_comment']
        accepted_substring = cb.CONFIG['tests']['integration']['reddit'][
            'accepted_substring']
        mock_compile.return_value = {
            'cmpinfo': '',
            'error': 'OK',
            'input': "Hello World",
            'langId': 116,
            'langName': "Python 3",
            'output': "Hello World\n",
            'public': True,
            'result': 15,
            'signal': 0,
            'source': "x = input()\nprint(x)",
            'status': 0,
            'stderr': "",
            'link': 'jnImo8'
        }
        mock_get_banned.return_value = []
        test_r = praw.Reddit(cb.USER_AGENT)
        test_r.login(cb.R_USERNAME, cb.R_PASSWORD)
        test_comment = None
        for message in test_r.get_inbox():
            if message.id == test_comment_id:
                test_comment = message
                break
        if test_comment is None:
            self.skipTest("Test comment not found")

        cb.main()
        test_comment.mark_as_unread()
        self.assertTrue(mock_compile.called)
        user = test_r.get_redditor(cb.R_USERNAME)
        test_reply = user.get_comments(limit=1).next()
        self.assertIn(accepted_substring, test_reply.body)