Skip to content

Commit 280a7fc

Browse files
author
A. Jesse Jiryu Davis
committed
Test GreenletIdent with Gevent enhanced Greenlets.
1 parent 804b90a commit 280a7fc

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

test/test_thread_util.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
from pymongo import thread_util
2828
if thread_util.have_gevent:
29-
import greenlet
29+
import greenlet # Plain greenlets.
30+
import gevent.greenlet # Gevent's enhanced Greenlets.
31+
import gevent.hub
3032

3133
from test.utils import looplet, RendezvousThread
3234

@@ -154,6 +156,42 @@ def test_greenlet_ident(self):
154156
self._test_ident(True)
155157

156158

159+
class TestGreenletIdent(unittest.TestCase):
160+
def setUp(self):
161+
if not thread_util.have_gevent:
162+
raise SkipTest("need Gevent")
163+
164+
def test_unwatch_cleans_up(self):
165+
# GreenletIdent.unwatch() should remove the on_thread_died callback
166+
# from an enhanced Gevent Greenlet's list of links.
167+
callback_ran = [False]
168+
169+
def on_greenlet_died(_):
170+
callback_ran[0] = True
171+
172+
ident = thread_util.create_ident(use_greenlets=True)
173+
174+
def watch_and_unwatch():
175+
ident.watch(on_greenlet_died)
176+
ident.unwatch(ident.get())
177+
178+
g = gevent.greenlet.Greenlet(run=watch_and_unwatch)
179+
g.start()
180+
g.join(10)
181+
the_hub = gevent.hub.get_hub()
182+
if hasattr(the_hub, 'join'):
183+
# Gevent 1.0
184+
the_hub.join()
185+
else:
186+
# Gevent 0.13 and less
187+
the_hub.shutdown()
188+
189+
self.assertTrue(g.successful())
190+
191+
# unwatch() canceled the callback.
192+
self.assertFalse(callback_ran[0])
193+
194+
157195
# No functools in Python 2.4
158196
def my_partial(f, *args, **kwargs):
159197
def _f(*new_args, **new_kwargs):

0 commit comments

Comments
 (0)