Commit cdd086e7 authored by Hanno Schlichting's avatar Hanno Schlichting

Cleaned up and speed up Transience tests a bit. Now they take 20 seconds out...

Cleaned up and speed up Transience tests a bit. Now they take 20 seconds out of 50 seconds total for Zope2 - before it was 30 out of 60. There should still be some more room for improvement.
parent 84601539
import sys
import time as origtime import time as origtime
epoch = origtime.time() epoch = origtime.time()
resolution = 120.0
timeout = 30
if sys.platform[:3].lower() == "win":
resolution = 60.0
timeout = 60
def time(): def time():
""" False timer -- returns time 60 x faster than normal time """ """ False timer -- returns time R x faster than normal time """
return (origtime.time() - epoch) * 60.0 return (origtime.time() - epoch) * resolution
def sleep(duration): def sleep(duration):
""" False sleep -- sleep for 1/60 the time specifed """ """ False sleep -- sleep for 1/R the time specifed """
origtime.sleep(duration / 60.0) origtime.sleep(duration / resolution)
import time as origtime
epoch = origtime.time()
def time():
""" False timer -- returns time 60 x faster than normal time """
return (origtime.time() - epoch) * 60
def sleep(duration):
""" False sleep -- sleep for 1/60 the time specifed """
origtime.sleep(duration / 60)
import sys, os, time, unittest
if __name__=='__main__':
sys.path.insert(0, '..')
sys.path.insert(0, '../../..')
import ZODB # in order to get Persistence.Persistent working import ZODB # in order to get Persistence.Persistent working
import transaction import transaction
from Testing import makerequest from Testing import makerequest
import Acquisition
from Acquisition import aq_base
from Products.Transience.Transience import TransientObjectContainer from Products.Transience.Transience import TransientObjectContainer
import Products.Transience.Transience import Products.Transience.Transience
import Products.Transience.TransientObject import Products.Transience.TransientObject
from Products.PythonScripts.PythonScript import PythonScript from unittest import TestCase, TestSuite, makeSuite
from ZODB.POSException import InvalidObjectReference
from DateTime import DateTime
from unittest import TestCase, TestSuite, TextTestRunner, makeSuite
from ZODB.DemoStorage import DemoStorage from ZODB.DemoStorage import DemoStorage
from OFS.Application import Application from OFS.Application import Application
import threading
import fauxtime import fauxtime
import time as oldtime import time as oldtime
...@@ -26,7 +14,6 @@ WRITEGRANULARITY = 30 ...@@ -26,7 +14,6 @@ WRITEGRANULARITY = 30
stuff = {} stuff = {}
def _getApp(): def _getApp():
app = stuff.get('app', None) app = stuff.get('app', None)
if not app: if not app:
ds = DemoStorage() ds = DemoStorage()
...@@ -109,10 +96,9 @@ class TestNotifications(TestBase): ...@@ -109,10 +96,9 @@ class TestNotifications(TestBase):
def testDelNotification(self): def testDelNotification(self):
self.app.sm.setDelNotificationTarget(delNotificationTarget) self.app.sm.setDelNotificationTarget(delNotificationTarget)
sdo = self.app.sm.new_or_existing('TempObject') sdo = self.app.sm.new_or_existing('TempObject')
timeout = self.timeout * 60 # sleep longer than timeout
# sleep 2X longer than timeout? doesnt work at 1.1X, 1.5X? fauxtime.sleep(self.timeout * 100.0)
fauxtime.sleep(timeout * 2) self.app.sm.get('TempObject')
sdo1 = self.app.sm.get('TempObject')
now = fauxtime.time() now = fauxtime.time()
k = sdo.get('endtime') k = sdo.get('endtime')
self.assertEqual(type(k), type(now)) self.assertEqual(type(k), type(now))
...@@ -122,18 +108,17 @@ class TestNotifications(TestBase): ...@@ -122,18 +108,17 @@ class TestNotifications(TestBase):
# in response to http://zope.org/Collectors/Zope/1403 # in response to http://zope.org/Collectors/Zope/1403
self.assertEqual(None, self.app.sm._getCallback('/foo/bar/baz')) self.assertEqual(None, self.app.sm._getCallback('/foo/bar/baz'))
def addNotificationTarget(item, context): def addNotificationTarget(item, context):
item['starttime'] = fauxtime.time() item['starttime'] = fauxtime.time()
def delNotificationTarget(item, context): def delNotificationTarget(item, context):
item['endtime'] = fauxtime.time() item['endtime'] = fauxtime.time()
def test_suite(): def test_suite():
last_accessed = makeSuite(TestLastAccessed, 'test') last_accessed = makeSuite(TestLastAccessed, 'test')
start_end = makeSuite(TestNotifications, 'test') start_end = makeSuite(TestNotifications, 'test')
suite = TestSuite((start_end, last_accessed)) suite = TestSuite((start_end, last_accessed))
return suite return suite
if __name__ == '__main__':
runner = TextTestRunner(verbosity=9)
runner.run(test_suite())
...@@ -10,17 +10,13 @@ ...@@ -10,17 +10,13 @@
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE
# #
############################################################################## ##############################################################################
import sys, os, time, random, unittest
if __name__ == "__main__":
sys.path.insert(0, '../../..')
import ZODB
import transaction import transaction
from unittest import TestCase, TestSuite, TextTestRunner, makeSuite from unittest import TestCase, makeSuite
from Products.Transience.TransactionHelper import PreventTransactionCommit, \ from Products.Transience.TransactionHelper import PreventTransactionCommit, \
makeTransactionUncommittable makeTransactionUncommittable
class TestTransactionHelper(TestCase): class TestTransactionHelper(TestCase):
def setUp(self): def setUp(self):
self.t = transaction.get() self.t = transaction.get()
...@@ -32,11 +28,8 @@ class TestTransactionHelper(TestCase): ...@@ -32,11 +28,8 @@ class TestTransactionHelper(TestCase):
makeTransactionUncommittable(self.t, "test") makeTransactionUncommittable(self.t, "test")
self.assertRaises(PreventTransactionCommit, transaction.commit) self.assertRaises(PreventTransactionCommit, transaction.commit)
transaction.abort() transaction.abort()
def test_suite(): def test_suite():
suite = makeSuite(TestTransactionHelper, 'test') suite = makeSuite(TestTransactionHelper, 'test')
return suite return suite
if __name__ == '__main__':
runner = TextTestRunner(verbosity=9)
runner.run(test_suite())
...@@ -10,14 +10,11 @@ ...@@ -10,14 +10,11 @@
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE
# #
############################################################################## ##############################################################################
import sys, os, unittest
import ZODB
from Products.Transience.Transience import TransientObjectContainer from Products.Transience.Transience import TransientObjectContainer
from Products.Transience.TransientObject import TransientObject
import Products.Transience.TransientObject import Products.Transience.TransientObject
import Products.Transience.Transience import Products.Transience.Transience
from unittest import TestCase, TestSuite, TextTestRunner, makeSuite from unittest import TestCase, TestSuite, makeSuite
import time as oldtime import time as oldtime
import fauxtime import fauxtime
...@@ -27,7 +24,7 @@ class TestTransientObject(TestCase): ...@@ -27,7 +24,7 @@ class TestTransientObject(TestCase):
Products.Transience.TransientObject.time = fauxtime Products.Transience.TransientObject.time = fauxtime
Products.Transience.Transience.setStrict(1) Products.Transience.Transience.setStrict(1)
self.errmargin = .20 self.errmargin = .20
self.timeout = 60 self.timeout = fauxtime.timeout
self.t = TransientObjectContainer('sdc', timeout_mins=self.timeout/60) self.t = TransientObjectContainer('sdc', timeout_mins=self.timeout/60)
def tearDown(self): def tearDown(self):
...@@ -69,7 +66,6 @@ class TestTransientObject(TestCase): ...@@ -69,7 +66,6 @@ class TestTransientObject(TestCase):
def testSetLastModified(self): def testSetLastModified(self):
t = self.t.new('xyzzy') t = self.t.new('xyzzy')
ft = fauxtime.time()
t.setLastModified() t.setLastModified()
self.failIfEqual(t.getLastModified(), None) self.failIfEqual(t.getLastModified(), None)
...@@ -137,7 +133,3 @@ data = { ...@@ -137,7 +133,3 @@ data = {
'epoch': 999999999, 'epoch': 999999999,
'fauxtime': fauxtime 'fauxtime': fauxtime
} }
if __name__ == '__main__':
runner = TextTestRunner(verbosity=9)
runner.run(test_suite())
...@@ -10,30 +10,28 @@ ...@@ -10,30 +10,28 @@
# FOR A PARTICULAR PURPOSE # FOR A PARTICULAR PURPOSE
# #
############################################################################## ##############################################################################
import sys, os, time, random, unittest import random
if __name__ == "__main__":
sys.path.insert(0, '../../..')
import ZODB
from Products.Transience.Transience import TransientObjectContainer,\ from Products.Transience.Transience import TransientObjectContainer,\
MaxTransientObjectsExceeded, SPARE_BUCKETS, getCurrentTimeslice MaxTransientObjectsExceeded, SPARE_BUCKETS
from Products.Transience.TransientObject import TransientObject from Products.Transience.TransientObject import TransientObject
import Products.Transience.Transience import Products.Transience.Transience
import Products.Transience.TransientObject import Products.Transience.TransientObject
from ExtensionClass import Base from unittest import TestCase, TestSuite, makeSuite
from unittest import TestCase, TestSuite, TextTestRunner, makeSuite
import time as oldtime import time as oldtime
import fauxtime import fauxtime
import slowfauxtime
class TestTransientObjectContainer(TestCase):
class TestBase(TestCase):
def setUp(self): def setUp(self):
Products.Transience.Transience.time = fauxtime Products.Transience.Transience.time = fauxtime
Products.Transience.TransientObject.time = fauxtime Products.Transience.TransientObject.time = fauxtime
Products.Transience.Transience.setStrict(1) Products.Transience.Transience.setStrict(1)
self.errmargin = .20 self.errmargin = .20
self.timeout = 120 self.timeout = fauxtime.timeout
self.period = 20 self.period = 20
self.t = TransientObjectContainer('sdc', timeout_mins=self.timeout/60, self.t = TransientObjectContainer('sdc', timeout_mins=self.timeout/60,
period_secs=self.period) period_secs=self.period)
...@@ -44,7 +42,6 @@ class TestBase(TestCase): ...@@ -44,7 +42,6 @@ class TestBase(TestCase):
Products.Transience.TransientObject.time = oldtime Products.Transience.TransientObject.time = oldtime
Products.Transience.Transience.setStrict(0) Products.Transience.Transience.setStrict(0)
class TestTransientObjectContainer(TestBase):
def testGetItemFails(self): def testGetItemFails(self):
self.assertRaises(KeyError, self._getitemfail) self.assertRaises(KeyError, self._getitemfail)
...@@ -239,59 +236,6 @@ class TestTransientObjectContainer(TestBase): ...@@ -239,59 +236,6 @@ class TestTransientObjectContainer(TestBase):
except KeyError: except KeyError:
self.failIf(self.t.has_key(x)) self.failIf(self.t.has_key(x))
def testItemsGetExpired(self):
for x in range(10, 110):
self.t[x] = x
# these items will time out while we sleep
fauxtime.sleep(self.timeout * (self.errmargin+1))
for x in range(110, 210):
self.t[x] = x
self.assertEqual(len(self.t.keys()), 100)
# call _gc just to make sure __len__ gets changed after a gc
#self.t._gc()
self.assertEqual(len(self.t), 100)
# we should still have 100 - 199
for x in range(110, 210):
self.assertEqual(self.t[x], x)
# but we shouldn't have 0 - 100
for x in range(10, 110):
try: self.t[x]
except KeyError: pass
else: assert 1 == 2, x
def testChangingTimeoutWorks(self):
# 1 minute
for x in range(10, 110):
self.t[x] = x
fauxtime.sleep(self.timeout * (self.errmargin+1))
self.assertEqual(len(self.t.keys()), 0)
# 2 minutes
self.t._setTimeout(self.timeout/60*2, self.period)
self.t._reset()
for x in range(10, 110):
self.t[x] = x
fauxtime.sleep(self.timeout)
self.assertEqual(len(self.t.keys()), 100)
fauxtime.sleep(self.timeout * (self.errmargin+1))
self.assertEqual(len(self.t.keys()), 0)
# 3 minutes
self.t._setTimeout(self.timeout/60*3, self.period)
self.t._reset()
for x in range(10, 110):
self.t[x] = x
fauxtime.sleep(self.timeout)
self.assertEqual(len(self.t.keys()), 100)
fauxtime.sleep(self.timeout)
self.assertEqual(len(self.t.keys()), 100)
fauxtime.sleep(self.timeout * (self.errmargin+1))
self.assertEqual(len(self.t.keys()), 0)
def testGetDelaysTimeout(self): def testGetDelaysTimeout(self):
for x in range(10, 110): for x in range(10, 110):
self.t[x] = x self.t[x] = x
...@@ -352,10 +296,10 @@ class TestTransientObjectContainer(TestBase): ...@@ -352,10 +296,10 @@ class TestTransientObjectContainer(TestBase):
self.failUnless(issubclass(t.__class__, TransientObject)) self.failUnless(issubclass(t.__class__, TransientObject))
def _dupNewItem(self): def _dupNewItem(self):
t = self.t.new('foobieblech') self.t.new('foobieblech')
def test_newDupFails(self): def test_newDupFails(self):
t = self.t.new('foobieblech') self.t.new('foobieblech')
self.assertRaises(KeyError, self._dupNewItem) self.assertRaises(KeyError, self._dupNewItem)
def test_new_or_existing(self): def test_new_or_existing(self):
...@@ -397,6 +341,79 @@ class TestTransientObjectContainer(TestBase): ...@@ -397,6 +341,79 @@ class TestTransientObjectContainer(TestBase):
self.t.new(str(x)) self.t.new(str(x))
class TestSlowTransientObjectContainer(TestCase):
def setUp(self):
Products.Transience.Transience.time = slowfauxtime
Products.Transience.TransientObject.time = slowfauxtime
Products.Transience.Transience.setStrict(1)
self.errmargin = .20
self.timeout = 120
self.period = 20
self.t = TransientObjectContainer('sdc', timeout_mins=self.timeout/60,
period_secs=self.period)
def tearDown(self):
self.t = None
Products.Transience.Transience.time = oldtime
Products.Transience.TransientObject.time = oldtime
Products.Transience.Transience.setStrict(0)
def testChangingTimeoutWorks(self):
# TODO: This test is slooooow
# 1 minute
for x in range(10, 110):
self.t[x] = x
slowfauxtime.sleep(self.timeout * (self.errmargin + 1))
self.assertEqual(len(self.t.keys()), 0)
# 2 minutes
self.t._setTimeout(self.timeout/60*2, self.period)
self.t._reset()
for x in range(10, 110):
self.t[x] = x
slowfauxtime.sleep(self.timeout)
self.assertEqual(len(self.t.keys()), 100)
slowfauxtime.sleep(self.timeout * (self.errmargin+1))
self.assertEqual(len(self.t.keys()), 0)
# 3 minutes
self.t._setTimeout(self.timeout/60*3, self.period)
self.t._reset()
for x in range(10, 110):
self.t[x] = x
slowfauxtime.sleep(self.timeout)
self.assertEqual(len(self.t.keys()), 100)
slowfauxtime.sleep(self.timeout)
self.assertEqual(len(self.t.keys()), 100)
slowfauxtime.sleep(self.timeout * (self.errmargin+1))
self.assertEqual(len(self.t.keys()), 0)
def testItemsGetExpired(self):
for x in range(10, 110):
self.t[x] = x
# these items will time out while we sleep
slowfauxtime.sleep(self.timeout * (self.errmargin+1))
for x in range(110, 210):
self.t[x] = x
self.assertEqual(len(self.t.keys()), 100)
# call _gc just to make sure __len__ gets changed after a gc
#self.t._gc()
self.assertEqual(len(self.t), 100)
# we should still have 100 - 199
for x in range(110, 210):
self.assertEqual(self.t[x], x)
# but we shouldn't have 0 - 100
for x in range(10, 110):
try: self.t[x]
except KeyError: pass
else: assert 1 == 2, x
def lsubtract(l1, l2): def lsubtract(l1, l2):
l1=list(l1) l1=list(l1)
l2=list(l2) l2=list(l2)
...@@ -404,11 +421,9 @@ def lsubtract(l1, l2): ...@@ -404,11 +421,9 @@ def lsubtract(l1, l2):
l = l + filter(lambda x, l2=l2: x not in l2, l1) l = l + filter(lambda x, l2=l2: x not in l2, l1)
return l return l
def test_suite():
testsuite = makeSuite(TestTransientObjectContainer, 'test')
alltests = TestSuite((testsuite,))
return alltests
if __name__ == '__main__': def test_suite():
runner = TextTestRunner(verbosity=9) suite = TestSuite()
runner.run(test_suite()) suite.addTest(makeSuite(TestTransientObjectContainer))
suite.addTest(makeSuite(TestSlowTransientObjectContainer))
return suite
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