Exemple #1
0
def setup_mock_web_api_server(test: TestCase):
    if get_mock_server_mode() == "threading":
        test.server_started = threading.Event()
        test.thread = MockServerThread(test)
        test.thread.start()
        test.server_started.wait()
    else:
        # start a mock server as another process
        target = MockServerProcessTarget()
        test.server_url = "http://localhost:8888"
        test.host, test.port = "localhost", 8888
        test.process = Process(target=target.run, daemon=True)
        test.process.start()

        time.sleep(0.1)

        # start a thread in the current process
        # this thread fetches mock_received_requests from the remote process
        test.monitor_thread = MonitorThread(test)
        test.monitor_thread.start()
        count = 0
        # wait until the first successful data retrieval
        while test.mock_received_requests is None:
            time.sleep(0.01)
            count += 1
            if count >= 100:
                raise Exception("The mock server is not yet running!")
def start_socket_mode_server(test, port: int):
    if get_mock_server_mode() == "threading":
        test.sm_thread = threading.Thread(
            target=start_thread_socket_mode_server(test, port))
        test.sm_thread.daemon = True
        test.sm_thread.start()
        time.sleep(2)  # wait for the server
    else:
        test.sm_process = Process(target=start_process_socket_mode_server,
                                  kwargs={"port": port})
        test.sm_process.start()
def stop_socket_mode_server(test):
    if get_mock_server_mode() == "threading":
        print(test)
        test.server.stop()
        test.server.close()
    else:
        # terminate the process
        test.sm_process.terminate()
        test.sm_process.join()
        # Python 3.6 does not have these methods
        if sys.version_info.major == 3 and sys.version_info.minor > 6:
            # cleanup the process's resources
            test.sm_process.kill()
            test.sm_process.close()

        test.sm_process = None
Exemple #4
0
def cleanup_mock_web_api_server(test: TestCase):
    if get_mock_server_mode() == "threading":
        test.thread.stop()
        test.thread = None
    else:
        # stop the thread to fetch mock_received_requests from the remote process
        test.monitor_thread.stop()

        # terminate the process
        test.process.terminate()
        test.process.join()

        # Python 3.6 does not have these methods
        if sys.version_info.major == 3 and sys.version_info.minor > 6:
            # cleanup the process's resources
            test.process.kill()
            test.process.close()

        test.process = None