Пример #1
0
class SteamBoatTest(TestCase):
    def setUp(self):
        def reject_handler(queue, item):
            raise Full

        self._thread_pool_executor = ThreadPoolExecutor(
            3, Queue(2), reject_handler)

        strategy = TestDegradationStrategy()

        cabin = CabinBuilder() \
            .with_name("cabin") \
            .with_executor(self._thread_pool_executor) \
            .with_open_length(1) \
            .with_half_open_length(0.4) \
            .with_closed_length(0.2) \
            .with_failure_ratio_threshold(0.95) \
            .with_failure_count_threshold(1) \
            .with_half_failure_count_threshold(1) \
            .build()
        steamboat = SteamBoat()
        steamboat.set_default_cabin(cabin, strategy)
        self._cabin = cabin
        self._steamboat = steamboat

    def tearDown(self):
        self._thread_pool_executor.shutdown()
        self._cabin.shutdown()

    def testSteamBoat(self):
        @self._steamboat.push_into_cabin("cabin")
        def test_func():
            time.sleep(random.random())

        fs = []
        for ind in range(100):
            fs.append(test_func())

        for f in fs:
            exc = f.exception()
            if exc is not None:
                LOGGER.error("%s" % exc)
            else:
                LOGGER.info("%s", f.result())

        ar = self._steamboat.submit_task("cabin",
                                         lambda: time.sleep(random.random()))
        ar.result()
        LOGGER.info(ar.time_info)
Пример #2
0
def create_executor():
    # 创建Executor
    def reject_handler(queue, item):
        raise Full()

    tpe = ThreadPoolExecutor(
        core_pool_size=3,
        queue=Queue(6),
        reject_handler=reject_handler)
    return tpe
Пример #3
0
    def setUp(self):
        def reject_handler(queue, item):
            raise Full

        self._thread_pool_executor = ThreadPoolExecutor(
            3, Queue(6), reject_handler)

        self._cabin = CabinBuilder() \
            .with_name("cabin") \
            .with_executor(self._thread_pool_executor) \
            .with_timeout(0.5) \
            .with_open_length(10) \
            .with_closed_length(2) \
            .with_half_open_length(3) \
            .with_failure_ratio_threshold(0.8) \
            .with_failure_count_threshold(5) \
            .with_half_failure_count_threshold(2) \
            .with_half_open_probability(0.5) \
            .build()
    def testThreadPoolExecutor(self):
        def reject_handler(queue, task_item):
            LOGGER.debug("reject_handler is invoked")
            queue.put(task_item)
            return task_item.async_result

        def function(ind):
            time.sleep(random.random())
            return "this is %d" % ind

        executor = ThreadPoolExecutor(6, Queue(4), reject_handler)
        futures = []
        for ind in range(15):
            future = executor.submit_task(function, ind)
            futures.append(future)

        for future in futures:
            LOGGER.info(future)
            try:
                LOGGER.info(future.result())
            except:
                LOGGER.info(future.exception())

        executor.shutdown()

        future = executor.submit_task(function, 100)
        LOGGER.info(future.exception())
Пример #5
0
    def setUp(self):
        def reject_handler(queue, item):
            raise Full

        self._thread_pool_executor = ThreadPoolExecutor(
            3, Queue(2), reject_handler)

        strategy = TestDegredationStrategy()

        cabin = CabinBuilder() \
            .with_name("cabin") \
            .with_executor(self._thread_pool_executor) \
            .with_open_length(1) \
            .with_half_open_length(0.4) \
            .with_closed_length(0.2) \
            .with_failure_ratio_threshold(0.95) \
            .with_failure_count_threshold(1) \
            .with_half_failure_count_threshold(1) \
            .build()
        steamboat = SteamBoat()
        steamboat.set_default_cabin(cabin, strategy)
        self._steamboat = steamboat
Пример #6
0
class CabinTest(TestCase):
    def setUp(self):
        def reject_handler(queue, item):
            raise Full

        self._thread_pool_executor = ThreadPoolExecutor(
            3, Queue(6), reject_handler)

        self._cabin = CabinBuilder() \
            .with_name("cabin") \
            .with_executor(self._thread_pool_executor) \
            .with_timeout(0.5) \
            .with_open_length(10) \
            .with_closed_length(2) \
            .with_half_open_length(3) \
            .with_failure_ratio_threshold(0.8) \
            .with_failure_count_threshold(5) \
            .with_half_failure_count_threshold(2) \
            .with_half_open_probability(0.5) \
            .build()

    def testTimeout(self):
        def sleep_for_a_moment(seconds):
            time.sleep(seconds)

        ars = []
        for _ in range(10):
            ar = self._cabin.execute(sleep_for_a_moment, 0.6)
            ars.append(ar)
        for ar in ars:
            LOGGER.info("time info: %s" % ar.time_info)
            exc = ar.exception()
            LOGGER.info("exception: %s(%s)" % (exc.__class__, str(exc)))

    def testCabin(self):
        def err_func(count):
            raise RuntimeError("err_func: %d" % count)

        def func(count):
            return "this is %d" % count

        LOGGER.info("start time: %s" % datetime.datetime.now())
        futures = []
        for ind in range(10):
            future = self._cabin.execute(err_func, ind)
            futures.append(future)
        for future in futures:
            LOGGER.info(future)
            try:
                LOGGER.info(future.result())
            except:
                LOGGER.error(future.exception())

        time.sleep(0.2)

        futures = []
        for ind in range(10):
            future = self._cabin.execute(func, ind)
            futures.append(future)

        for future in futures:
            LOGGER.info(future)
            try:
                LOGGER.info(future.result())
            except:
                LOGGER.error(future.exception())

    def tearDown(self):
        self._thread_pool_executor.shutdown()
        self._cabin.shutdown()