|
14 | 14 |
|
15 | 15 | """Tests for connection-pooling with greenlets and Gevent"""
|
16 | 16 |
|
| 17 | +import gc |
| 18 | +import time |
17 | 19 | import unittest
|
18 | 20 |
|
19 | 21 | from nose.plugins.skip import SkipTest
|
20 | 22 |
|
21 | 23 | from pymongo import pool
|
| 24 | +from pymongo.errors import ConfigurationError |
22 | 25 | from test import host, port
|
23 | 26 | from test.utils import looplet
|
24 | 27 | from test.test_pooling_base import (
|
25 | 28 | _TestPooling, _TestMaxPoolSize, _TestMaxOpenSockets,
|
26 |
| - _TestPoolSocketSharing, _TestWaitQueueMultiple) |
| 29 | + _TestPoolSocketSharing, _TestWaitQueueMultiple, has_gevent) |
27 | 30 |
|
28 | 31 |
|
29 | 32 | class TestPoolingGevent(_TestPooling, unittest.TestCase):
|
@@ -189,5 +192,49 @@ class TestWaitQueueMultipleGevent(_TestWaitQueueMultiple, unittest.TestCase):
|
189 | 192 | use_greenlets = True
|
190 | 193 |
|
191 | 194 |
|
| 195 | +class TestUseGreenletsWithoutGevent(unittest.TestCase): |
| 196 | + def test_use_greenlets_without_gevent(self): |
| 197 | + # Verify that Pool(use_greenlets=True) raises ConfigurationError if |
| 198 | + # Gevent is not installed, and that its destructor runs without error. |
| 199 | + if has_gevent: |
| 200 | + raise SkipTest( |
| 201 | + "Gevent is installed, can't test what happens calling " |
| 202 | + "Pool(use_greenlets=True) when Gevent is unavailable") |
| 203 | + |
| 204 | + # Possible outcomes of __del__. |
| 205 | + DID_NOT_RUN, RAISED, SUCCESS = range(3) |
| 206 | + outcome = [DID_NOT_RUN] |
| 207 | + |
| 208 | + class TestPool(pool.Pool): |
| 209 | + def __del__(self): |
| 210 | + try: |
| 211 | + pool.Pool.__del__(self) # Pool is old-style, no super() |
| 212 | + outcome[0] = SUCCESS |
| 213 | + except: |
| 214 | + outcome[0] = RAISED |
| 215 | + |
| 216 | + # Pool raises ConfigurationError, "The Gevent module is not available". |
| 217 | + self.assertRaises( |
| 218 | + ConfigurationError, |
| 219 | + TestPool, |
| 220 | + pair=(host, port), |
| 221 | + max_size=10, |
| 222 | + net_timeout=1000, |
| 223 | + conn_timeout=1000, |
| 224 | + use_ssl=False, |
| 225 | + use_greenlets=True) |
| 226 | + |
| 227 | + # Convince Jython or PyPy to call __del__. |
| 228 | + for _ in range(10): |
| 229 | + if outcome[0] == DID_NOT_RUN: |
| 230 | + gc.collect() |
| 231 | + time.sleep(0.1) |
| 232 | + |
| 233 | + if outcome[0] == DID_NOT_RUN: |
| 234 | + self.fail("Pool.__del__ didn't run") |
| 235 | + elif outcome[0] == RAISED: |
| 236 | + self.fail("Pool.__del__ raised exception") |
| 237 | + |
| 238 | + |
192 | 239 | if __name__ == '__main__':
|
193 | 240 | unittest.main()
|
0 commit comments