Package grassyknoll :: Package tests :: Package test_concurrent :: Module test_Wrappers
[hide private]

Source Code for Module grassyknoll.tests.test_concurrent.test_Wrappers

  1  from nose.tools import * 
  2  from grassyknoll.concurrent.Wrappers import * 
  3  from grassyknoll.concurrent.Message import * 
  4  from grassyknoll.concurrent.MailBox import * 
  5  from grassyknoll.concurrent.Fault import * 
  6   
  7  # XXX these tests should get broken up into classes 
  8   
  9   
10 -def test_functionMessenger():
11 box=MailBox() 12 13 @functionMessenger(send_none=False) 14 def doit(x): 15 if x > 0: return x + 3
16 17 m=doit(Message(FunctionCall(1), reply_box=box)) 18 assert isinstance(m, Message) 19 assert m.payload==4 20 del m 21 22 # no reply_box 23 m=doit(Message(FunctionCall(1))) 24 assert isinstance(m, Message) 25 assert m.payload==4 26 del m 27 28 # send_none is False 29 assert doit(Message(FunctionCall(0), reply_box=box)) is None 30 31 @functionMessenger(send_none=True) 32 def doit(x): 33 if x > 0: return x + 3 34 35 # send_none is True 36 m=doit(Message(FunctionCall(0), reply_box=box)) 37 38 assert isinstance(m, Message) 39 assert m.payload is None 40 del m 41 42 @functionMessenger() 43 def notfunny(x): 44 raise RuntimeError, "die, die, die!" 45 46 assert_raises(RuntimeError, notfunny, Message(FunctionCall("pants"))) 47
48 -def test_methodMessenger():
49 box=MailBox() 50 51 class Foo(object): 52 @methodMessenger(send_none=False) 53 def doit(self, x): 54 assert isinstance(self, Foo) 55 if x > 0: return x + 3
56 foo=Foo() 57 58 m=foo.doit(Message(FunctionCall(1), reply_box=box)) 59 assert isinstance(m, Message) 60 assert m.payload==4 61 del m 62 63 # no reply_box 64 m=foo.doit(Message(FunctionCall(1))) 65 assert isinstance(m, Message) 66 assert m.payload==4 67 del m 68 69 # send_none is False 70 assert foo.doit(Message(FunctionCall(0), reply_box=box)) is None 71 72 class Foo(object): 73 @methodMessenger(send_none=True) 74 def doit(self, x): 75 assert isinstance(self, Foo) 76 if x > 0: return x + 3 77 foo=Foo() 78 79 # send_none is True 80 m=foo.doit(Message(FunctionCall(0), reply_box=box)) 81 82 assert isinstance(m, Message) 83 assert m.payload is None 84 del m 85 86 class Foo(object): 87 @methodMessenger() 88 def notfunny(self, x): 89 assert isinstance(self, Foo) 90 raise RuntimeError, "die, die, die!" 91 foo=Foo() 92 93 assert_raises(RuntimeError, foo.notfunny, Message(FunctionCall("pants"))) 94
95 -def test_objectMessenger():
96 box=MailBox() 97 98 class Foo(object): 99 100 def add3(self, x): 101 return x + 3
102 103 def mult2(self, x): 104 return x * 2 105 106 def wonky(self, x): 107 if x > 0: return "wonky" 108 109 def notfunny(self): 110 raise RuntimeError, "die, die, die!" 111 112 doit=objectMessenger(Foo(), send_none=False) 113 114 m=doit(Message(MethodCall("add3", 1), reply_box=box)) 115 assert isinstance(m, Message) 116 assert m.payload==4 117 del m 118 119 m=doit(Message(MethodCall("mult2", 2), reply_box=box)) 120 assert isinstance(m, Message) 121 assert m.payload==4 122 del m 123 124 assert_raises(RuntimeError, doit, Message(MethodCall("notfunny"), reply_box=box)) 125 126 assert_raises(AttributeError, doit, Message(MethodCall("yousuck", 2), reply_box=box)) 127 128 # no reply_box 129 m=doit(Message(MethodCall('add3', 1))) 130 assert isinstance(m, Message) 131 assert m.payload==4 132 del m 133 134 # send_none is False 135 assert doit(Message(MethodCall("wonky", 0), reply_box=box)) is None 136 137 doit=objectMessenger(Foo(), send_none=True) 138 m=doit(Message(MethodCall("wonky", 0), reply_box=box)) 139 assert isinstance(m, Message) 140 assert m.payload is None 141 del m 142
143 -def test_errorMessenger():
144 box=MailBox() 145 146 @errorMessenger(RuntimeError, ValueError) 147 def doit(mesg): 148 if mesg.payload > 0: raise RuntimeError, "die, die, die!" 149 else: raise ValueError, "you sunk my battleship!"
150 151 m=doit(Message(1, reply_box=box)) 152 assert isinstance(m, Message) 153 assert isinstance(m.payload, Fault) 154 assert m.payload.type is RuntimeError 155 del m 156 157 m=doit(Message(-1, reply_box=box)) 158 assert isinstance(m, Message) 159 assert isinstance(m.payload, Fault) 160 assert m.payload.type is ValueError 161 del m 162 163 # no reply_box 164 m=doit(Message(1)) 165 assert isinstance(m, Message) 166 assert isinstance(m.payload, Fault) 167 assert m.payload.type is RuntimeError 168 del m 169