Permalink
Browse files

Get rid of obsolete code

  • Loading branch information...
1 parent cec0cb9 commit 8e43b64ee00af1ced726a183ea7a5b0daff3acf5 Alvaro committed Nov 16, 2012
Showing with 64 additions and 261 deletions.
  1. +0 −98 evy/coros.py
  2. +64 −0 evy/event.py
  3. +0 −163 tests/test_coros.py
View
98 evy/coros.py
@@ -1,98 +0,0 @@
-#
-# Evy - a concurrent networking library for Python
-#
-# Unless otherwise noted, the files in Evy are under the following MIT license:
-#
-# Copyright (c) 2012, Alvaro Saurin
-# Copyright (c) 2008-2010, Eventlet Contributors (see AUTHORS)
-# Copyright (c) 2007-2010, Linden Research, Inc.
-# Copyright (c) 2005-2006, Bob Ippolito
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-#
-
-
-
-from evy import event as _event
-
-class NOT_USED:
- def __repr__ (self):
- return 'NOT_USED'
-
-NOT_USED = NOT_USED()
-
-
-class metaphore(object):
- """This is sort of an inverse semaphore: a counter that starts at 0 and
- waits only if nonzero. It's used to implement a "wait for all" scenario.
-
- >>> from evy import api, coros
- >>> count = coros.metaphore()
- >>> count.wait()
- >>> def decrementer(count, id):
- ... print "%s decrementing" % id
- ... count.dec()
- ...
- >>> _ = evy.spawn(decrementer, count, 'A')
- >>> _ = evy.spawn(decrementer, count, 'B')
- >>> count.inc(2)
- >>> count.wait()
- A decrementing
- B decrementing
- """
-
- def __init__ (self):
- self.counter = 0
- self.event = _event.Event()
- # send() right away, else we'd wait on the default 0 count!
- self.event.send()
-
- def inc (self, by = 1):
- """Increment our counter. If this transitions the counter from zero to
- nonzero, make any subsequent :meth:`wait` call wait.
- """
- assert by > 0
- self.counter += by
- if self.counter == by:
- # If we just incremented self.counter by 'by', and the new count
- # equals 'by', then the old value of self.counter was 0.
- # Transitioning from 0 to a nonzero value means wait() must
- # actually wait.
- self.event.reset()
-
- def dec (self, by = 1):
- """Decrement our counter. If this transitions the counter from nonzero
- to zero, a current or subsequent wait() call need no longer wait.
- """
- assert by > 0
- self.counter -= by
- if self.counter <= 0:
- # Don't leave self.counter < 0, that will screw things up in
- # future calls.
- self.counter = 0
- # Transitioning from nonzero to 0 means wait() need no longer wait.
- self.event.send()
-
- def wait (self):
- """Suspend the caller only if our count is nonzero. In that case,
- resume the caller once the count decrements to zero again.
- """
- self.event.wait()
-
-
View
64 evy/event.py
@@ -240,3 +240,67 @@ def send_exception (self, *args):
"""
# the arguments and the same as for greenlet.throw
return self.send(None, args)
+
+
+
+class metaphore(object):
+ """
+ This is sort of an inverse semaphore: a counter that starts at 0 and
+ waits only if nonzero. It's used to implement a "wait for all" scenario.
+
+ >>> from evy import event
+ >>> count = event.metaphore()
+ >>> count.wait()
+ >>> def decrementer(count, id):
+ ... print "%s decrementing" % id
+ ... count.dec()
+ ...
+ >>> _ = evy.spawn(decrementer, count, 'A')
+ >>> _ = evy.spawn(decrementer, count, 'B')
+ >>> count.inc(2)
+ >>> count.wait()
+ A decrementing
+ B decrementing
+ """
+
+ def __init__ (self):
+ self.counter = 0
+ self.event = Event()
+ # send() right away, else we'd wait on the default 0 count!
+ self.event.send()
+
+ def inc (self, by = 1):
+ """
+ Increment our counter. If this transitions the counter from zero to
+ nonzero, make any subsequent :meth:`wait` call wait.
+ """
+ assert by > 0
+ self.counter += by
+ if self.counter == by:
+ # If we just incremented self.counter by 'by', and the new count
+ # equals 'by', then the old value of self.counter was 0.
+ # Transitioning from 0 to a nonzero value means wait() must
+ # actually wait.
+ self.event.reset()
+
+ def dec (self, by = 1):
+ """
+ Decrement our counter. If this transitions the counter from nonzero
+ to zero, a current or subsequent wait() call need no longer wait.
+ """
+ assert by > 0
+ self.counter -= by
+ if self.counter <= 0:
+ # Don't leave self.counter < 0, that will screw things up in
+ # future calls.
+ self.counter = 0
+ # Transitioning from nonzero to 0 means wait() need no longer wait.
+ self.event.send()
+
+ def wait (self):
+ """
+ Suspend the caller only if our count is nonzero. In that case,
+ resume the caller once the count decrements to zero again.
+ """
+ self.event.wait()
+
View
163 tests/test_coros.py
@@ -1,163 +0,0 @@
-#
-# Evy - a concurrent networking library for Python
-#
-# Unless otherwise noted, the files in Evy are under the following MIT license:
-#
-# Copyright (c) 2012, Alvaro Saurin
-# Copyright (c) 2008-2010, Eventlet Contributors (see AUTHORS)
-# Copyright (c) 2007-2010, Linden Research, Inc.
-# Copyright (c) 2005-2006, Bob Ippolito
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-#
-
-
-from unittest import main
-from tests import LimitedTestCase, silence_warnings
-import evy
-from evy import coros
-from evy import event
-from evy import greenthread
-
-class IncrActor(coros.Actor):
- def received (self, evt):
- self.value = getattr(self, 'value', 0) + 1
- if evt: evt.send()
-
-
-class TestActor(LimitedTestCase):
- mode = 'static'
-
- @silence_warnings
- def setUp (self):
- super(TestActor, self).setUp()
- self.actor = IncrActor()
-
- def tearDown (self):
- super(TestActor, self).tearDown()
- greenthread.kill(self.actor._killer)
-
- def test_cast (self):
- evt = event.Event()
- self.actor.cast(evt)
- evt.wait()
- evt.reset()
- self.assertEqual(self.actor.value, 1)
- self.actor.cast(evt)
- evt.wait()
- self.assertEqual(self.actor.value, 2)
-
- def test_cast_multi_1 (self):
- # make sure that both messages make it in there
- evt = event.Event()
- evt1 = event.Event()
- self.actor.cast(evt)
- self.actor.cast(evt1)
- evt.wait()
- evt1.wait()
- self.assertEqual(self.actor.value, 2)
-
- def test_cast_multi_2 (self):
- # the actor goes through a slightly different code path if it
- # is forced to enter its event loop prior to any cast()s
- evy.sleep(0)
- self.test_cast_multi_1()
-
- def test_sleeping_during_received (self):
- # ensure that even if the received method cooperatively
- # yields, eventually all messages are delivered
- msgs = []
- waiters = []
-
- def received ( (message, evt) ):
- evy.sleep(0)
- msgs.append(message)
- evt.send()
-
- self.actor.received = received
-
- waiters.append(event.Event())
- self.actor.cast((1, waiters[-1]))
- evy.sleep(0)
- waiters.append(event.Event())
- self.actor.cast((2, waiters[-1]))
- waiters.append(event.Event())
- self.actor.cast((3, waiters[-1]))
- evy.sleep(0)
- waiters.append(event.Event())
- self.actor.cast((4, waiters[-1]))
- waiters.append(event.Event())
- self.actor.cast((5, waiters[-1]))
- for evt in waiters:
- evt.wait()
- self.assertEqual(msgs, [1, 2, 3, 4, 5])
-
- def test_raising_received (self):
- msgs = []
-
- def received ( (message, evt) ):
- evt.send()
- if message == 'fail':
- raise RuntimeError()
- else:
- msgs.append(message)
-
- self.actor.received = received
-
- evt = event.Event()
- self.actor.cast(('fail', evt))
- evt.wait()
- evt.reset()
- self.actor.cast(('should_appear', evt))
- evt.wait()
- self.assertEqual(['should_appear'], msgs)
-
- @silence_warnings
- def test_multiple (self):
- self.actor = IncrActor(concurrency = 2)
- total = [0]
-
- def received ( (func, ev, value) ):
- func()
- total[0] += value
- ev.send()
-
- self.actor.received = received
-
- def onemoment ():
- evy.sleep(0.1)
-
- evt = event.Event()
- evt1 = event.Event()
-
- self.actor.cast((onemoment, evt, 1))
- self.actor.cast((lambda: None, evt1, 2))
-
- evt1.wait()
- self.assertEqual(total[0], 2)
- evy.sleep(0)
- self.assertEqual(self.actor._pool.free(), 1)
- evt.wait()
- self.assertEqual(total[0], 3)
- evy.sleep(0)
- self.assertEqual(self.actor._pool.free(), 2)
-
-
-if __name__ == '__main__':
- main()

0 comments on commit 8e43b64

Please sign in to comment.