CartoDB / carto-python

CARTO Python client
https://carto.com
BSD 3-Clause "New" or "Revised" License
154 stars 62 forks source link

Fix cancel race condition #92

Closed rafatower closed 6 years ago

rafatower commented 6 years ago

As stated in the NEWS, this PR does 2 things:

I did this because I was a bit fed up of a couple tests failing randomly:

    def test_batch_create(api_key_auth_client_usr):
        sql = BatchSQLClient(api_key_auth_client_usr)

        # Create query
        data = sql.create(BATCH_SQL_SINGLE_QUERY)

        # Update status
        job_id = data['job_id']

        # Cancel if not finished
        try:
            confirmation = sql.cancel(job_id)
        except CartoException:
            pass
        else:
>           assert confirmation == 'cancelled'
E           AssertionError: assert 'running' == 'cancelled'
E             - running
E             + cancelled

https://travis-ci.org/CartoDB/carto-python/jobs/418121501 https://travis-ci.org/CartoDB/carto-python/jobs/418210293 https://travis-ci.org/CartoDB/carto-python/jobs/418210297

rafatower commented 6 years ago

Here's how I reproduced the issue in local:

$ pip install pytest-repeat
$ py.test --count=100 -x -s -v -k 'test_batch_create' tests/test_sql.py
...
tests/test_sql.py::test_batch_create[70/100] PASSED
tests/test_sql.py::test_batch_create[71/100] PASSED
tests/test_sql.py::test_batch_create[72/100] FAILED

======================================================================================================================================== FAILURES ========================================================================================================================================
_______________________________________________________________________________________________________________________________ test_batch_create[72/100] ________________________________________________________________________________________________________________________________

api_key_auth_client_usr = <carto.auth.APIKeyAuthClient object at 0x7f4190b651d0>

    def test_batch_create(api_key_auth_client_usr):
        sql = BatchSQLClient(api_key_auth_client_usr)

        # Create query
        data = sql.create(BATCH_SQL_SINGLE_QUERY)

        # Update status
        job_id = data['job_id']

        # Cancel if not finished
        try:
            confirmation = sql.cancel(job_id)
        except CartoException:
            pass
        else:
>           assert confirmation == 'cancelled'
E           AssertionError: assert 'running' == 'cancelled'
E             - running
E             + cancelled

tests/test_sql.py:74: AssertionError