Unittesting a function containing an infinite loop
<p>Given a function handling requests on a connection the body of the function is a infinite loop:</p>

<pre><code>def handle_connection():
    # initialize stuff
    ...

    while True:
        # stuff to get the request
        ...

        # stuff to handle the request
        ...
</code></pre>

<p>How would I unittest this function?</p>

<p>You can limit it to run only once while testing, like:</p>

<pre><code>a = 0
while True and not a:
    # do your stuff
    a = 1
</code></pre>

<p>that will not require you to change indentation,</p>

<p>or output specific content while running to make sure it gets the right values into the variables while running:</p>

<pre><code>while True:
    # get request
    print(request)
    # interact with request
    print(data_achieved)
</code></pre>

<p>which will save you adding a variable.</p>

python unit-testing