|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Open Source Initiative OSI - The MIT License (MIT):Licensing |
| 3 | +# |
| 4 | +# The MIT License (MIT) |
| 5 | +# Copyright (c) 2013 DotCloud Inc ([email protected]) |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 8 | +# this software and associated documentation files (the "Software"), to deal in |
| 9 | +# the Software without restriction, including without limitation the rights to |
| 10 | +# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 11 | +# of the Software, and to permit persons to whom the Software is furnished to do |
| 12 | +# so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | + |
| 26 | +from nose.tools import assert_raises |
| 27 | +import gevent |
| 28 | + |
| 29 | +from zerorpc import zmq |
| 30 | +import zerorpc |
| 31 | +from testutils import teardown, random_ipc_endpoint |
| 32 | + |
| 33 | + |
| 34 | +def test_client_server_client_timeout_with_async(): |
| 35 | + endpoint = random_ipc_endpoint() |
| 36 | + |
| 37 | + class MySrv(zerorpc.Server): |
| 38 | + |
| 39 | + def lolita(self): |
| 40 | + return 42 |
| 41 | + |
| 42 | + def add(self, a, b): |
| 43 | + gevent.sleep(10) |
| 44 | + return a + b |
| 45 | + |
| 46 | + srv = MySrv() |
| 47 | + srv.bind(endpoint) |
| 48 | + gevent.spawn(srv.run) |
| 49 | + |
| 50 | + client = zerorpc.Client(timeout=2) |
| 51 | + client.connect(endpoint) |
| 52 | + |
| 53 | + async_result = client.add(1, 4, async=True) |
| 54 | + with assert_raises(zerorpc.TimeoutExpired): |
| 55 | + print async_result.get() |
| 56 | + client.close() |
| 57 | + srv.close() |
| 58 | + |
| 59 | + |
| 60 | +def test_client_server_with_async(): |
| 61 | + endpoint = random_ipc_endpoint() |
| 62 | + |
| 63 | + class MySrv(zerorpc.Server): |
| 64 | + |
| 65 | + def lolita(self): |
| 66 | + return 42 |
| 67 | + |
| 68 | + def add(self, a, b): |
| 69 | + return a + b |
| 70 | + |
| 71 | + srv = MySrv() |
| 72 | + srv.bind(endpoint) |
| 73 | + gevent.spawn(srv.run) |
| 74 | + |
| 75 | + client = zerorpc.Client() |
| 76 | + client.connect(endpoint) |
| 77 | + |
| 78 | + async_result = client.lolita(async=True) |
| 79 | + assert async_result.get() == 42 |
| 80 | + |
| 81 | + async_result = client.add(1, 4, async=True) |
| 82 | + assert async_result.get() == 5 |
0 commit comments