def testCancelRequest(self): resources = [] def callback(req, res): resources.append(res) exclusiveReq1 = rm._registerResource( "storage", "resource", rm.EXCLUSIVE, callback) exclusiveReq2 = rm._registerResource( "storage", "resource", rm.EXCLUSIVE, callback) exclusiveReq3 = rm._registerResource( "storage", "resource", rm.EXCLUSIVE, callback) self.assertTrue(exclusiveReq1.granted()) self.assertFalse(exclusiveReq2.canceled()) self.assertFalse(exclusiveReq3.granted()) exclusiveReq2.cancel() self.assertTrue(exclusiveReq2.canceled()) self.assertEqual(resources.pop(), None) # exclusiveReq 2 resources.pop().release() # exclusiveReq 1 self.assertTrue(exclusiveReq3.granted()) resources.pop().release() # exclusiveReq 3
def testCancelRequest(self, tmp_manager): resources = [] def callback(req, res): resources.append(res) exclusiveReq1 = rm._registerResource("storage", "resource", rm.EXCLUSIVE, callback) exclusiveReq2 = rm._registerResource("storage", "resource", rm.EXCLUSIVE, callback) exclusiveReq3 = rm._registerResource("storage", "resource", rm.EXCLUSIVE, callback) assert exclusiveReq1.granted() assert not exclusiveReq2.canceled() assert not exclusiveReq3.granted() exclusiveReq2.cancel() assert exclusiveReq2.canceled() assert resources.pop() is None # exclusiveReq 2 resources.pop().release() # exclusiveReq 1 assert exclusiveReq3.granted() resources.pop().release() # exclusiveReq 3
def testRequestRefCmp(self): resources = [] requests = [] def callback(req, res): resources.insert(0, res) requests.insert(0, req) req1 = rm._registerResource( "string", "resource", rm.EXCLUSIVE, callback) req2 = rm._registerResource( "string", "resource", rm.EXCLUSIVE, callback) self.assertNotEqual(req1, req2) self.assertEqual(req1, req1) self.assertEqual(req2, req2) req1.wait() req1Clone = requests.pop() self.assertEqual(req1, req1Clone) self.assertNotEqual(req1Clone, req2) resources.pop().release() req2.wait() req2Clone = requests.pop() self.assertEqual(req2, req2Clone) self.assertNotEqual(req1, req2Clone) self.assertNotEqual(req1Clone, req2Clone) resources[0].release() self.assertNotEqual(req1, "STUFF")
def register(): time.sleep(rnd.randint(0, 4)) rm._registerResource("string", "resource", lockTranslator[rnd.randint(0, 1)], callback) threadLimit.release()
def testRequestRefCmp(self): resources = [] requests = [] def callback(req, res): resources.insert(0, res) requests.insert(0, req) req1 = rm._registerResource("string", "resource", rm.EXCLUSIVE, callback) req2 = rm._registerResource("string", "resource", rm.EXCLUSIVE, callback) assert req1 != req2 assert req1 == req1 assert req2 == req2 req1.wait() req1Clone = requests.pop() assert req1 == req1Clone assert req1Clone != req2 resources.pop().release() req2.wait() req2Clone = requests.pop() assert req2 == req2Clone assert req1 != req2Clone assert req1Clone != req2Clone resources[0].release() assert req1 != "STUFF"
def testRequestWithBadCallbackOnGrant(self, tmp_manager): def callback(req, res): res.release() raise Exception("BUY MILK!") req = rm._registerResource("string", "resource", rm.EXCLUSIVE, callback) req.wait()
def testRequestWithBadCallbackOnGrant(self): def callback(req, res): res.release() raise Exception("BUY MILK!") req = rm._registerResource( "string", "resource", rm.EXCLUSIVE, callback) req.wait()
def testCancelExclusiveBetweenShared(self): resources = [] def callback(req, res): resources.insert(0, res) exclusive1 = rm.acquireResource("string", "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource( "string", "resource", rm.SHARED, callback) sharedReq2 = rm._registerResource( "string", "resource", rm.SHARED, callback) exclusiveReq1 = rm._registerResource( "string", "resource", rm.EXCLUSIVE, callback) sharedReq3 = rm._registerResource( "string", "resource", rm.SHARED, callback) sharedReq4 = rm._registerResource( "string", "resource", rm.SHARED, callback) self.assertFalse(sharedReq1.granted()) self.assertFalse(sharedReq2.granted()) self.assertFalse(exclusiveReq1.granted()) self.assertFalse(sharedReq3.granted()) self.assertFalse(sharedReq4.granted()) exclusiveReq1.cancel() resources.pop() self.assertFalse(sharedReq1.granted()) self.assertFalse(sharedReq2.granted()) self.assertFalse(exclusiveReq1.granted()) self.assertTrue(exclusiveReq1.canceled()) self.assertFalse(sharedReq3.granted()) self.assertFalse(sharedReq4.granted()) exclusive1.release() self.assertTrue(sharedReq1.granted()) self.assertTrue(sharedReq2.granted()) self.assertTrue(sharedReq3.granted()) self.assertTrue(sharedReq4.granted()) while len(resources) > 0: resources.pop().release()
def testCancelExclusiveBetweenShared(self, tmp_manager): resources = [] def callback(req, res): resources.insert(0, res) exclusive1 = rm.acquireResource("string", "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource("string", "resource", rm.SHARED, callback) sharedReq2 = rm._registerResource("string", "resource", rm.SHARED, callback) exclusiveReq1 = rm._registerResource("string", "resource", rm.EXCLUSIVE, callback) sharedReq3 = rm._registerResource("string", "resource", rm.SHARED, callback) sharedReq4 = rm._registerResource("string", "resource", rm.SHARED, callback) assert not sharedReq1.granted() assert not sharedReq2.granted() assert not exclusiveReq1.granted() assert not sharedReq3.granted() assert not sharedReq4.granted() exclusiveReq1.cancel() resources.pop() assert not sharedReq1.granted() assert not sharedReq2.granted() assert not exclusiveReq1.granted() assert exclusiveReq1.canceled() assert not sharedReq3.granted() assert not sharedReq4.granted() exclusive1.release() assert sharedReq1.granted() assert sharedReq2.granted() assert sharedReq3.granted() assert sharedReq4.granted() while len(resources) > 0: resources.pop().release()
def testRequestWithBadCallbackOnCancel(self, tmp_manager): def callback(req, res): raise Exception("BUY MILK!") blocker = rm.acquireResource("string", "resource", rm.EXCLUSIVE) req = rm._registerResource("string", "resource", rm.EXCLUSIVE, callback) req.cancel() blocker.release()
def testRequestWithBadCallbackOnCancel(self): def callback(req, res): raise Exception("BUY MILK!") blocker = rm.acquireResource("string", "resource", rm.EXCLUSIVE) req = rm._registerResource( "string", "resource", rm.EXCLUSIVE, callback) req.cancel() blocker.release()
def testRequestRefStr(self): resources = [] def callback(req, res): resources.insert(0, res) req = rm._registerResource("string", "resource", rm.SHARED, callback) try: str(req) finally: req.wait() resources[0].release()
def testRequestRefStr(self, tmp_manager): resources = [] def callback(req, res): resources.insert(0, res) req = rm._registerResource("string", "resource", rm.SHARED, callback) try: str(req) finally: req.wait() resources[0].release()
def testResourceLockSwitch(self, namespace="string"): resources = [] def callback(req, res): resources.insert(0, res) exclusive1 = rm.acquireResource(namespace, "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource(namespace, "resource", rm.SHARED, callback) sharedReq2 = rm._registerResource(namespace, "resource", rm.SHARED, callback) exclusive2 = rm._registerResource(namespace, "resource", rm.EXCLUSIVE, callback) exclusive3 = rm._registerResource(namespace, "resource", rm.EXCLUSIVE, callback) sharedReq3 = rm._registerResource(namespace, "resource", rm.SHARED, callback) self.assertEqual(exclusive1.read(), "resource:exclusive") exclusive1.release() self.assertEqual(resources[-1].read(), "resource:shared") resources.pop().release() self.assertEqual(resources[-1].read(), "") resources.pop().release() self.assertEqual(resources[-1].read(), "resource:exclusive") resources.pop().release() self.assertEqual(resources[-1].read(), "") resources.pop().release() self.assertEqual(resources[-1].read(), "resource:shared") resources.pop().release() # This part is to stop pyflakes for complaining, the reason I need the # resourcesRefs alive is so that the manage will not autocollect during # the test hash(sharedReq1) hash(sharedReq2) hash(sharedReq3) hash(exclusive2) hash(exclusive3) hash(sharedReq3)
def testFailCreateAfterSwitch(self): resources = [] def callback(req, res): resources.append(res) exclusive1 = rm.acquireResource( "failAfterSwitch", "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource( "failAfterSwitch", "resource", rm.SHARED, callback) exclusive1.release() self.assertTrue(sharedReq1.canceled()) self.assertEqual(resources[0], None)
def testResourceLockSwitch(self, namespace="string"): resources = [] def callback(req, res): resources.insert(0, res) exclusive1 = rm.acquireResource(namespace, "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource( namespace, "resource", rm.SHARED, callback) sharedReq2 = rm._registerResource( namespace, "resource", rm.SHARED, callback) exclusive2 = rm._registerResource( namespace, "resource", rm.EXCLUSIVE, callback) exclusive3 = rm._registerResource( namespace, "resource", rm.EXCLUSIVE, callback) sharedReq3 = rm._registerResource( namespace, "resource", rm.SHARED, callback) self.assertEqual(exclusive1.read(), "resource:exclusive") exclusive1.release() self.assertEqual(resources[-1].read(), "resource:shared") resources.pop().release() self.assertEqual(resources[-1].read(), "") resources.pop().release() self.assertEqual(resources[-1].read(), "resource:exclusive") resources.pop().release() self.assertEqual(resources[-1].read(), "") resources.pop().release() self.assertEqual(resources[-1].read(), "resource:shared") resources.pop().release() # This part is to stop pyflakes for complaining, the reason I need the # resourcesRefs alive is so that the manage will not autocollect during # the test hash(sharedReq1) hash(sharedReq2) hash(sharedReq3) hash(exclusive2) hash(exclusive3) hash(sharedReq3)
def testAccessAttributeNotExposedByRequestRef(self, tmp_manager): resources = [] def callback(req, res): resources.insert(0, res) req = rm._registerResource("string", "resource", rm.SHARED, callback) with pytest.raises(AttributeError): try: req.grant() finally: req.wait() resources[0].release()
def testFailCreateAfterSwitch(self, tmp_manager): resources = [] def callback(req, res): resources.append(res) exclusive1 = rm.acquireResource("failAfterSwitch", "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource("failAfterSwitch", "resource", rm.SHARED, callback) exclusive1.release() assert sharedReq1.canceled() assert resources[0] is None
def testAcquireResourceExclusive(self, tmp_manager): resources = [] def callback(req, res): resources.append(res) exclusive1 = rm.acquireResource("storage", "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource("storage", "resource", rm.SHARED, callback) sharedReq2 = rm._registerResource("storage", "resource", rm.SHARED, callback) exclusiveReq1 = rm._registerResource("storage", "resource", rm.EXCLUSIVE, callback) exclusiveReq2 = rm._registerResource("storage", "resource", rm.EXCLUSIVE, callback) assert not sharedReq1.granted() assert not sharedReq2.granted() assert not exclusiveReq1.granted() assert not exclusiveReq2.granted() exclusive1.release() assert sharedReq1.granted() assert sharedReq2.granted() assert not exclusiveReq1.granted() assert not exclusiveReq2.granted() resources.pop().release() # Shared 1 assert not exclusiveReq1.granted() assert not exclusiveReq2.granted() resources.pop().release() # Shared 2 assert exclusiveReq1.granted() assert not exclusiveReq2.granted() resources.pop().release() # exclusiveReq 1 assert exclusiveReq2.granted() resources.pop().release() # exclusiveReq 2
def testAcquireResourceExclusive(self): resources = [] def callback(req, res): resources.append(res) exclusive1 = rm.acquireResource("storage", "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource( "storage", "resource", rm.SHARED, callback) sharedReq2 = rm._registerResource( "storage", "resource", rm.SHARED, callback) exclusiveReq1 = rm._registerResource( "storage", "resource", rm.EXCLUSIVE, callback) exclusiveReq2 = rm._registerResource( "storage", "resource", rm.EXCLUSIVE, callback) self.assertFalse(sharedReq1.granted()) self.assertFalse(sharedReq2.granted()) self.assertFalse(exclusiveReq1.granted()) self.assertFalse(exclusiveReq2.granted()) exclusive1.release() self.assertTrue(sharedReq1.granted()) self.assertTrue(sharedReq2.granted()) self.assertFalse(exclusiveReq1.granted()) self.assertFalse(exclusiveReq2.granted()) resources.pop().release() # Shared 1 self.assertFalse(exclusiveReq1.granted()) self.assertFalse(exclusiveReq2.granted()) resources.pop().release() # Shared 2 self.assertTrue(exclusiveReq1.granted()) self.assertFalse(exclusiveReq2.granted()) resources.pop().release() # exclusiveReq 1 self.assertTrue(exclusiveReq2.granted()) resources.pop().release() # exclusiveReq 2
def testResourceLockSwitch(self, namespace, tmp_manager): resources = [] def callback(req, res): resources.insert(0, res) exclusive1 = rm.acquireResource(namespace, "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource(namespace, "resource", rm.SHARED, callback) sharedReq2 = rm._registerResource(namespace, "resource", rm.SHARED, callback) exclusive2 = rm._registerResource(namespace, "resource", rm.EXCLUSIVE, callback) exclusive3 = rm._registerResource(namespace, "resource", rm.EXCLUSIVE, callback) sharedReq3 = rm._registerResource(namespace, "resource", rm.SHARED, callback) assert exclusive1.read() == "resource:exclusive" exclusive1.release() assert resources[-1].read() == "resource:shared" resources.pop().release() assert resources[-1].read() == "" resources.pop().release() assert resources[-1].read() == "resource:exclusive" resources.pop().release() assert resources[-1].read() == "" resources.pop().release() assert resources[-1].read() == "resource:shared" resources.pop().release() # Silense flake8 unused local variables warnings. sharedReq1 sharedReq2 exclusive2 exclusive3 sharedReq3
def testResourceLockSwitch(self, namespace="string"): resources = [] def callback(req, res): resources.insert(0, res) exclusive1 = rm.acquireResource(namespace, "resource", rm.EXCLUSIVE) sharedReq1 = rm._registerResource( namespace, "resource", rm.SHARED, callback) sharedReq2 = rm._registerResource( namespace, "resource", rm.SHARED, callback) exclusive2 = rm._registerResource( namespace, "resource", rm.EXCLUSIVE, callback) exclusive3 = rm._registerResource( namespace, "resource", rm.EXCLUSIVE, callback) sharedReq3 = rm._registerResource( namespace, "resource", rm.SHARED, callback) self.assertEqual(exclusive1.read(), "resource:exclusive") exclusive1.release() self.assertEqual(resources[-1].read(), "resource:shared") resources.pop().release() self.assertEqual(resources[-1].read(), "") resources.pop().release() self.assertEqual(resources[-1].read(), "resource:exclusive") resources.pop().release() self.assertEqual(resources[-1].read(), "") resources.pop().release() self.assertEqual(resources[-1].read(), "resource:shared") resources.pop().release() # Silense flake8 unused local variables warnings. sharedReq1 sharedReq2 exclusive2 exclusive3 sharedReq3
def testRequestRecancel(self): resources = [] def callback(req, res): resources.insert(0, res) blocker = rm.acquireResource("string", "resource", rm.EXCLUSIVE) req = rm._registerResource( "string", "resource", rm.EXCLUSIVE, callback) req.cancel() self.assertRaises(rm.RequestAlreadyProcessedError, req.cancel) blocker.release()
def testAccessAttributeNotExposedByRequestRef(self): resources = [] def callback(req, res): resources.insert(0, res) req = rm._registerResource("string", "resource", rm.SHARED, callback) with pytest.raises( AttributeError, message="Managed to access an attribute not exposed by wrapper" ): try: req.grant() finally: req.wait() resources[0].release()
def testAccessAttributeNotExposedByRequestRef(self): resources = [] def callback(req, res): resources.insert(0, res) req = rm._registerResource("string", "resource", rm.SHARED, callback) try: req.grant() except AttributeError: return except Exception as ex: self.fail("Wrong exception was raised. " "Expected AttributeError got %s", ex.__class__.__name__) finally: req.wait() resources[0].release() self.fail("Managed to access an attribute not exposed by wrapper")
def testErrorInFactory(self): req = rm._registerResource( "error", "resource", rm.EXCLUSIVE, lambda req, res: 1) self.assertTrue(req.canceled())
def testErrorInFactory(self, tmp_manager): req = rm._registerResource("error", "resource", rm.EXCLUSIVE, lambda req, res: 1) assert req.canceled()