Beispiel #1
0
def make_addressbook():
    phone1 = addressbook.PhoneNumber()
    phone1.type = addressbook.PhoneType.MOBILE
    phone1.number = b'555-1212'
    phone2 = addressbook.PhoneNumber()
    phone2.type = addressbook.PhoneType.HOME
    phone2.number = b'555-1234'
    person = addressbook.Person()
    person.name = b"Alice"
    person.phones = [phone1, phone2]
    person.created_at = 1400000000

    ab = addressbook.AddressBook()
    ab.people = {person.name: person}
    return ab
Beispiel #2
0
def rpc_client():
    # test void request
    with client() as c:
        assert c.ping() is None

    phone1 = addressbook.PhoneNumber()
    phone1.type = addressbook.PhoneType.MOBILE
    phone1.number = '555-1212'
    phone2 = addressbook.PhoneNumber()
    phone2.type = addressbook.PhoneType.HOME
    phone2.number = '555-1234'
    person = addressbook.Person()
    person.name = "Alice"
    person.phones = [phone1, phone2]
    person.created_at = int(time.time())

    # test put struct
    with client() as c:
        assert c.add(person)

    # test get struct
    with client() as c:
        alice = c.get("Alice")
        assert person == alice

    # test get complex struct
    with client() as c:
        # test get list
        # assert isinstance(c.get_phonenumbers("Alice"), list)

        # test get empty list
        assert len(c.get_phonenumbers("Alice", 0)) == 0
        assert len(c.get_phonenumbers("Alice", 1000)) == 1000

        # assert isinstance(c.get_phones("Alice"), dict)
        # assert isinstance(c.book(), addressbook.AddressBook)

    # test exception
    with client() as c:
        try:
            name = "Bob"
            print("Try to remove non-exists name...")
            c.remove(name)
        except addressbook.PersonNotExistsError as e:
            print("remove({}) -> {}".format(name, e))
Beispiel #3
0
def test_rpc():
    p = multiprocessing.Process(target=serve)
    p.start()
    time.sleep(0.1)

    # test void request
    with client() as c:
        assert c.ping() is None

    phone1 = addressbook.PhoneNumber()
    phone1.type = addressbook.PhoneType.MOBILE
    phone1.number = '555-1212'
    phone2 = addressbook.PhoneNumber()
    phone2.type = addressbook.PhoneType.HOME
    phone2.number = '555-1234'
    person = addressbook.Person()
    person.name = "Alice"
    person.phones = [phone1, phone2]
    person.created_at = int(time.time())

    # test put struct
    with client() as c:
        assert c.add(person)

    # test get struct
    with client() as c:
        alice = c.get("Alice")
        assert person == alice

    # test get complex struct
    with client() as c:
        assert isinstance(c.get_phones("Alice"), dict)
        assert isinstance(c.book(), addressbook.AddressBook)

    # test exception
    with client() as c:
        try:
            name = "Bob"
            print("Try to remove non-exists name...")
            c.remove(name)
        except addressbook.PersonNotExistsError as e:
            print("remove({}) -> {}".format(name, e))

    p.terminate()
Beispiel #4
0
def rpc_client():
    # test void and normal request
    with client() as c:
        assert c.ping() is None
        assert c.hello("world") == "hello world"

    # test big request and return args
    with client() as c:
        big_str = "world" * 100000
        assert c.hello(big_str) == "hello " + big_str

    phone1 = addressbook.PhoneNumber()
    phone1.type = addressbook.PhoneType.MOBILE
    phone1.number = '555-1212'
    phone2 = addressbook.PhoneNumber()
    phone2.type = addressbook.PhoneType.HOME
    phone2.number = '555-1234'

    # empty struct
    phone3 = addressbook.PhoneNumber()

    person = addressbook.Person()
    person.name = "Alice"
    person.phones = [phone1, phone2, phone3]
    person.created_at = int(time.time())

    # test put struct
    with client() as c:
        assert c.add(person)

    # test get struct
    with client() as c:
        alice = c.get("Alice")
        assert person == alice

    # test get complex struct
    with client() as c:
        # test get list
        # assert isinstance(c.get_phonenumbers("Alice"), list)

        # test get empty list
        assert len(c.get_phonenumbers("Alice", 0)) == 0
        assert len(c.get_phonenumbers("Alice", 1000)) == 1000

        # assert isinstance(c.get_phones("Alice"), dict)
        # assert isinstance(c.book(), addressbook.AddressBook)

    # test exception
    with client() as c:
        try:
            name = "Bob"
            c.remove(name)

            # should not be executed
            assert False

        except Exception as e:
            assert isinstance(e, addressbook.PersonNotExistsError)

    # test client timeout
    with client(timeout=500) as c:
        try:
            c.sleep(1000)

            # should not be executed
            assert False

        except Exception as e:
            assert isinstance(e, socket.timeout)