Example #1
0
def test_3_2_6_3_when_rejected():
    """
    Testing return of pending promises to make
    sure they are properly chained.

    This covers the case where the root promise
    is rejected after the chaining is defined.
    """

    p1 = Promise()
    pending = Promise()
    pr = p1.then(None, lambda r: pending)
    assert pending.isPending()
    assert pr.isPending()
    p1.reject("Error")
    pending.fulfill(10)
    assert pending.isFulfilled()
    assert_equals(10, pending.value)
    assert pr.isFulfilled()
    assert_equals(10, pr.value)

    p2 = Promise()
    bad = Promise()
    pr = p2.then(None, lambda r: bad)
    assert bad.isPending()
    assert pr.isPending()
    p2.reject("Error")
    bad.reject("Assertion")
    assert bad.isRejected()
    assert_equals("Assertion", bad.reason)
    assert pr.isRejected()
    assert_equals("Assertion", pr.reason)
Example #2
0
    def handle_request(self, request):
        request_indication = map_request_to_request_indication(request)
        #TODO: "check authorization of requestingEntity based on default access rights"

        p = Promise()

        if request_indication.method != "retrieve":
            return p.reject(SCLMethodNotAllowed(request_indication.method))

        path = urlparse(request_indication.path).path

        channel_id = path[len(PREFIX) + 1:]

        try:
            notification, content_type = self.queues[channel_id].popleft()
        except KeyError:
            self.logger.info("Polling on unknown channel: %s", channel_id)
            return p.reject(SCLNotFound(path))
        except IndexError:
            #TODO: handle timeouts
            self.waiting[channel_id].append(p)
        else:
            response_confirmation = RetrieveResponseConfirmation(
                notification, content_type)
            response = map_response_confirmation_to_response(
                request, response_confirmation)
            p.fulfill(response)

        self.logger.debug("Returning channel promise: %s", p.isPending())
        return p
def test_list_promise_when():
    p1 = Promise()
    p2 = Promise()
    pl = listPromise(p1, p2)
    assert p1.isPending()
    assert p2.isPending()
    assert pl.isPending()
    p1.fulfill(5)
    assert p1.isFulfilled()
    assert p2.isPending()
    assert pl.isPending()
    p2.fulfill(10)
    assert p1.isFulfilled()
    assert p2.isFulfilled()
    assert pl.isFulfilled()
    assert_equals(5, p1.value)
    assert_equals(10, p2.value)
    assert_equals(5, pl.value[0])
    assert_equals(10, pl.value[1])
def test_dict_promise_if():
    p1 = Promise()
    p2 = Promise()
    d = {"a": p1, "b": p2}
    pd = dictPromise(d)
    assert p1.isPending()
    assert p2.isPending()
    assert pd.isPending()
    p1.fulfill(5)
    assert p1.isFulfilled()
    assert p2.isPending()
    assert pd.isPending()
    p2.fulfill(10)
    assert p1.isFulfilled()
    assert p2.isFulfilled()
    assert pd.isFulfilled()
    assert_equals(5, p1.value)
    assert_equals(10, p2.value)
    assert_equals(5, pd.value["a"])
    assert_equals(10, pd.value["b"])