File tree Expand file tree Collapse file tree 2 files changed +21
-7
lines changed
appengine/taskqueue/pull-counter Expand file tree Collapse file tree 2 files changed +21
-7
lines changed Original file line number Diff line number Diff 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+
5158class 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
Original file line number Diff line number Diff line change 1616
1717from google .appengine .ext import testbed as gaetestbed
1818import main
19+ import mock
1920import 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
You can’t perform that action at this time.
0 commit comments