22
22
import os
23
23
import pytest
24
24
import time
25
+ import uuid
25
26
26
27
from datetime import datetime
27
28
from googleapiclient .discovery import build
@@ -41,17 +42,22 @@ def app():
41
42
return flask .Flask (__name__ )
42
43
43
44
45
+ def unique_job_name (label ):
46
+ return datetime .now ().strftime ('{}-%Y%m%d-%H%M%S-{}' .format (
47
+ label , uuid .uuid4 ().hex ))
48
+
49
+
44
50
def test_run_template_python_empty_args (app ):
45
51
project = PROJECT
46
- job = datetime . now (). strftime ( 'test_run_template_python-%Y%m%d-%H%M%S ' )
52
+ job = unique_job_name ( 'test_run_template_empty ' )
47
53
template = 'gs://dataflow-templates/latest/Word_Count'
48
54
with pytest .raises (HttpError ):
49
55
main .run (project , job , template )
50
56
51
57
52
58
def test_run_template_python (app ):
53
59
project = PROJECT
54
- job = datetime . now (). strftime ( 'test_run_template_python-%Y%m%d-%H%M%S ' )
60
+ job = unique_job_name ( 'test_run_template_python' )
55
61
template = 'gs://dataflow-templates/latest/Word_Count'
56
62
parameters = {
57
63
'inputFile' : 'gs://apache-beam-samples/shakespeare/kinglear.txt' ,
@@ -70,7 +76,7 @@ def test_run_template_http_empty_args(app):
70
76
def test_run_template_http_url (app ):
71
77
args = {
72
78
'project' : PROJECT ,
73
- 'job' : datetime . now (). strftime ( 'test_run_template_url-%Y%m%d-%H%M%S ' ),
79
+ 'job' : unique_job_name ( 'test_run_template_url' ),
74
80
'template' : 'gs://dataflow-templates/latest/Word_Count' ,
75
81
'inputFile' : 'gs://apache-beam-samples/shakespeare/kinglear.txt' ,
76
82
'output' : 'gs://{}/dataflow/wordcount/outputs' .format (BUCKET ),
@@ -84,7 +90,7 @@ def test_run_template_http_url(app):
84
90
def test_run_template_http_data (app ):
85
91
args = {
86
92
'project' : PROJECT ,
87
- 'job' : datetime . now (). strftime ( 'test_run_template_data-%Y%m%d-%H%M%S ' ),
93
+ 'job' : unique_job_name ( 'test_run_template_data' ),
88
94
'template' : 'gs://dataflow-templates/latest/Word_Count' ,
89
95
'inputFile' : 'gs://apache-beam-samples/shakespeare/kinglear.txt' ,
90
96
'output' : 'gs://{}/dataflow/wordcount/outputs' .format (BUCKET ),
@@ -98,7 +104,7 @@ def test_run_template_http_data(app):
98
104
def test_run_template_http_json (app ):
99
105
args = {
100
106
'project' : PROJECT ,
101
- 'job' : datetime . now (). strftime ( 'test_run_template_json-%Y%m%d-%H%M%S ' ),
107
+ 'job' : unique_job_name ( 'test_run_template_json' ),
102
108
'template' : 'gs://dataflow-templates/latest/Word_Count' ,
103
109
'inputFile' : 'gs://apache-beam-samples/shakespeare/kinglear.txt' ,
104
110
'output' : 'gs://{}/dataflow/wordcount/outputs' .format (BUCKET ),
@@ -110,9 +116,17 @@ def test_run_template_http_json(app):
110
116
111
117
112
118
def dataflow_jobs_cancel (job_id ):
113
- # Wait time until a job can be cancelled, as a best effort.
114
- # If it fails to be cancelled, the job will run for ~8 minutes.
115
- time .sleep (5 ) # seconds
119
+ # Wait time until the job can be cancelled.
120
+ state = None
121
+ while state != 'JOB_STATE_RUNNING' :
122
+ job = dataflow .projects ().jobs ().get (
123
+ projectId = PROJECT ,
124
+ jobId = job_id
125
+ ).execute ()
126
+ state = job ['currentState' ]
127
+ time .sleep (1 )
128
+
129
+ # Cancel the Dataflow job.
116
130
request = dataflow .projects ().jobs ().update (
117
131
projectId = PROJECT ,
118
132
jobId = job_id ,
0 commit comments