Ejemplo n.º 1
0
def test_scheduler():
    scheduler = Scheduler("scheduler")
    assert scheduler.get_services_count() == 0
    assert scheduler.get_state() == BaseStates.Idle

    scheduler.start()
    assert scheduler.get_state() == BaseStates.Started

    scheduler.stop()
    assert scheduler.get_state() == BaseStates.Stopped

    scheduler.add_service(TestWorker("test-worker-1"))
    assert scheduler.get_services_count() == 1

    scheduler.add_service(TestWorker("test-worker-2"))
    assert scheduler.get_services_count() == 2
Ejemplo n.º 2
0
def test_os():
    """
    At this point we've tested BaseService, so I expect
    the following to work.
    :return:
    """
    worker_interval = .5
    scheduler_interval = 2

    # A timer that will trigger and call stop on the scheduler
    class Timer(BaseService):
        def __init__(self, service):
            BaseService.__init__(self, "timer")
            self.service = service

        def event_loop(self):
            gevent.sleep(scheduler_interval)
            # gevent.kill(self.service.get_greenlet())
            self.service.stop()

    class TestOutputService(OutputService):
        def __init__(self):
            OutputService.__init__(self, "mock-output-service")
            self.event_loop_ack = False

        def event_loop(self):
            gevent.sleep(.1)
            self.event_loop_ack = True
            # this service will stop as the end of the event loop has been reached
            self.set_state(BaseStates.Stopped)
            self.idle()

    scheduler = Scheduler("scheduler")

    # add services
    test_worker_1 = TestWorker("test-worker-1", worker_interval)
    scheduler.add_service(test_worker_1)
    scheduler.add_service(TestWorker("test-worker-2", worker_interval))
    scheduler.add_service(TestOutputService())
    assert scheduler.get_services_count() == 3

    # test for existence of a service manager
    assert scheduler.get_service_manager() is not None

    # test that a child worker has access to the service_manager proxy
    test_worker_1_service = scheduler.get_service_manager().get_directory_service_proxy().get_service("test-worker-1")
    assert test_worker_1_service is not None
    # assert that the service is exactly as what was started
    assert test_worker_1_service == test_worker_1
    # assert that the service has a reference to the directory proxy
    directory_proxy = test_worker_1.get_directory_service_proxy()
    assert directory_proxy is not None
    assert directory_proxy.get_service_count() == 3
    # assert all the same all around
    assert directory_proxy.get_service("test-worker-1") == test_worker_1_service
    # direct check to make sure
    assert directory_proxy._service_manager_directory == scheduler.get_service_manager()._directory

    # start test timer
    timer = Timer(scheduler)

    # take timing from start to finish
    t1 = time.time()
    gevent.joinall([scheduler.start(), timer.start()])  # blocks
    t2 = time.time()
    t3 = t2 - t1
    assert t3 > scheduler_interval

    # stop was called, check all states
    assert scheduler.get_service_manager().get_state() == BaseStates.Stopped
    assert scheduler.get_state() == BaseStates.Stopped
    for pid_key, pid in scheduler.get_services():
        assert pid.get_state() == BaseStates.Stopped