Exemple #1
0
def test_wrap():
    cb1 = Mock()
    cb2 = Mock()
    x = wrap(promise(cb1))
    x(1, y=2)
    cb1.assert_called_with(1, y=2)
    p2 = promise(cb2)
    x(p2)
    p2()
    cb1.assert_called_with(cb2())
Exemple #2
0
def test_wrap():
    cb1 = Mock()
    cb2 = Mock()
    x = wrap(promise(cb1))
    x(1, y=2)
    cb1.assert_called_with(1, y=2)
    p2 = promise(cb2)
    x(p2)
    p2()
    cb1.assert_called_with(cb2())
Exemple #3
0
    def test_example(self):

        _pending = deque()

        class Protocol:
            """难以理解"""
            def __init__(self):
                self.buffer = []

            def read(self, size, callback=None):
                callback = callback or promise()
                _pending.append((size, callback))
                return callback

            def read_header(self, callback=None):
                return self.read(4, callback)

            def read_body(self, header, callback=None):
                body_size, = unpack('>L', header)
                return self.read(body_size, callback)

            def prepare_body(self, value):
                self.buffer.append(value)

        proto = Protocol()
        # 读头,读body,准备数据
        proto.read_header().then(proto.read_body).then(wrap(
            proto.prepare_body))

        while _pending:
            size, callback = _pending.popleft()
            if size == 4:
                callback(pack('>L', 1231012302))
            else:
                callback('Hello world')

        assert proto.buffer
        assert proto.buffer[0] == 'Hello world'
Exemple #4
0
    def test_example(self):

        _pending = deque()

        class Protocol(object):

            def __init__(self):
                self.buffer = []

            def read(self, size, callback=None):
                callback = callback or promise()
                _pending.append((size, callback))
                return callback

            def read_header(self, callback=None):
                return self.read(4, callback)

            def read_body(self, header, callback=None):
                body_size, = unpack('>L', header)
                return self.read(body_size, callback)

            def prepare_body(self, value):
                self.buffer.append(value)

        proto = Protocol()
        proto.read_header().then(
            proto.read_body).then(wrap(proto.prepare_body))

        while _pending:
            size, callback = _pending.popleft()
            if size == 4:
                callback(pack('>L', 1231012302))
            else:
                callback('Hello world')

        assert proto.buffer
        assert proto.buffer[0] == 'Hello world'