1515"""Tests for connection-pooling with greenlets and Gevent"""
1616
1717import os
18+ import socket
1819import time
1920import threading
2021import unittest
2526from test .test_connection import get_connection
2627from test .utils import delay , force_reclaim_sockets
2728
29+
2830host = os .environ .get ("DB_IP" , "localhost" )
2931port = int (os .environ .get ("DB_PORT" , 27017 ))
3032
33+
3134def looplet (greenlets ):
3235 """World's smallest event loop; run until all greenlets are done
3336 """
@@ -55,39 +58,23 @@ def _test_pool(self, use_greenlets, use_request):
5558 gr1: get results
5659 gr0: get results
5760 """
58- NOT_STARTED = 0
59- SUCCESS = 1
60- SKIP = 2
61+ if use_greenlets :
62+ try :
63+ from gevent import monkey , Greenlet
64+ except ImportError :
65+ raise SkipTest ('gevent not installed' )
66+
67+ # Note we don't do patch_thread() or patch_all() - we're
68+ # testing here that patch_thread() is unnecessary for
69+ # the connection pool to work properly.
70+ monkey .patch_socket ()
6171
6272 try :
63- from multiprocessing import Value , Process
64- except ImportError :
65- # Python < 2.6
66- raise SkipTest ('No multiprocessing module' )
67-
68- outcome = Value ('i' , NOT_STARTED )
69-
70- results = {
71- 'find_fast_result' : None ,
72- 'find_slow_result' : None ,
73- }
74-
75- # Do test in separate process so patch_socket() doesn't affect all
76- # subsequent unittests
77- def do_test ():
78- if use_greenlets :
79- try :
80- from gevent import Greenlet
81- from gevent import monkey
82-
83- # Note we don't do patch_thread() or patch_all() - we're
84- # testing here that patch_thread() is unnecessary for
85- # the connection pool to work properly.
86- monkey .patch_socket ()
87- except ImportError :
88- outcome .value = SKIP
89- return
90-
73+ results = {
74+ 'find_fast_result' : None ,
75+ 'find_slow_result' : None ,
76+ }
77+
9178 cx = get_connection (
9279 use_greenlets = use_greenlets ,
9380 auto_start_request = False
@@ -120,8 +107,8 @@ def find_slow():
120107
121108 history .append ('find_slow start' )
122109
123- # Javascript function that pauses for half a second
124- where = delay (0.5 )
110+ # Javascript function that pauses
111+ where = delay (2 )
125112 results ['find_slow_result' ] = list (db .test .find (
126113 {'$where' : where }
127114 ))
@@ -139,7 +126,7 @@ def find_slow():
139126 gr0 = threading .Thread (target = find_slow )
140127 gr1 = threading .Thread (target = find_fast )
141128 gr0 .start ()
142- time .sleep (0 .1 )
129+ time .sleep (.1 )
143130 gr1 .start ()
144131
145132 gr0 .join ()
@@ -157,20 +144,9 @@ def find_slow():
157144 'find_slow done' ,
158145 ], history )
159146
160- outcome .value = SUCCESS
161-
162- proc = Process (target = do_test )
163- proc .start ()
164- proc .join ()
165-
166- if outcome .value == SKIP :
167- raise SkipTest ('gevent not installed' )
168-
169- self .assertEqual (
170- SUCCESS ,
171- outcome .value ,
172- 'test failed'
173- )
147+ finally :
148+ # Undo Gevent patching
149+ reload (socket )
174150
175151 def test_threads_pool (self ):
176152 # Test the same sequence of calls as the gevent tests to ensure my test
0 commit comments