1
# -*- test-case-name: twisted.trial.test.test_util -*-
2
from twisted.python import failure, log
3
from twisted.python.runtime import platformType
1
from twisted.python import log
2
from twisted.internet import utils
4
3
from twisted.internet import defer, reactor, threads, interfaces
5
from twisted.trial import unittest, util, runner
7
import os, time, signal
9
class UserError(Exception):
12
class TestUserMethod(unittest.TestCase):
14
self.janitor = util._Janitor()
16
def errorfulMethod(self):
17
raise UserError, 'i am a user error'
19
def errorfulDeferred(self):
27
def testErrorHandling(self):
28
"""wrapper around user code"""
29
umw = util.UserMethodWrapper(self.errorfulMethod, self.janitor)
30
self.failUnlessRaises(util.UserMethodError, umw)
31
self.failUnless(umw.errors[0].check(UserError))
32
self.failUnless(umw.endTime >= umw.startTime)
34
def testDeferredError(self):
35
umw = util.UserMethodWrapper(self.errorfulDeferred, self.janitor)
36
self.failUnlessRaises(util.UserMethodError, umw)
37
self.failUnless(umw.errors[0].check(UserError))
38
self.failUnless(umw.endTime >= umw.startTime)
4
from twisted.trial import unittest, util
5
from twisted.trial.test import packages
9
suppress = [(['ignore', 'Do NOT use wait.*'], {})]
41
11
class WaitReentrancyTest(unittest.TestCase):
74
44
def testReentrantWait(self):
75
45
self.assertRaises(util.WaitIsNotReentrantError, self._reentrantWait)
78
class TestWait2(unittest.TestCase):
81
def _generateFailure(self):
83
raise RuntimeError, "i am a complete and utter failure"
85
return failure.Failure()
87
def _errorfulMethod(self):
88
L = [self._generateFailure() for x in xrange(self.NUM_FAILURES)]
89
raise util.MultiError(L)
91
def testMultiError(self):
92
self.assertRaises(util.MultiError, self._errorfulMethod)
94
self._errorfulMethod()
95
except util.MultiError, e:
96
self.assert_(hasattr(e, 'failures'))
97
self.assertEqual(len(e.failures), self.NUM_FAILURES)
99
self.assert_(f.check(RuntimeError))
101
def testMultiErrorAsFailure(self):
102
self.assertRaises(util.MultiError, self._errorfulMethod)
104
self._errorfulMethod()
105
except util.MultiError:
106
f = failure.Failure()
107
self.assert_(hasattr(f, 'value'))
108
self.assert_(hasattr(f.value, 'failures'))
109
self.assertEqual(len(f.value.failures), self.NUM_FAILURES)
110
for f in f.value.failures:
111
self.assert_(f.check(RuntimeError))
47
def test_twoWaitImplementations(self):
48
# If this test times out, then wait is being re-entered.
49
tc = TestMktemp('test_name')
50
tc._timedOut = False # whitebox
52
def _runsInsideWait(r):
54
self.assertRaises(util.WaitIsNotReentrantError, util.wait, d)
55
d.addCallback(utils.suppressWarnings(_runsInsideWait, *suppress))
56
reactor.callLater(0, d.callback, 'yo')
58
test_twoWaitImplementations.timeout = 4
114
61
class TestMktemp(unittest.TestCase):
118
exp = os.path.join('twisted.trial.test.test_trial', 'UtilityTestCase', 'testMktmp')
119
self.failIfEqual(tmp, tmp1)
120
self.failIf(os.path.exists(exp))
64
dirs = os.path.dirname(name).split(os.sep)[:-1]
66
dirs, ['twisted.trial.test.test_util', 'TestMktemp', 'test_name'])
68
def test_unique(self):
70
self.failIfEqual(name, self.mktemp())
72
def test_created(self):
74
dirname = os.path.dirname(name)
75
self.failUnless(os.path.exists(dirname))
76
self.failIf(os.path.exists(name))
78
def test_location(self):
79
path = os.path.abspath(self.mktemp())
80
self.failUnless(path.startswith(os.getcwd()))
123
83
class TestWaitInterrupt(unittest.TestCase):
199
159
for a, b in zip(parents, expected):
200
160
self.failUnlessEqual(a, b)
163
class TestFindObject(packages.PackageTest):
165
packages.PackageTest.setUp(self, '_TestFindObject')
166
self.oldPath = sys.path[:]
167
sys.path.append('_TestFindObject')
170
sys.path = self.oldPath
171
packages.PackageTest.tearDown(self, '_TestFindObject')
173
def test_importPackage(self):
174
package1 = util.findObject('package')
175
import package as package2
176
self.failUnlessEqual(package1, (True, package2))
178
def test_importModule(self):
179
test_sample2 = util.findObject('goodpackage.test_sample')
180
from goodpackage import test_sample
181
self.failUnlessEqual((True, test_sample), test_sample2)
183
def test_importError(self):
184
self.failUnlessRaises(ZeroDivisionError,
185
util.findObject, 'package.test_bad_module')
187
def test_sophisticatedImportError(self):
188
self.failUnlessRaises(ImportError,
189
util.findObject, 'package2.test_module')
191
def test_importNonexistentPackage(self):
192
self.failUnlessEqual(util.findObject('doesntexist')[0], False)
194
def test_findNonexistentModule(self):
195
self.failUnlessEqual(util.findObject('package.doesntexist')[0], False)
197
def test_findNonexistentObject(self):
198
self.failUnlessEqual(util.findObject(
199
'goodpackage.test_sample.doesnt')[0], False)
200
self.failUnlessEqual(util.findObject(
201
'goodpackage.test_sample.AlphabetTest.doesntexist')[0], False)
203
def test_findObjectExist(self):
204
alpha1 = util.findObject('goodpackage.test_sample.AlphabetTest')
205
from goodpackage import test_sample
206
self.failUnlessEqual(alpha1, (True, test_sample.AlphabetTest))