def register(self, queue, project=None): """Register a new queue in the shard catalog. This method should be called whenever a new queue is being created, and will create an entry in the shard catalog for the given queue. After using this method to register the queue in the catalog, the caller should call `lookup()` to get a reference to a storage driver which will allow interacting with the queue's assigned backend shard. :param queue: Name of the new queue to assign to a shard :type queue: six.text_type :param project: Project to which the queue belongs, or None for the "global" or "generic" project. :type project: six.text_type :raises: NoShardFound """ # NOTE(cpp-cabrera): only register a queue if the entry # doesn't exist if not self._catalogue_ctrl.exists(project, queue): # NOTE(cpp-cabrera): limit=0 implies unlimited - select from # all shards shard = select.weighted(self._shards_ctrl.list(limit=0)) if not shard: raise errors.NoShardFound() self._catalogue_ctrl.insert(project, queue, shard['name'])
def test_weighted_boundaries(self): objs = [{'weight': 1, 'name': str(i)} for i in range(3)] for i in range(len(objs)): fixed_gen = lambda x, y: i self.assertEqual(select.weighted(objs, generator=fixed_gen), objs[i])
def test_weighted_returns_last_if_selector_is_sum_minus_one(self): objs = [{'weight': 10, 'name': str(i)} for i in range(10)] sum_weights = sum([o['weight'] for o in objs]) capped_gen = lambda x, y: sum_weights - 1 self.assertEqual(select.weighted(objs, generator=capped_gen), objs[-1])
def test_weighted_returns_first_if_selector_is_zero(self): objs = [{'weight': 10, 'name': str(i)} for i in range(10)] zero_gen = lambda x, y: 0 self.assertEqual(select.weighted(objs, generator=zero_gen), objs[0])
def test_weighted_returns_none_if_selector_oob(self): objs = [{'weight': 10, 'name': str(i)} for i in range(10)] sum_weights = sum([o['weight'] for o in objs]) capped_gen = lambda x, y: sum_weights self.assertIsNone(select.weighted(objs, generator=capped_gen))
def test_weighted_returns_an_object_it_was_given(self): objs = [{'weight': 10, 'name': str(i)} for i in range(10)] ret = select.weighted(objs) self.assertIn(ret, objs)
def test_weighted_ignores_zero_weight_objs(self): objs = [{'weight': 0, 'name': str(i)} for i in range(2)] expect = {'weight': 1, 'name': 'theone'} objs.append(expect) self.assertEqual(select.weighted(objs), expect)
def test_weighted_returns_none_if_objs_have_zero_weight(self): objs = [{'weight': 0, 'name': str(i)} for i in range(2)] self.assertIsNone(select.weighted(objs))
def test_weighted_returns_none_if_no_objs(self): self.assertIsNone(select.weighted([]))