<commit_before>import pytest
import gnsq


class MockConnection(object):
    def __init__(self, operations):
        self.operations = iter(operations)

    def __getattr__(self, name):
        expected_name, expected_args = self.operations.next()
        assert name == expected_name

        def check_args(*args):
            assert args == expected_args

        return check_args

    def assert_finished(self):
        with pytest.raises(StopIteration):
            self.operations.next()


def test_basic():
    message = gnsq.Message(None, 0, 42, '1234', 'sup')
    assert message.id == '1234'
    assert message.body == 'sup'
    assert message.has_responded() is False


def test_finish():
    mock_conn = MockConnection([
        ('finish', ('1234',)),
    ])

    message = gnsq.Message(mock_conn, 0, 42, '1234', 'sup')
    assert message.has_responded() is False

    message.finish()
    assert message.has_responded() is True

    with pytest.raises(gnsq.errors.NSQException):
        message.finish()


def test_requeue():
    mock_conn = MockConnection([
        ('requeue', ('1234', 0)),
    ])

    message = gnsq.Message(mock_conn, 0, 42, '1234', 'sup')
    assert message.has_responded() is False

    message.requeue()
    assert message.has_responded() is True

    with pytest.raises(gnsq.errors.NSQException):
        message.requeue()

    mock_conn.assert_finished()


def test_requeue_timeout():
    mock_conn = MockConnection([
        ('requeue', ('1234', 1000)),
    ])

    message = gnsq.Message(mock_conn, 0, 42, '1234', 'sup')
    assert message.has_responded() is False

    message.requeue(1000)
    assert message.has_responded() is True

    with pytest.raises(gnsq.errors.NSQException):
        message.requeue(1000)

    mock_conn.assert_finished()


def test_touch():
    mock_conn = MockConnection([
        ('touch', ('1234',)),
        ('touch', ('1234',)),
        ('touch', ('1234',)),
        ('finish', ('1234',)),
    ])

    message = gnsq.Message(mock_conn, 0, 42, '1234', 'sup')
    assert message.has_responded() is False

    message.touch()
    message.touch()
    message.touch()
    assert message.has_responded() is False

    message.finish()
    assert message.has_responded() is True

    with pytest.raises(gnsq.errors.NSQException):
        message.touch()

    mock_conn.assert_finished()<commit_msg>Update message test to expect signals.<commit_after>import pytest
import gnsq


class MockConnection(object):
    def __init__(self, operations):
        self.operations = iter(operations)

    def __getattr__(self, name):
        def check_args(*args):
            expected_name, expected_args = self.operations.next()
            assert name == expected_name
            assert args == expected_args
        return check_args

    def assert_finished(self):
        with pytest.raises(StopIteration):
            self.operations.next()

    def connect_message(self, message):
        message.on_finish.connect(self.finish)
        message.on_requeue.connect(self.requeue)
        message.on_touch.connect(self.touch)


def test_basic():
    message = gnsq.Message(0, 42, '1234', 'sup')
    assert message.timestamp == 0
    assert message.attempts == 42
    assert message.id == '1234'
    assert message.body == 'sup'
    assert message.has_responded() is False


def test_finish():
    mock_conn = MockConnection([
        ('finish', ('1234',)),
    ])

    message = gnsq.Message(0, 42, '1234', 'sup')
    mock_conn.connect_message(message)
    assert message.has_responded() is False

    message.finish()
    assert message.has_responded() is True

    with pytest.raises(gnsq.errors.NSQException):
        message.finish()


def test_requeue():
    mock_conn = MockConnection([
        ('requeue', ('1234', 0)),
    ])

    message = gnsq.Message(0, 42, '1234', 'sup')
    mock_conn.connect_message(message)
    assert message.has_responded() is False

    message.requeue()
    assert message.has_responded() is True

    with pytest.raises(gnsq.errors.NSQException):
        message.requeue()

    mock_conn.assert_finished()


def test_requeue_timeout():
    mock_conn = MockConnection([
        ('requeue', ('1234', 1000)),
    ])

    message = gnsq.Message(0, 42, '1234', 'sup')
    mock_conn.connect_message(message)
    assert message.has_responded() is False

    message.requeue(1000)
    assert message.has_responded() is True

    with pytest.raises(gnsq.errors.NSQException):
        message.requeue(1000)

    mock_conn.assert_finished()


def test_touch():
    mock_conn = MockConnection([
        ('touch', ('1234',)),
        ('touch', ('1234',)),
        ('touch', ('1234',)),
        ('finish', ('1234',)),
    ])

    message = gnsq.Message(0, 42, '1234', 'sup')
    mock_conn.connect_message(message)
    assert message.has_responded() is False

    message.touch()
    message.touch()
    message.touch()
    assert message.has_responded() is False

    message.finish()
    assert message.has_responded() is True

    with pytest.raises(gnsq.errors.NSQException):
        message.touch()

    mock_conn.assert_finished()
{'fraction_non_alphanumeric': 0.13191323040844244, 'fraction_numerical': 0.0318546023060387, 'mean_word_length': 3.010971786833856, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '621870', 'n_tokens_mistral': 1736, 'n_tokens_neox': 1567, 'n_words': 343}