def test_queue_task_work(self):
        url, manager = yield from self.create_server()

        result_storage = []

        @manager.result_handler
        @asyncio.coroutine
        def store_result(result):
            result_storage.append(result)

        @manager.result_handler
        @asyncio.coroutine
        def bad_function(result):
            raise Exception()

        t = Task("test_task", [1, 2, 3], "pool", [1], {})

        r = yield from client.post("{}/task".format(url), data=json.dumps(t.for_json()), loop=self.loop)
        assert r.status == 200
        data = yield from r.json()
        assert data["result"] == "success"

        r = yield from client.options("{}/".format(url), loop=self.loop)
        assert r.status == 200
        data = yield from r.json()
        assert data["tasks"]["pending"] == 1

        r = yield from client.patch("{}/task/pending".format(url), params={"pool": "pool"}, loop=self.loop)
        assert r.status == 200
        data = yield from r.json()
        assert data["id"] == str(t.id)

        r = yield from client.options("{}/".format(url), loop=self.loop)
        assert r.status == 200
        data = yield from r.json()
        assert data["tasks"]["pending"] == 0
        assert data["tasks"]["active"] == 1
        assert data["locks"] == 3

        r = yield from client.patch(
            "{}/task/{}".format(url, str(t.id)),
            data=json.dumps({"stdout": "", "stderr": "", "result": "", "status": "success"}),
            loop=self.loop,
        )
        assert r.status == 200
        yield from r.release()

        assert len(result_storage) == 1

        r = yield from client.options("{}/".format(url), loop=self.loop)
        assert r.status == 200
        data = yield from r.json()
        assert data["tasks"]["pending"] == 0
        assert data["tasks"]["active"] == 0
        assert data["locks"] == 0
    def test_queue_pool_required(self):
        url, _ = yield from self.create_server()
        t = Task("test_task", [1, 2, 3], "pool", [1], {})
        r = yield from client.post("{}/task".format(url), data=json.dumps(t.for_json()), loop=self.loop)
        data = yield from r.json()

        r = yield from client.patch("{}/task/pending".format(url), loop=self.loop)
        assert r.status == 200
        data = yield from r.json()
        assert data is None
    def test_complete_unknown_task(self):
        url, _ = yield from self.create_server()
        t = Task("test_task", [1, 2, 3], "pool", [1], {})

        r = yield from client.patch(
            "{}/task/{}".format(url, str(t.id)),
            data=json.dumps({"stdout": "", "stderr": "", "result": "", "status": "success"}),
            loop=self.loop,
        )
        assert r.status == 404
        data = yield from r.json()
        assert data["error"] == "Unknown task"