|
26 | 26 |
|
27 | 27 | from pymongo import thread_util
|
28 | 28 | 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 |
30 | 32 |
|
31 | 33 | from test.utils import looplet, RendezvousThread
|
32 | 34 |
|
@@ -154,6 +156,42 @@ def test_greenlet_ident(self):
|
154 | 156 | self._test_ident(True)
|
155 | 157 |
|
156 | 158 |
|
| 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 | + |
157 | 195 | # No functools in Python 2.4
|
158 | 196 | def my_partial(f, *args, **kwargs):
|
159 | 197 | def _f(*new_args, **new_kwargs):
|
|
0 commit comments