Skip to content

Commit 48f9559

Browse files
author
Jon Wayne Parrott
committed
Adding test for taskqueue pull worker. Fixes #254 (#349)
1 parent 9fe2db6 commit 48f9559

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

appengine/taskqueue/pull-counter/main.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ def post(self):
4848
self.redirect('/')
4949

5050

51+
@ndb.transactional
52+
def update_counter(key, tasks):
53+
counter = Counter.get_or_insert(key, count=0)
54+
counter.count += len(tasks)
55+
counter.put()
56+
57+
5158
class CounterWorker(webapp2.RequestHandler):
5259
def get(self):
5360
"""Indefinitely fetch tasks and update the datastore."""
@@ -60,20 +67,18 @@ def get(self):
6067
logging.exception(e)
6168
time.sleep(1)
6269
continue
70+
6371
if tasks:
6472
key = tasks[0].tag
6573

66-
@ndb.transactional
67-
def update_counter():
68-
counter = Counter.get_or_insert(key, count=0)
69-
counter.count += len(tasks)
70-
counter.put()
7174
try:
72-
update_counter()
75+
update_counter(key, tasks)
7376
except Exception as e:
7477
logging.exception(e)
75-
else:
78+
raise
79+
finally:
7680
queue.delete_tasks(tasks)
81+
7782
time.sleep(1)
7883

7984

appengine/taskqueue/pull-counter/pullcounter_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from google.appengine.ext import testbed as gaetestbed
1818
import main
19+
import mock
1920
import webtest
2021

2122

@@ -31,3 +32,11 @@ def test_app(testbed):
3132
tasks = tq_stub.get_filtered_tasks()
3233
assert len(tasks) == 1
3334
assert tasks[0].name == 'task1'
35+
36+
with mock.patch('main.update_counter') as mock_update:
37+
# Force update to fail, otherwise the loop will go forever.
38+
mock_update.side_effect = RuntimeError()
39+
40+
app.get('/_ah/start', status=500)
41+
42+
assert mock_update.called

0 commit comments

Comments
 (0)