Commit 6f4eac96 authored by Jason Madden's avatar Jason Madden

Cleanup to pass leak tests.

parent 0c1d7806
...@@ -81,20 +81,11 @@ class TestSemaphoreMultiThread(greentest.TestCase): ...@@ -81,20 +81,11 @@ class TestSemaphoreMultiThread(greentest.TestCase):
# would be from an arbitrary thread. # would be from an arbitrary thread.
return Semaphore(1, gevent.get_hub()) return Semaphore(1, gevent.get_hub())
def test_acquire_in_one_then_another(self, release=True, **thread_acquire_kwargs): def _makeThreadMain(self, thread_running, thread_acquired, sem,
from gevent import monkey acquired, exc_info,
self.assertFalse(monkey.is_module_patched('threading')) **thread_acquire_kwargs):
from gevent._hub_local import get_hub_if_exists
import sys import sys
import threading
thread_running = threading.Event()
thread_acquired = threading.Event()
sem = self._makeOne()
# Make future acquires block
sem.acquire()
exc_info = []
acquired = []
def thread_main(): def thread_main():
thread_running.set() thread_running.set()
...@@ -106,9 +97,33 @@ class TestSemaphoreMultiThread(greentest.TestCase): ...@@ -106,9 +97,33 @@ class TestSemaphoreMultiThread(greentest.TestCase):
exc_info[:] = sys.exc_info() exc_info[:] = sys.exc_info()
raise # Print raise # Print
finally: finally:
hub = get_hub_if_exists()
if hub is not None:
hub.join()
hub.destroy(destroy_loop=True)
thread_acquired.set() thread_acquired.set()
return thread_main
def _do_test_acquire_in_one_then_another(self, release=True, **thread_acquire_kwargs):
from gevent import monkey
self.assertFalse(monkey.is_module_patched('threading'))
import threading
thread_running = threading.Event()
thread_acquired = threading.Event()
t = threading.Thread(target=thread_main) sem = self._makeOne()
# Make future acquires block
sem.acquire()
exc_info = []
acquired = []
t = threading.Thread(target=self._makeThreadMain(
thread_running, thread_acquired, sem,
acquired, exc_info,
**thread_acquire_kwargs
))
t.start() t.start()
thread_running.wait(10) # implausibly large time thread_running.wait(10) # implausibly large time
if release: if release:
...@@ -128,19 +143,21 @@ class TestSemaphoreMultiThread(greentest.TestCase): ...@@ -128,19 +143,21 @@ class TestSemaphoreMultiThread(greentest.TestCase):
self.assertEqual(acquired, [True]) self.assertEqual(acquired, [True])
thread_acquired.wait(timing.LARGE_TICK * 5) thread_acquired.wait(timing.LARGE_TICK * 5)
try: try:
self.assertEqual(exc_info, []) self.assertEqual(exc_info, [])
finally: finally:
exc_info = None exc_info = None
return sem, acquired return sem, acquired
def test_acquire_in_one_then_another(self):
self._do_test_acquire_in_one_then_another(release=True)
def test_acquire_in_one_then_another_timed(self): def test_acquire_in_one_then_another_timed(self):
sem, acquired_in_thread = self.test_acquire_in_one_then_another( sem, acquired_in_thread = self._do_test_acquire_in_one_then_another(
release=False, release=False,
timeout=timing.SMALLEST_RELIABLE_DELAY) timeout=timing.SMALLEST_RELIABLE_DELAY)
self.assertEqual([False], acquired_in_thread) self.assertEqual([False], acquired_in_thread)
# This doesn't, of course, notify anything, because # This doesn't, of course, notify anything, because
# the waiter has given up. # the waiter has given up.
...@@ -157,9 +174,10 @@ class TestSemaphoreMultiThread(greentest.TestCase): ...@@ -157,9 +174,10 @@ class TestSemaphoreMultiThread(greentest.TestCase):
from gevent import monkey from gevent import monkey
self.assertFalse(monkey.is_module_patched('threading')) self.assertFalse(monkey.is_module_patched('threading'))
import sys
import threading import threading
sem = self._makeOne() sem = self._makeOne()
# Make future acquires block # Make future acquires block
sem.acquire() sem.acquire()
...@@ -175,17 +193,15 @@ class TestSemaphoreMultiThread(greentest.TestCase): ...@@ -175,17 +193,15 @@ class TestSemaphoreMultiThread(greentest.TestCase):
exc_info = [] exc_info = []
acquired = [] acquired = []
def thread_main():
try:
acquired.append(
sem.acquire(timeout=timing.LARGE_TICK)
)
except:
exc_info[:] = sys.exc_info()
raise # Print
glet = gevent.spawn(greenlet_one) glet = gevent.spawn(greenlet_one)
thread = threading.Thread(target=thread_main) thread = threading.Thread(target=self._makeThreadMain(
threading.Event(), threading.Event(),
sem,
acquired, exc_info,
timeout=timing.LARGE_TICK
))
gevent.idle() gevent.idle()
sem.release() sem.release()
glet.join() glet.join()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment