def test_can_shutdown_slave_only(self): '''Confirm that this TestCase's test infrastructure works as needed. ''' master_store = IMasterStore(Person) slave_store = ISlaveStore(Person) # Both Stores work when pgbouncer is up. master_store.get(Person, 1) slave_store.get(Person, 1) # Slave Store breaks when pgbouncer is torn down. Master Store # is fine. self.pgbouncer_fixture.stop() master_store.get(Person, 2) self.assertRaises(DisconnectionError, slave_store.get, Person, 2)
def test_can_shutdown_slave_only(self): """Confirm that this TestCase's test infrastructure works as needed. """ master_store = IMasterStore(Person) slave_store = ISlaveStore(Person) # Both Stores work when pgbouncer is up. master_store.get(Person, 1) slave_store.get(Person, 1) # Slave Store breaks when pgbouncer is torn down. Master Store # is fine. self.pgbouncer_fixture.stop() master_store.get(Person, 2) self.assertRaises(DisconnectionError, slave_store.get, Person, 2)
def removeAssociation(self, server_url, handle): """See `OpenIDStore`.""" store = IMasterStore(self.Association) assoc = store.get(self.Association, ( server_url.decode('UTF-8'), handle.decode('ASCII'))) if assoc is None: return False store.remove(assoc) return True
def test_useNonce(self): timestamp = time.time() # The nonce can only be used once. self.assertEqual(self.store.useNonce("server-url", timestamp, "salt"), True) storm_store = IMasterStore(self.store.Nonce) new_nonce = storm_store.get(self.store.Nonce, (u"server-url", timestamp, u"salt")) self.assertIsNot(None, new_nonce) self.assertEqual(self.store.useNonce("server-url", timestamp, "salt"), False) self.assertEqual(self.store.useNonce("server-url", timestamp, "salt"), False)
def storeAssociation(self, server_url, association): """See `OpenIDStore`.""" store = IMasterStore(self.Association) db_assoc = store.get( self.Association, (server_url.decode('UTF-8'), association.handle.decode('ASCII'))) if db_assoc is None: db_assoc = self.Association(server_url, association) store.add(db_assoc) else: db_assoc.update(association)
def __call__(self, chunk_size): """Take a batch of targets and update their BugTasks' name caches. See `ITunableLoop`. """ # XXX 2008-03-05 gmb: # We cast chunk_size to an integer to ensure that we're not # trying to slice using floats or anything similarly # foolish. We shouldn't have to do this, but bug #198767 # means that we do. chunk_size = int(chunk_size) start = self.offset end = self.offset + chunk_size chunk = self.candidates[start:end] self.transaction.begin() store = IMasterStore(BugTask) # Transpose the target rows into lists of object IDs to retrieve. ids_to_cache = zip(*(target for (target, names) in chunk)) for index, cls in enumerate(target_classes): # Get all of the objects that we will need into the cache. list(store.find(cls, cls.id.is_in(set(ids_to_cache[index])))) for target_bits, cached_names in chunk: self.offset += 1 # Resolve the IDs to objects, and get the actual IBugTarget. # If the ID is None, don't even try to get an object. target_objects = ( (store.get(cls, id) if id is not None else None) for cls, id in zip(target_classes, target_bits)) target = bug_target_from_key(*target_objects) new_name = target.bugtargetdisplayname cached_names.discard(new_name) # If there are any outdated names cached, update them all in # a single query. if len(cached_names) > 0: self.logger.info( "Updating %r to '%s'." % (tuple(cached_names), new_name)) self.total_updated += len(cached_names) conditions = ( col == id for col, id in zip(target_columns, target_bits)) to_update = store.find( BugTask, BugTask.targetnamecache.is_in(cached_names), *conditions) to_update.set(targetnamecache=new_name) self.logger.info("Checked %i targets." % len(chunk)) self.transaction.commit()
def test_getAssociation_expired(self): lifetime = 600 timestamp = int(time.time()) - 2 * lifetime self.store.storeAssociation("server-url", Association("handle", "secret", timestamp, lifetime, "HMAC-SHA1")) # The association is not returned because it is out of date. # Further more, it is removed from the database. assoc = self.store.getAssociation("server-url", "handle") self.assertEquals(assoc, None) store = IMasterStore(self.store.Association) db_assoc = store.get(self.store.Association, (u"server-url", u"handle")) self.assertEqual(db_assoc, None)
def test_useNonce(self): timestamp = time.time() # The nonce can only be used once. self.assertEqual(self.store.useNonce('server-url', timestamp, 'salt'), True) storm_store = IMasterStore(self.store.Nonce) new_nonce = storm_store.get(self.store.Nonce, (u'server-url', timestamp, u'salt')) self.assertIsNot(None, new_nonce) self.assertEqual(self.store.useNonce('server-url', timestamp, 'salt'), False) self.assertEqual(self.store.useNonce('server-url', timestamp, 'salt'), False)
def get_object_from_master_store(obj): """Return a copy of the given object retrieved from its master Store. Returns the object if it already comes from the relevant master Store. Registered as a trusted adapter, so if the input is security wrapped, so is the result. Otherwise an unwrapped object is returned. """ master_store = IMasterStore(obj) if master_store is not Store.of(obj): obj = master_store.get(obj.__class__, obj.id) if obj is None: return None alsoProvides(obj, IMasterObject) return obj
def test_getAssociation_expired(self): lifetime = 600 timestamp = int(time.time()) - 2 * lifetime self.store.storeAssociation( 'server-url', Association('handle', 'secret', timestamp, lifetime, 'HMAC-SHA1')) # The association is not returned because it is out of date. # Further more, it is removed from the database. assoc = self.store.getAssociation('server-url', 'handle') self.assertEqual(assoc, None) store = IMasterStore(self.store.Association) db_assoc = store.get(self.store.Association, (u'server-url', u'handle')) self.assertEqual(db_assoc, None)
def __call__(self, chunk_size): """Take a batch of targets and update their BugTasks' name caches. See `ITunableLoop`. """ # XXX 2008-03-05 gmb: # We cast chunk_size to an integer to ensure that we're not # trying to slice using floats or anything similarly # foolish. We shouldn't have to do this, but bug #198767 # means that we do. chunk_size = int(chunk_size) start = self.offset end = self.offset + chunk_size chunk = self.candidates[start:end] self.transaction.begin() store = IMasterStore(BugTask) # Transpose the target rows into lists of object IDs to retrieve. ids_to_cache = zip(*(target for (target, names) in chunk)) for index, cls in enumerate(target_classes): # Get all of the objects that we will need into the cache. list(store.find(cls, cls.id.is_in(set(ids_to_cache[index])))) for target_bits, cached_names in chunk: self.offset += 1 # Resolve the IDs to objects, and get the actual IBugTarget. # If the ID is None, don't even try to get an object. target_objects = ( (store.get(cls, id) if id is not None else None) for cls, id in zip(target_classes, target_bits) ) target = bug_target_from_key(*target_objects) new_name = target.bugtargetdisplayname cached_names.discard(new_name) # If there are any outdated names cached, update them all in # a single query. if len(cached_names) > 0: self.logger.info("Updating %r to '%s'." % (tuple(cached_names), new_name)) self.total_updated += len(cached_names) conditions = (col == id for col, id in zip(target_columns, target_bits)) to_update = store.find(BugTask, BugTask.targetnamecache.is_in(cached_names), *conditions) to_update.set(targetnamecache=new_name) self.logger.info("Checked %i targets." % len(chunk)) self.transaction.commit()
def useNonce(self, server_url, timestamp, salt): """See `OpenIDStore`.""" # If the nonce is too far from the present time, it is not valid. if abs(timestamp - time.time()) > nonce.SKEW: return False server_url = server_url.decode('UTF-8') salt = salt.decode('ASCII') store = IMasterStore(self.Nonce) old_nonce = store.get(self.Nonce, (server_url, timestamp, salt)) if old_nonce is not None: # The nonce has already been seen, so reject it. return False # Record the nonce so it can't be used again. store.add(self.Nonce(server_url, timestamp, salt)) return True
def getByID(self, build_id): """See `ISpecificBuildFarmJobSource`.""" store = IMasterStore(LiveFSBuild) return store.get(LiveFSBuild, build_id)