Example #1
0
def test_unserialisable_headers(rabbit_manager, rabbit_config, mock_container):

    vhost = rabbit_config["vhost"]

    container = mock_container
    container.service_name = "service"
    container.config = rabbit_config
    container.spawn_managed_thread = eventlet.spawn

    ctx_data = {"language": "en", "customheader": None}
    service = Mock()
    worker_ctx = WorkerContext(container, service, DummyProvider("method"), data=ctx_data)

    publisher = Publisher(exchange=foobar_ex, queue=foobar_queue).bind(container, "publish")

    publisher.setup()
    publisher.start()

    service.publish = publisher.get_dependency(worker_ctx)
    service.publish("msg")
    messages = rabbit_manager.get_messages(vhost, foobar_queue.name)

    assert messages[0]["properties"]["headers"] == {
        "nameko.language": "en",
        "nameko.call_id_stack": ["service.method.0"],
        # no `customheader`
    }
Example #2
0
def test_publish_to_exchange(empty_config, maybe_declare, patch_publisher):
    container = Mock(spec=ServiceContainer)
    container.service_name = "srcservice"
    container.config = empty_config

    service = Mock()
    worker_ctx = WorkerContext(container, service, "publish")

    publisher = PublishProvider(exchange=foobar_ex)
    publisher.bind("publish", container)

    producer = Mock()
    connection = Mock()

    get_connection, get_producer = patch_publisher(publisher)

    get_connection.return_value = as_context_manager(connection)
    get_producer.return_value = as_context_manager(producer)

    # test declarations
    publisher.prepare()
    maybe_declare.assert_called_once_with(foobar_ex, connection)

    # test publish
    msg = "msg"
    publisher.inject(worker_ctx)
    service.publish(msg)
    headers = {"nameko.call_id_stack": ["srcservice.publish.0"]}
    producer.publish.assert_called_once_with(msg, headers=headers, exchange=foobar_ex)
Example #3
0
def test_publish_to_exchange(maybe_declare, patch_publisher, mock_container):
    container = mock_container
    container.service_name = "srcservice"

    service = Mock()
    worker_ctx = WorkerContext(container, service, DummyProvider("publish"))

    publisher = Publisher(exchange=foobar_ex).bind(container, "publish")

    producer = Mock()
    connection = Mock()

    get_connection, get_producer = patch_publisher(publisher)

    get_connection.return_value = as_context_manager(connection)
    get_producer.return_value = as_context_manager(producer)

    # test declarations
    publisher.setup()
    maybe_declare.assert_called_once_with(foobar_ex, connection)

    # test publish
    msg = "msg"
    service.publish = publisher.get_dependency(worker_ctx)
    service.publish(msg, publish_kwarg="value")
    headers = {"nameko.call_id_stack": ["srcservice.publish.0"]}
    producer.publish.assert_called_once_with(
        msg,
        headers=headers,
        exchange=foobar_ex,
        retry=True,
        serializer=container.serializer,
        retry_policy=DEFAULT_RETRY_POLICY,
        publish_kwarg="value",
    )
Example #4
0
def test_publish_to_queue(empty_config, maybe_declare, patch_publisher):
    container = Mock(spec=ServiceContainer)
    container.service_name = "srcservice"
    container.config = empty_config

    ctx_data = {"language": "en"}
    service = Mock()
    worker_ctx = WorkerContext(container, service, DummyProvider("publish"), data=ctx_data)

    publisher = PublishProvider(queue=foobar_queue)
    publisher.bind("publish", container)

    producer = Mock()
    connection = Mock()

    get_connection, get_producer = patch_publisher(publisher)

    get_connection.return_value = as_context_manager(connection)
    get_producer.return_value = as_context_manager(producer)

    # test declarations
    publisher.prepare()
    maybe_declare.assert_called_once_with(foobar_queue, connection)

    # test publish
    msg = "msg"
    headers = {"nameko.language": "en", "nameko.call_id_stack": ["srcservice.publish.0"]}
    publisher.inject(worker_ctx)
    service.publish(msg, publish_kwarg="value")
    producer.publish.assert_called_once_with(
        msg, headers=headers, exchange=foobar_ex, retry=True, retry_policy=DEFAULT_RETRY_POLICY, publish_kwarg="value"
    )
Example #5
0
def test_unserialisable_headers(rabbit_manager, rabbit_config):

    vhost = rabbit_config["vhost"]

    container = Mock(spec=ServiceContainer)
    container.service_name = "service"
    container.config = rabbit_config
    container.spawn_managed_thread = eventlet.spawn

    ctx_data = {"language": "en", "customheader": None}
    service = Mock()
    worker_ctx = CustomWorkerContext(container, service, "method", data=ctx_data)

    publisher = PublishProvider(exchange=foobar_ex, queue=foobar_queue)
    publisher.bind("publish", container)

    publisher.prepare()
    publisher.start()

    publisher.inject(worker_ctx)
    service.publish("msg")
    messages = rabbit_manager.get_messages(vhost, foobar_queue.name)

    assert messages[0]["properties"]["headers"] == {
        "nameko.language": "en",
        "nameko.call_id_stack": ["service.method.0"],
        # no `customheader`
    }
Example #6
0
def test_publish_custom_headers(empty_config, maybe_declare, patch_publisher):

    container = Mock(spec=ServiceContainer)
    container.service_name = "srcservice"
    container.config = empty_config

    ctx_data = {"language": "en", "customheader": "customvalue"}
    service = Mock()
    worker_ctx = CustomWorkerContext(container, service, "method", data=ctx_data)

    publisher = PublishProvider(queue=foobar_queue)
    publisher.bind("publish", container)

    producer = Mock()
    connection = Mock()

    get_connection, get_producer = patch_publisher(publisher)

    get_connection.return_value = as_context_manager(connection)
    get_producer.return_value = as_context_manager(producer)

    # test declarations
    publisher.prepare()
    maybe_declare.assert_called_once_with(foobar_queue, connection)

    # test publish
    msg = "msg"
    headers = {
        "nameko.language": "en",
        "nameko.customheader": "customvalue",
        "nameko.call_id_stack": ["srcservice.method.0"],
    }
    publisher.inject(worker_ctx)
    service.publish(msg)
    producer.publish.assert_called_once_with(msg, headers=headers, exchange=foobar_ex)
Example #7
0
    def test_create_move_publishes_to_redis(self):
        """
        Tests that we are publishing to redis when we create moves
        """
        request = Mock(name="request")
        request.user = self.player1

        redis = Mock(name="redis")
        redis.publish = Mock()

        self.game.board = pickle.dumps(["X", "", "", "", "X", "", "", "", ""])
        self.game.save()

        with patch("core.views.Redis") as mock_redis:
            mock_redis.return_value = redis

            move = 8
            player = "X"
            response = self.client.post("/create_move/%d/" % self.game.id, {"move": move})

            redis.publish.assert_called_with(self.player2.id, ["game_over", self.game.id, player])

            _pop_last_call(redis.publish)

            redis.publish.assert_called_with(self.player1.id, ["game_over", self.game.id, player])

            _pop_last_call(redis.publish)

            redis.publish.assert_called_with(self.player2.id, ["opponent_moved", self.game.id, player, move])
Example #8
0
def default_args():
    args = Mock()
    args.conf_file = ".lamvery.yml"
    args.dry_run = True
    args.publish = True
    args.alias = None
    args.version = None
    return args
Example #9
0
def default_args():
    args = Mock()
    args.conf_file = ".lamvery.yml"
    args.dry_run = True
    args.publish = True
    args.no_libs = False
    args.single_file = False
    return args
Example #10
0
def test_publish_custom_headers(mock_container, maybe_declare, patch_publisher):

    container = mock_container
    container.service_name = "srcservice"

    ctx_data = {"language": "en", "customheader": "customvalue"}
    service = Mock()
    worker_ctx = WorkerContext(container, service, DummyProvider("method"), data=ctx_data)

    publisher = Publisher(queue=foobar_queue).bind(container, "publish")

    producer = Mock()
    connection = Mock()

    get_connection, get_producer = patch_publisher(publisher)

    get_connection.return_value = as_context_manager(connection)
    get_producer.return_value = as_context_manager(producer)

    # test declarations
    publisher.setup()
    maybe_declare.assert_called_once_with(foobar_queue, connection)

    # test publish
    msg = "msg"
    headers = {
        "nameko.language": "en",
        "nameko.customheader": "customvalue",
        "nameko.call_id_stack": ["srcservice.method.0"],
    }
    service.publish = publisher.get_dependency(worker_ctx)
    service.publish(msg, publish_kwarg="value")
    producer.publish.assert_called_once_with(
        msg,
        headers=headers,
        exchange=foobar_ex,
        retry=True,
        serializer=container.serializer,
        retry_policy=DEFAULT_RETRY_POLICY,
        publish_kwarg="value",
    )
Example #11
0
def test_publish_to_rabbit(rabbit_manager, rabbit_config):

    vhost = rabbit_config["vhost"]

    container = Mock(spec=ServiceContainer)
    container.service_name = "service"
    container.config = rabbit_config
    container.spawn_managed_thread = eventlet.spawn

    ctx_data = {"language": "en", "customheader": "customvalue"}
    service = Mock()
    worker_ctx = CustomWorkerContext(container, service, DummyProvider("method"), data=ctx_data)

    publisher = PublishProvider(exchange=foobar_ex, queue=foobar_queue)
    publisher.bind("publish", container)

    # test queue, exchange and binding created in rabbit
    publisher.prepare()
    publisher.start()

    exchanges = rabbit_manager.get_exchanges(vhost)
    queues = rabbit_manager.get_queues(vhost)
    bindings = rabbit_manager.get_queue_bindings(vhost, foobar_queue.name)

    assert "foobar_ex" in [exchange["name"] for exchange in exchanges]
    assert "foobar_queue" in [queue["name"] for queue in queues]
    assert "foobar_ex" in [binding["source"] for binding in bindings]

    # test message published to queue
    publisher.inject(worker_ctx)
    service.publish("msg")
    messages = rabbit_manager.get_messages(vhost, foobar_queue.name)
    assert ["msg"] == [msg["payload"] for msg in messages]

    # test message headers
    assert messages[0]["properties"]["headers"] == {
        "nameko.language": "en",
        "nameko.customheader": "customvalue",
        "nameko.call_id_stack": ["service.method.0"],
    }
Example #12
0
def test_publish_to_rabbit(rabbit_manager, rabbit_config, mock_container):

    vhost = rabbit_config["vhost"]

    container = mock_container
    container.service_name = "service"
    container.config = rabbit_config

    ctx_data = {"language": "en", "customheader": "customvalue"}
    service = Mock()
    worker_ctx = WorkerContext(container, service, DummyProvider("method"), data=ctx_data)

    publisher = Publisher(exchange=foobar_ex, queue=foobar_queue).bind(container, "publish")

    # test queue, exchange and binding created in rabbit
    publisher.setup()
    publisher.start()

    exchanges = rabbit_manager.get_exchanges(vhost)
    queues = rabbit_manager.get_queues(vhost)
    bindings = rabbit_manager.get_queue_bindings(vhost, foobar_queue.name)

    assert "foobar_ex" in [exchange["name"] for exchange in exchanges]
    assert "foobar_queue" in [queue["name"] for queue in queues]
    assert "foobar_ex" in [binding["source"] for binding in bindings]

    # test message published to queue
    service.publish = publisher.get_dependency(worker_ctx)
    service.publish("msg")
    messages = rabbit_manager.get_messages(vhost, foobar_queue.name)
    assert ['"msg"'] == [msg["payload"] for msg in messages]

    # test message headers
    assert messages[0]["properties"]["headers"] == {
        "nameko.language": "en",
        "nameko.customheader": "customvalue",
        "nameko.call_id_stack": ["service.method.0"],
    }
Example #13
0
    def test_winning_move_publishes_to_redis(self):
        """
        Tests that we are publishing to redis when we create moves
        """
        request = Mock(name="request")
        request.user = self.player1

        redis = Mock(name="redis")
        redis.publish = Mock()

        with patch("core.views.Redis") as mock_redis:
            mock_redis.return_value = redis

            move = 0
            player = "X"
            response = self.client.post("/create_move/%d/" % self.game.id, {"move": move})

            redis.publish.assert_called_once_with(self.player2.id, ["opponent_moved", self.game.id, player, move])
Example #14
0
    def test_create_move_makes_move(self):
        """
        Tests that we are creating moves in the db when we call create_move
        """
        request = Mock(name="request")
        request.user = self.player1

        redis = Mock(name="redis")
        redis.publish = Mock()

        with patch("core.views.Redis") as mock_redis:
            mock_redis.return_value = redis

            move = 0
            player = "X"
            response = self.client.post("/create_move/%d/" % self.game.id, {"move": move})

            game = Game.objects.get(pk=self.game.id)
            board = game.get_board()
            self.assertEqual(board[0], player)