def test_registers_task_instance(self, worker: Worker):
        def func(x):
            return x

        worker.register("test", func)
        task = worker.get_registered_task("test")
        assert isinstance(task, Task)
def worker(conn: MagicMock, monkeypatch):

    mock_executor = MagicMock()

    monkeypatch.setattr("faktory.worker.Connection", conn)

    work = Worker()
    work._last_heartbeat = datetime.now()
    monkeypatch.setattr(work, "_executor", mock_executor)
    return work
    def test_can_get_registered_task(self, worker: Worker):
        # This can arguably be under this set of tests
        # or the `get_registered_task` tests,
        # but it makes sure that any registered task
        # can be retrieved by name.
        def func(x):
            return x

        worker.register("test", func)
        task = worker.get_registered_task("test")

        assert task.func == func
        assert task.name == "test"
        assert task.bind == False
def test_mocked_connection_correctly(conn, monkeypatch):
    """
    Quick test to make sure we're mocking out the Worker's
    connection correctly so that we're okay to assume
    the rest of the test suite is valid.
    """

    conn_mock = MagicMock(return_value=conn)

    monkeypatch.setattr("faktory.worker.Connection", conn_mock)
    worker = Worker()
    assert worker.faktory == conn
 def test_is_json_serializable(self, worker: Worker):
     worker_id = worker.get_worker_id()
     try:
         json.dumps(worker_id)
     except TypeError as exc:
         pytest.fail("Worker id: {} isn't JSON serializable".format(worker_id))
 def test_is_different(self, worker: Worker):
     worker_id = worker.get_worker_id()
     second_worker_id = worker.get_worker_id()
     assert worker_id != second_worker_id
    def test_can_register_with_and_without_binds(self, worker: Worker, bind: bool):
        def func(x):
            return x

        worker.register("test", func, bind=bind)
    def test_errors_without_callable_func(self, worker: Worker):

        with pytest.raises(ValueError):
            worker.register("test", "will_cause_error", True)