Skip to content

Commit 3994c54

Browse files
Add local testing infrastructure (#149)
* initial commit. This will likely need to be updated to work in ci * pull redis host from environment, use requirements file * formatting * downgrade pytest so it still supports python 2.7
1 parent 743b7c3 commit 3994c54

File tree

10 files changed

+68
-17
lines changed

10 files changed

+68
-17
lines changed

.circleci/config.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ workflows:
1212

1313
defaults: &defaults
1414
working_directory: ~/code
15+
environment:
16+
# circleci exposes services on localhost
17+
REDIS_HOST: localhost
1518
steps:
1619
- checkout
1720
- run:
1821
name: Install dependencies
19-
command: sudo pip install -r requirements.txt freezefrog psutil pytest
22+
command: sudo pip install -r requirements.txt -r requirements-test.txt
2023
- run:
2124
name: Test
2225
command: pytest

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM circleci/python:3.6
2+
3+
WORKDIR /src
4+
COPY requirements.txt .
5+
COPY requirements-test.txt .
6+
RUN pip install --user -r requirements.txt -r requirements-test.txt

README.rst

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,3 +718,19 @@ reported to Rollbar. Here is a custom worker launch script:
718718
tiger.log.addHandler(rollbar_handler)
719719
720720
tiger.run_worker_with_args(sys.argv[1:])
721+
722+
723+
Running The Test Suite
724+
----------------------
725+
726+
Tests can be run locally using the provided docker compose file. After installing docker, tests should be runnable with:
727+
728+
.. code :: bash
729+
730+
docker-compose run --rm tasktiger pytest
731+
732+
Tests can be more granularly run using normal pytest flags. For example:
733+
734+
.. code :: bash
735+
736+
docker-compose run --rm tasktiger pytest tests/test_base.py::TestCase

docker-compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: "3.7"
2+
services:
3+
redis:
4+
image: redis:4.0.6
5+
expose:
6+
- 6379
7+
tasktiger:
8+
build:
9+
context: .
10+
dockerfile: Dockerfile
11+
environment:
12+
REDIS_HOST: redis
13+
volumes:
14+
- .:/src
15+
depends_on:
16+
- redis

requirements-test.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
freezefrog==0.3.2
2+
psutil==5.6.3
3+
pytest==4.6.5

tests/config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import os
2+
13
# How much to delay scheduled tasks for testing purposes.
24
DELAY = 0.2
35

46
# Redis database number which will be wiped and used for the tests
5-
TEST_DB = 7
7+
TEST_DB = int(os.environ.get('REDIS_DB', 7))
8+
9+
# Redis hostname
10+
REDIS_HOST = os.environ.get('REDIS_HOST', 'localhost')

tests/tasks.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from tasktiger import RetryException
88
from tasktiger.retry import fixed
99

10-
from .config import DELAY, TEST_DB
10+
from .config import DELAY, TEST_DB, REDIS_HOST
1111
from .utils import get_tiger
1212

1313

@@ -52,28 +52,28 @@ def long_task_killed():
5252
@tiger.task(hard_timeout=DELAY * 2)
5353
def long_task_ok():
5454
# Signal task has started
55-
conn = redis.Redis(db=TEST_DB, decode_responses=True)
55+
conn = redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
5656
conn.lpush(LONG_TASK_SIGNAL_KEY, '1')
5757

5858
time.sleep(DELAY)
5959

6060

6161
def wait_for_long_task():
6262
"""Waits for a long task to start."""
63-
conn = redis.Redis(db=TEST_DB, decode_responses=True)
63+
conn = redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
6464
result = conn.blpop(LONG_TASK_SIGNAL_KEY, int(ceil(DELAY * 3)))
6565
assert result[1] == '1'
6666

6767

6868
@tiger.task(unique=True)
6969
def unique_task(value=None):
70-
conn = redis.Redis(db=TEST_DB, decode_responses=True)
70+
conn = redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
7171
conn.lpush('unique_task', value)
7272

7373

7474
@tiger.task(lock=True)
7575
def locked_task(key, other=None):
76-
conn = redis.Redis(db=TEST_DB, decode_responses=True)
76+
conn = redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
7777
data = conn.getset(key, 1)
7878
if data is not None:
7979
raise Exception('task failed, key already set')
@@ -83,7 +83,7 @@ def locked_task(key, other=None):
8383

8484
@tiger.task(queue='batch', batch=True)
8585
def batch_task(params):
86-
conn = redis.Redis(db=TEST_DB, decode_responses=True)
86+
conn = redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
8787
try:
8888
conn.rpush('batch_task', json.dumps(params))
8989
except Exception:
@@ -94,7 +94,7 @@ def batch_task(params):
9494

9595
@tiger.task(queue='batch')
9696
def non_batch_task(arg):
97-
conn = redis.Redis(db=TEST_DB, decode_responses=True)
97+
conn = redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
9898
conn.rpush('batch_task', arg)
9999
if arg == 10:
100100
raise Exception('exception')
@@ -109,7 +109,7 @@ def retry_task_2():
109109

110110

111111
def verify_current_task():
112-
conn = redis.Redis(db=TEST_DB, decode_responses=True)
112+
conn = redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
113113

114114
try:
115115
tiger.current_tasks
@@ -121,7 +121,7 @@ def verify_current_task():
121121

122122
@tiger.task(batch=True, queue='batch')
123123
def verify_current_tasks(tasks):
124-
conn = redis.Redis(db=TEST_DB, decode_responses=True)
124+
conn = redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
125125

126126
try:
127127
tasks = tiger.current_task

tests/tasks_periodic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from tasktiger.schedule import periodic
66

7-
from .config import TEST_DB
7+
from .config import TEST_DB, REDIS_HOST
88
from .utils import get_tiger
99

1010
tiger = get_tiger()
@@ -13,7 +13,7 @@
1313
@tiger.task(schedule=periodic(seconds=1), queue='periodic')
1414
def periodic_task():
1515
"""Periodic task."""
16-
conn = redis.Redis(db=TEST_DB, decode_responses=True)
16+
conn = redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
1717
conn.incr('period_count', 1)
1818

1919

tests/test_context_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from .tasks import exception_task, simple_task
77
from .test_base import BaseTestCase
8-
from .config import TEST_DB
8+
from .config import TEST_DB, REDIS_HOST
99

1010

1111
class ContextManagerTester(object):
@@ -17,7 +17,9 @@ class ContextManagerTester(object):
1717

1818
def __init__(self, name):
1919
self.name = name
20-
self.conn = redis.Redis(db=TEST_DB, decode_responses=True)
20+
self.conn = redis.Redis(
21+
host=REDIS_HOST, db=TEST_DB, decode_responses=True
22+
)
2123
self.conn.set('cm:{}:enter'.format(self.name), 0)
2224
self.conn.set('cm:{}:exit'.format(self.name), 0)
2325

tests/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import structlog
44
from tasktiger import TaskTiger, Worker, fixed
55

6-
from .config import DELAY, TEST_DB
6+
from .config import DELAY, TEST_DB, REDIS_HOST
77

88
TEST_TIGER_CONFIG = {
99
# We need this 0 here so we don't pick up scheduled tasks when
@@ -51,7 +51,7 @@ def setup_structlog():
5151

5252

5353
def get_redis():
54-
return redis.Redis(db=TEST_DB, decode_responses=True)
54+
return redis.Redis(host=REDIS_HOST, db=TEST_DB, decode_responses=True)
5555

5656

5757
def get_tiger():

0 commit comments

Comments
 (0)