jeudi 24 mars 2016

How to use threads for functional tests of client server application?

I have client and server module, each one can be started by a function. I just need to find a way to run booth in parallel which:

  1. in case of an exception in the client/server would stop the other so the test runner would not stay stuck

  2. in case of an exception in client/server would print the exception or propagate it to the runner so I could see it and debug the client/server using the test suite

  3. would preferably use threads for performance reasons

The first tentative with simple threads ended with an ugly os._exit(1) when catching a exception in the run method of the thread (which kills the test runner...)

The second tentative (to try to avoid os._exit()) was with concurrent.futures.ThreadPoolExecutor. It allows to get the exception out of the thread but I still can't find a way to abort the other thread.

with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
    server_future = executor.submit(server)
    client_future = executor.submit(client)

    concurrent.futures.wait([server_future, client_future],
                           return_when=concurrent.futures.FIRST_EXCEPTION)

    if client_future.done() && client_future.exception():
        # we can handle the client exception here
        # but how to stop the server from waiting the client?
        # also, raise is blocking

    if server_future.done() && server_future.exception():
        # same here

  • Is there a way to achieve this with threads?
  • If not with threads, is there a simple way to test a client server app at all? (I think the two first requirements are enough to have a usable solution)

Aucun commentaire:

Enregistrer un commentaire