Fixed intermittent patcher_test failures -- it turns out that you don't want to use tempfile.mkstemp files as dummy modules, because their names sometimes contain dashes and are unimportable.

This commit is contained in:
Ryan Williams
2010-01-06 19:13:50 -08:00
parent 155f9c0e45
commit b8a83ab9a0

View File

@@ -1,7 +1,8 @@
import os
import tempfile
import shutil
import subprocess
import sys
import tempfile
from tests import LimitedTestCase
@@ -16,49 +17,42 @@ from eventlet.green import socket
from eventlet.green import urllib
from eventlet import patcher
print 'patcher', socket, urllib
patcher.inject('%s', globals(), ('socket', socket), ('urllib', urllib))
patcher.inject('base', globals(), ('socket', socket), ('urllib', urllib))
del patcher
"""
import_module_contents = """
import %(mod)s
import httplib
print "importing", %(mod)s, httplib, %(mod)s.socket, %(mod)s.urllib
import patching
import socket
print "importing", patching, socket, patching.socket, patching.urllib
"""
class Patcher(LimitedTestCase):
TEST_TIMEOUT=3 # starting processes is time-consuming
def setUp(self):
self._saved_syspath = sys.path
self.tempfiles = []
self.tempdir = tempfile.mkdtemp('_patcher_test')
def tearDown(self):
sys.path = self._saved_syspath
for tf in self.tempfiles:
os.remove(tf)
shutil.rmtree(self.tempdir)
def write_to_tempfile(self, contents):
fn, filename = tempfile.mkstemp('_patcher_test.py')
fd = os.fdopen(fn, 'w')
def write_to_tempfile(self, name, contents):
filename = os.path.join(self.tempdir, name + '.py')
fd = open(filename, "w")
fd.write(contents)
fd.close()
self.tempfiles.append(filename)
return os.path.dirname(filename), os.path.basename(filename)
def test_patch_a_module(self):
base = self.write_to_tempfile(base_module_contents)
base_modname = os.path.splitext(base[1])[0]
patching = self.write_to_tempfile(patching_module_contents % base_modname)
patching_modname = os.path.splitext(patching[1])[0]
importing = self.write_to_tempfile(
import_module_contents % dict(mod=patching_modname))
self.write_to_tempfile("base", base_module_contents)
self.write_to_tempfile("patching", patching_module_contents)
self.write_to_tempfile("importing", import_module_contents)
python_path = os.pathsep.join(sys.path)
python_path += os.pathsep.join((base[0], patching[0], importing[0]))
python_path = os.pathsep.join(sys.path + [self.tempdir])
new_env = os.environ.copy()
new_env['PYTHONPATH'] = python_path
p = subprocess.Popen([sys.executable,
os.path.join(importing[0], importing[1])],
os.path.join(self.tempdir, "importing.py")],
stdout=subprocess.PIPE, env=new_env)
output = p.communicate()
lines = output[0].split("\n")