|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Open Source Initiative OSI - The MIT License (MIT):Licensing |
| 3 | +# |
| 4 | +# The MIT License (MIT) |
| 5 | +# Copyright (c) 2012 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 | +import gevent |
| 27 | +from nose.tools import assert_raises |
| 28 | + |
| 29 | +import zerorpc |
| 30 | +from testutils import teardown, random_ipc_endpoint |
| 31 | + |
| 32 | +def test_simple_context(): |
| 33 | + endpoint = random_ipc_endpoint() |
| 34 | + |
| 35 | + class LittleContext: |
| 36 | + def echo(self, msg): |
| 37 | + return msg |
| 38 | + |
| 39 | + class MySrv(zerorpc.Server): |
| 40 | + |
| 41 | + @zerorpc.context |
| 42 | + def open_context(self): |
| 43 | + yield LittleContext() |
| 44 | + |
| 45 | + srv = MySrv() |
| 46 | + srv.bind(endpoint) |
| 47 | + gevent.spawn(srv.run) |
| 48 | + |
| 49 | + client = zerorpc.Client() |
| 50 | + client.connect(endpoint) |
| 51 | + |
| 52 | + context = client.open_context() |
| 53 | + assert context.echo(42) == 42 |
| 54 | + |
| 55 | +def test_recursive_context(): |
| 56 | + endpoint = random_ipc_endpoint() |
| 57 | + |
| 58 | + class MySrv: |
| 59 | + def __init__(self, id_ = 0): |
| 60 | + self._id = id_ |
| 61 | + |
| 62 | + @zerorpc.context |
| 63 | + def open_context(self): |
| 64 | + yield MySrv(self._id + 1) |
| 65 | + |
| 66 | + def get_id(self): |
| 67 | + return self._id |
| 68 | + |
| 69 | + srv = zerorpc.Server(MySrv()) |
| 70 | + srv.bind(endpoint) |
| 71 | + gevent.spawn(srv.run) |
| 72 | + |
| 73 | + client = zerorpc.Client() |
| 74 | + client.connect(endpoint) |
| 75 | + |
| 76 | + assert client.get_id() == 0 |
| 77 | + context = client.open_context() |
| 78 | + assert context.get_id() == 1 |
| 79 | + for x in xrange(2, 32): |
| 80 | + context = context.open_context() |
| 81 | + print context.get_id(), x |
| 82 | + assert context.get_id() == x |
| 83 | + |
| 84 | +def test_lost_server_then_request(): |
| 85 | + endpoint = random_ipc_endpoint() |
| 86 | + |
| 87 | + class LittleContext: |
| 88 | + def something(self): |
| 89 | + pass |
| 90 | + |
| 91 | + class MySrv: |
| 92 | + @zerorpc.context |
| 93 | + def open_context(self): |
| 94 | + yield LittleContext() |
| 95 | + |
| 96 | + srv = zerorpc.Server(MySrv(), heartbeat=1) |
| 97 | + srv.bind(endpoint) |
| 98 | + gevent.spawn(srv.run) |
| 99 | + |
| 100 | + client = zerorpc.Client(heartbeat=1) |
| 101 | + client.connect(endpoint) |
| 102 | + |
| 103 | + print 'open context' |
| 104 | + context = client.open_context() |
| 105 | + print 'close server' |
| 106 | + srv.close() |
| 107 | + print 'client call' |
| 108 | + with assert_raises(zerorpc.LostRemote): |
| 109 | + context.something() |
| 110 | + print 'done' |
| 111 | + |
| 112 | +def test_lost_server_then_request_recursive(): |
| 113 | + endpoint = random_ipc_endpoint() |
| 114 | + |
| 115 | + class MySrv: |
| 116 | + def __init__(self, id_ = 0): |
| 117 | + self._id = id_ |
| 118 | + |
| 119 | + @zerorpc.context |
| 120 | + def open_context(self): |
| 121 | + yield MySrv(self._id + 1) |
| 122 | + |
| 123 | + def get_id(self): |
| 124 | + return self._id |
| 125 | + |
| 126 | + srv = zerorpc.Server(MySrv(), heartbeat=1) |
| 127 | + srv.bind(endpoint) |
| 128 | + gevent.spawn(srv.run) |
| 129 | + |
| 130 | + client = zerorpc.Client(heartbeat=1) |
| 131 | + client.connect(endpoint) |
| 132 | + |
| 133 | + print 'open context' |
| 134 | + context = client.open_context() |
| 135 | + print 'recurse 10 times' |
| 136 | + for x in xrange(9): |
| 137 | + context = context.open_context() |
| 138 | + assert context.get_id() == 10 |
| 139 | + print 'close server' |
| 140 | + srv.close() |
| 141 | + print 'client call' |
| 142 | + with assert_raises(zerorpc.LostRemote): |
| 143 | + context.get_id() |
| 144 | + print 'done' |
| 145 | + |
| 146 | +def test_lost_server_while_idle(): |
| 147 | + pass |
| 148 | + |
| 149 | +def test_lost_client_then_request(): |
| 150 | + pass |
| 151 | + |
| 152 | +def test_lost_client_while_idle(): |
| 153 | + pass |
0 commit comments