def test_body(self): worker = Mock(name='worker') x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) x.body() assert x.pool.num_processes == 3 _keep = [Mock(name='req{0}'.format(i)) for i in range(20)] [state.task_reserved(m) for m in _keep] x.body() x.body() assert x.pool.num_processes == 10 state.reserved_requests.clear() x.body() assert x.pool.num_processes == 10 x._last_scale_up = monotonic() - 10000 x.body() assert x.pool.num_processes == 3
def test_update(self): worker = Mock(name='worker') x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) x.worker.consumer.prefetch_multiplier = 1 x.keepalive = -1 assert x.processes == 3 x.scale_up(5) x.update(7, None) assert x.processes == 7 assert x.max_concurrency == 7 x.scale_down(4) x.update(None, 6) assert x.processes == 6 assert x.min_concurrency == 6 x.update(max=300, min=10) x.update(max=300, min=2) x.update(max=None, min=None)
def test_body(self): worker = Mock(name='worker') x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) x.body() self.assertEqual(x.pool.num_processes, 3) for i in range(20): state.reserved_requests.add(i) x.body() x.body() self.assertEqual(x.pool.num_processes, 10) worker.consumer._update_prefetch_count.assert_called() state.reserved_requests.clear() x.body() self.assertEqual(x.pool.num_processes, 10) x._last_scale_up = monotonic() - 10000 x.body() self.assertEqual(x.pool.num_processes, 3) worker.consumer._update_prefetch_count.assert_called()
def test_body(self): worker = Mock(name='worker') x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) x.body() self.assertEqual(x.pool.num_processes, 3) for i in range(20): state.reserved_requests.add(i) x.body() x.body() self.assertEqual(x.pool.num_processes, 10) self.assertTrue(worker.consumer.increment_prefetch_count.called) state.reserved_requests.clear() x.body() self.assertEqual(x.pool.num_processes, 10) x._last_action = monotonic() - 10000 x.body() self.assertEqual(x.pool.num_processes, 3) self.assertTrue(worker.consumer.decrement_prefetch_count.called)
def test_no_negative_scale(self): total_num_processes = [] worker = Mock(name='worker') x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) x.body() # the body func scales up or down _keep = [Mock(name=f'req{i}') for i in range(35)] for req in _keep: state.task_reserved(req) x.body() total_num_processes.append(self.pool.num_processes) for req in _keep: state.task_ready(req) x.body() total_num_processes.append(self.pool.num_processes) assert all(x.min_concurrency <= i <= x.max_concurrency for i in total_num_processes)
def test_no_negative_scale(self): total_num_processes = [] worker = Mock(name='worker') x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) x.body() # the body func scales up or down for i in range(35): state.reserved_requests.add(i) x.body() total_num_processes.append(self.pool.num_processes) for i in range(35): state.reserved_requests.remove(i) x.body() total_num_processes.append(self.pool.num_processes) self.assertTrue( all(x.min_concurrency <= i <= x.max_concurrency for i in total_num_processes))
def test_update_and_force(self): x = autoscale.Autoscaler(self.pool, 10, 3) self.assertEqual(x.processes, 3) x.force_scale_up(5) self.assertEqual(x.processes, 8) x.update(5, None) self.assertEqual(x.processes, 5) x.force_scale_down(3) self.assertEqual(x.processes, 2) x.update(3, None) self.assertEqual(x.processes, 3) x.force_scale_down(1000) self.assertEqual(x.min_concurrency, 0) self.assertEqual(x.processes, 0) x.force_scale_up(1000) x.min_concurrency = 1 x.force_scale_down(1) x.update(max=300, min=10) x.update(max=300, min=2) x.update(max=None, min=None)
def test_update_and_force(self): worker = Mock(name='worker') x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) assert x.processes == 3 x.force_scale_up(5) assert x.processes == 8 x.update(5, None) assert x.processes == 5 x.force_scale_down(3) assert x.processes == 2 x.update(None, 3) assert x.processes == 3 x.force_scale_down(1000) assert x.min_concurrency == 0 assert x.processes == 0 x.force_scale_up(1000) x.min_concurrency = 1 x.force_scale_down(1) x.update(max=300, min=10) x.update(max=300, min=2) x.update(max=None, min=None)
def test_shrink_raises_exception(self): x = autoscale.Autoscaler(self.pool, 10, 3, logger=logger) x.scale_up(3) x._last_action = time() - 10000 x.pool.shrink_raises_exception = True x.scale_down(1)
def test_shrink_raises_exception(self): worker = Mock(name='worker') x = autoscale.Autoscaler(self.pool, 10, 3, worker=worker) x.scale_up(3) x.pool.shrink_raises_exception = True x._shrink(1)
def test_info(self): x = autoscale.Autoscaler(self.pool, 10, 3) info = x.info() self.assertEqual(info['max'], 10) self.assertEqual(info['min'], 3) self.assertEqual(info['current'], 3)