def test_reentrant_lock(self): '''Test whether retaking a lock fails''' p = CMDB() p.saveObject(self.cmdbobject, dict()) p.getObjectWithLock(self.cmdbobject) self.assertRaises(DoubleLockException, p.getObjectWithLock, self.cmdbobject)
def test_multiple_release(self): '''Test whether multiple release calls succeeds''' p = CMDB() p.saveObject(self.cmdbobject, 123) p.getObjectWithLock(self.cmdbobject) p.releaseObjectLock(self.cmdbobject) p.releaseObjectLock(self.cmdbobject) p.releaseObjectLock(self.cmdbobject)
def test_store_retrieve_none(self): '''Test register-store-retrieve cycle using None as object''' p = CMDB() p.registerObject(self.cmdbobject, None) p.getObjectWithLock(self.cmdbobject) p.saveObject(self.cmdbobject, None) v = p.getObjectWithLock(self.cmdbobject) self.assertEqual(v, None)
def test_timeout_but_lock_kept(self): p = CMDB() p.saveObject(self.cmdbobject, None) o = p.getObjectWithLock(self.cmdbobject, locktimeout=1) time.sleep(2) p.saveObject('test-test', 123)
def test_context_manager(self): p = CMDB() p.saveObject(self.cmdbobject, 456) with p(self.cmdbobject) as value: self.assertEquals(value, 456) p.saveObject(self.cmdbobject, 123) self.assert_(not p.pm_isLocked(self.cmdbobject)) self.assertRaises(ObjectNotOwnedException, p.saveObject, self.cmdbobject, 456)
def test_store_retrieve_dict(self): '''Test register-store-retrieve cycle using dict as object''' d = { 'foo': 'bar', 'baz': ['bat', '123', ], } p = CMDB() p.registerObject(self.cmdbobject, None) v = p.getObjectWithLock(self.cmdbobject) p.saveObject(self.cmdbobject, d) v = p.getObjectWithLock(self.cmdbobject) self.assertEqual(v, d)
def test_generations(self): '''Test whether register-store-retrieve-store-retrieve works fine''' l = random.randint(0, 100) l1 = [random.randint(0, 100) for i in xrange(l)] l2 = [random.randint(0, 100) for i in xrange(l / 2)] p = CMDB() p.registerObject(self.cmdbobject, None) p.getObjectWithLock(self.cmdbobject) p.saveObject(self.cmdbobject, l1) v = p.getObjectWithLock(self.cmdbobject) self.assertEqual(v, l1) p.saveObject(self.cmdbobject, l2) v = p.getObjectWithLock(self.cmdbobject) self.assertEqual(v, l2)
def test_getobject(self): p = CMDB() p.saveObject(self.cmdbobject, 456) def run(): p.getObjectWithLock(self.cmdbobject) start = time.time() value = p.getObject(self.cmdbobject) end = time.time() self.assertEquals(value, 456) #getObject should be non-blocking, so the time to fetch the object #should be at least less than 1s self.assert_(end - start < 1.0)
def test_store_retrieve_dict(self): '''Test register-store-retrieve cycle using dict as object''' d = { 'foo': 'bar', 'baz': [ 'bat', '123', ], } p = CMDB() p.registerObject(self.cmdbobject, None) v = p.getObjectWithLock(self.cmdbobject) p.saveObject(self.cmdbobject, d) v = p.getObjectWithLock(self.cmdbobject) self.assertEqual(v, d)
def test_timeout_lock_lost(self): p = CMDB() p.saveObject(self.cmdbobject, None) def run(): o = p.getObjectWithLock(self.cmdbobject) t = threading.Thread(target=run) o = p.getObjectWithLock(self.cmdbobject, locktimeout=2) t.start() t.join() #Object has been owned by another thread by now self.assertRaises(ObjectNotOwnedException, p.saveObject, self.cmdbobject, 123)
def test_timeout_relock(self): p = CMDB() p.saveObject(self.cmdbobject, None) from pylabs.cmdb.cmdb import DEFAULT_LOCK_TIMEOUT, isLocked, isSafe p.getObjectWithLock(self.cmdbobject) self.assert_(isLocked(self.cmdbobject)) self.assert_(isSafe(self.cmdbobject)) time.sleep(DEFAULT_LOCK_TIMEOUT + 1.0) self.assert_(not isLocked(self.cmdbobject)) self.assert_(isSafe(self.cmdbobject)) p.getObjectWithLock(self.cmdbobject) self.assert_(isLocked(self.cmdbobject)) self.assert_(isSafe(self.cmdbobject))
def test_timeout(self): p = CMDB() p.saveObject(self.cmdbobject, 123) def run(): p.getObjectWithLock(self.cmdbobject, locktimeout=3) time.sleep(3) t1 = threading.Thread(target=run) t1.start() time.sleep(1) self.assert_(p.pm_isLocked(self.cmdbobject)) start = time.time() p.getObjectWithLock(self.cmdbobject) end = time.time() #end - start must be somewhere close to 3 self.assert_(end - start > 2) self.assert_(end - start < 4)
def test_unregistered_type(self): '''Test whether we can't store a not-registered type''' p = CMDB() p.saveObject(self.cmdbobject, None)
def test_storage(self): '''Test whether we can store an object in CMDB''' p = CMDB() p.registerObject(self.cmdbobject, None) p.getObjectWithLock(self.cmdbobject) p.saveObject(self.cmdbobject, None)
def test_double_save(self): '''Test whether two save calls fails''' p = CMDB() p.saveObject(self.cmdbobject, 123) self.assertRaises(ObjectNotOwnedException, p.saveObject, self.cmdbobject, 456)