Skip to content

Commit 7d0240a

Browse files
committed
Test Pool(use_greenlets=True) when Gevent is not installed. PYTHON-561
1 parent 58b5ece commit 7d0240a

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

test/test_pooling_gevent.py

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414

1515
"""Tests for connection-pooling with greenlets and Gevent"""
1616

17+
import gc
18+
import time
1719
import unittest
1820

1921
from nose.plugins.skip import SkipTest
2022

2123
from pymongo import pool
24+
from pymongo.errors import ConfigurationError
2225
from test import host, port
2326
from test.utils import looplet
2427
from test.test_pooling_base import (
2528
_TestPooling, _TestMaxPoolSize, _TestMaxOpenSockets,
26-
_TestPoolSocketSharing, _TestWaitQueueMultiple)
29+
_TestPoolSocketSharing, _TestWaitQueueMultiple, has_gevent)
2730

2831

2932
class TestPoolingGevent(_TestPooling, unittest.TestCase):
@@ -189,5 +192,49 @@ class TestWaitQueueMultipleGevent(_TestWaitQueueMultiple, unittest.TestCase):
189192
use_greenlets = True
190193

191194

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+
192239
if __name__ == '__main__':
193240
unittest.main()

0 commit comments

Comments
 (0)