def test_getBridgeDistributor_recognised(self): bridge = self.fakeBridges[0] Storage.initializeDBLock() with Storage.getDB() as db: ringname = db.insertBridgeAndGetRing(bridge, 'moat', time.time(), self.validRings) self.assertIn(ringname, self.validRings) self.assertEqual(ringname, 'moat') db.commit() with Storage.getDB() as db: ringname = db.getBridgeDistributor(bridge, self.validRings) self.assertEqual(ringname, 'moat')
def test_getBridgeDistributor_recognised(self): bridge = self.fakeBridges[0] Storage.initializeDBLock() with Storage.getDB() as db: ringname = db.insertBridgeAndGetRing(bridge, 'moat', time.time(), self.validRings) self.assertIn(ringname, self.validRings) self.assertEqual(ringname, 'moat') db.commit() with Storage.getDB() as db: ringname = db.getBridgeDistributor(bridge, self.validRings) self.assertEqual(ringname, 'moat')
def test_insertBridgeAndGetRing_new_bridge(self): bridge = self.fakeBridges[0] Storage.initializeDBLock() with Storage.getDB() as db: ringname = db.insertBridgeAndGetRing(bridge, 'moat', time.time(), self.validRings) self.assertIn(ringname, self.validRings)
def test_insertBridgeAndGetRing_new_bridge(self): bridge = self.fakeBridges[0] Storage.initializeDBLock() with Storage.getDB() as db: ringname = db.insertBridgeAndGetRing(bridge, 'moat', time.time(), self.validRings) self.assertIn(ringname, self.validRings)
def test_getDB_ConcurrencyLock(self): timeout = 1 d1 = deferToThread(self._runAndDie, timeout, Storage.getDB) d1.addCallback(self._cb_assertFalse) d1.addErrback(self._eb_Failure) d2 = deferToThread(Storage.getDB, False) d2.addCallback(self._cb_assertFalse) d2.addErrback(self._eb_Failure) d2.addCallback(self._cb_assertTrue, Storage.getDB(False))
def test_getDB_ConcurrencyLock(self): timeout = 1 d1 = deferToThread(self._runAndDie, timeout, Storage.getDB) d1.addCallback(self._cb_assertFalse) d1.addErrback(self._eb_Failure) d2 = deferToThread(Storage.getDB, False) d2.addCallback(self._cb_assertFalse) d2.addErrback(self._eb_Failure) d2.addCallback(self._cb_assertTrue, Storage.getDB(False))
def test_fetchBridgeMeasurements(self): query = "INSERT INTO BridgeMeasurements (hex_key, bridge_type, " \ "address, port, blocking_country, blocking_asn, " \ "measured_by, last_measured, verdict) VALUES ('key', " \ "'obfs4', '1.2.3.4', '1234', 'RU', '1234', 'OONI', '%s', 1)" oldMsmt = query % "2017-01-01" newMsmt = query % datetime.datetime.utcnow().strftime("%Y-%m-%d") Storage.initializeDBLock() with Storage.getDB() as db: db._cur.execute(oldMsmt) # We're calling _Database__fetchBridgeMeasurements instead of # __fetchBridgeMeasurements to account for Python's name meddling. rows = db._Database__fetchBridgeMeasurements() # Outdated measurements should not be returned. self.assertEquals(len(rows), 0) db._cur.execute(newMsmt) rows = db._Database__fetchBridgeMeasurements() # Measurements that are "young enough" should be returned. self.assertEquals(len(rows), 1)
def test_getDB_AcquireLock(self): Storage.initializeDBLock() with Storage.getDB() as db: self.assertIsInstance(db, Storage.Database) self.assertTrue(Storage.dbIsLocked()) self.assertEqual(db, Storage._OPENED_DB)
def test_getDB_AcquireLock(self): Storage.initializeDBLock() with Storage.getDB() as db: self.assertIsInstance(db, Storage.Database) self.assertTrue(Storage.dbIsLocked()) self.assertEqual(db, Storage._OPENED_DB)