File tree Expand file tree Collapse file tree 3 files changed +15
-9
lines changed
Expand file tree Collapse file tree 3 files changed +15
-9
lines changed Original file line number Diff line number Diff line change @@ -40,10 +40,12 @@ def statelessness(request):
4040# [END functions_concepts_stateless]
4141
4242
43+ # Placeholder
4344def heavy_computation ():
4445 return time .time ()
4546
4647
48+ # Placeholder
4749def light_computation ():
4850 return time .time ()
4951
@@ -101,7 +103,8 @@ def timeout(request):
101103 time .sleep (120 )
102104
103105 # May not execute if function's timeout is <2 minutes
104- return 'Done!'
106+ print ('Function completed!' )
107+ return 'Function completed!'
105108# [END functions_concepts_after_timeout]
106109
107110
Original file line number Diff line number Diff line change 2525# [END functions_tips_connection_pooling]
2626
2727
28+ # Placeholder
2829def file_wide_computation ():
29- return sum ( range ( 10 ))
30+ return 1
3031
3132
33+ # Placeholder
3234def function_specific_computation ():
33- return sum ( range ( 10 ))
35+ return 1
3436
3537
3638# [START functions_tips_lazy_globals]
@@ -105,7 +107,8 @@ def avoid_infinite_retries(data, context):
105107 event_age = (datetime .now () - event_time ).total_seconds () * 1000
106108
107109 # Ignore events that are too old
108- if event_age > 10000 :
110+ max_age_ms = 10000
111+ if event_age > max_age_ms :
109112 print ('Dropped {} (age {}ms)' .format (context .event_id , event_age ))
110113 return 'Timeout'
111114
@@ -135,11 +138,11 @@ def retry_or_not(data, context):
135138 try_again = False
136139
137140 try :
138- raise Exception ('I failed you' )
139- except Exception as e :
141+ raise RuntimeError ('I failed you' )
142+ except RuntimeError :
140143 error_client .report_exception ()
141144 if try_again :
142- raise e # Raise the exception and try again
145+ raise # Raise the exception and try again
143146 else :
144- return # Swallow the exception and don't retry
147+ pass # Swallow the exception and don't retry
145148# [END functions_tips_retry]
Original file line number Diff line number Diff line change @@ -84,7 +84,7 @@ def test_retry_or_not():
8484 assert error_client .report_exception .call_count == 1
8585
8686 event .data = {'retry' : True }
87- with pytest .raises (Exception ):
87+ with pytest .raises (RuntimeError ):
8888 main .retry_or_not (event , None )
8989
9090 assert error_client .report_exception .call_count == 2
You can’t perform that action at this time.
0 commit comments