forked from GoogleCloudPlatform/DataflowPythonSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_test.py
More file actions
345 lines (273 loc) · 11.4 KB
/
Copy pathpipeline_test.py
File metadata and controls
345 lines (273 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# Copyright 2016 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests for the Pipeline class."""
import gc
import logging
import unittest
from google.cloud.dataflow.io.iobase import NativeSource
from google.cloud.dataflow.pipeline import Pipeline
from google.cloud.dataflow.pipeline import PipelineOptions
from google.cloud.dataflow.pipeline import PipelineVisitor
from google.cloud.dataflow.pvalue import AsIter
from google.cloud.dataflow.pvalue import SideOutputValue
from google.cloud.dataflow.transforms import CombinePerKey
from google.cloud.dataflow.transforms import Create
from google.cloud.dataflow.transforms import FlatMap
from google.cloud.dataflow.transforms import Flatten
from google.cloud.dataflow.transforms import Map
from google.cloud.dataflow.transforms import PTransform
from google.cloud.dataflow.transforms import Read
from google.cloud.dataflow.transforms.util import assert_that, equal_to
class FakeSource(NativeSource):
"""Fake source returning a fixed list of values."""
class _Reader(object):
def __init__(self, vals):
self._vals = vals
def __enter__(self):
return self
def __exit__(self, exception_type, exception_value, traceback):
pass
def __iter__(self):
for v in self._vals:
yield v
def __init__(self, vals):
self._vals = vals
def reader(self):
return FakeSource._Reader(self._vals)
class PipelineTest(unittest.TestCase):
def setUp(self):
self.runner_name = 'DirectPipelineRunner'
@staticmethod
def custom_callable(pcoll):
return pcoll | FlatMap('+1', lambda x: [x + 1])
# Some of these tests designate a runner by name, others supply a runner.
# This variation is just to verify that both means of runner specification
# work and is not related to other aspects of the tests.
class CustomTransform(PTransform):
def apply(self, pcoll):
return pcoll | FlatMap('+1', lambda x: [x + 1])
class Visitor(PipelineVisitor):
def __init__(self, visited):
self.visited = visited
self.enter_composite = []
self.leave_composite = []
def visit_value(self, value, _):
self.visited.append(value)
def enter_composite_transform(self, transform_node):
self.enter_composite.append(transform_node)
def leave_composite_transform(self, transform_node):
self.leave_composite.append(transform_node)
def test_create(self):
pipeline = Pipeline(self.runner_name)
pcoll = pipeline | Create('label1', [1, 2, 3])
assert_that(pcoll, equal_to([1, 2, 3]))
# Test if initial value is an iterator object.
pcoll2 = pipeline | Create('label2', iter((4, 5, 6)))
pcoll3 = pcoll2 | FlatMap('do', lambda x: [x + 10])
assert_that(pcoll3, equal_to([14, 15, 16]), label='pcoll3')
pipeline.run()
def test_create_singleton_pcollection(self):
pipeline = Pipeline(self.runner_name)
pcoll = pipeline | Create('label', [[1, 2, 3]])
assert_that(pcoll, equal_to([[1, 2, 3]]))
pipeline.run()
def test_read(self):
pipeline = Pipeline(self.runner_name)
pcoll = pipeline | Read('read', FakeSource([1, 2, 3]))
assert_that(pcoll, equal_to([1, 2, 3]))
pipeline.run()
def test_visit_entire_graph(self):
pipeline = Pipeline(self.runner_name)
pcoll1 = pipeline | Create('pcoll', [1, 2, 3])
pcoll2 = pcoll1 | FlatMap('do1', lambda x: [x + 1])
pcoll3 = pcoll2 | FlatMap('do2', lambda x: [x + 1])
pcoll4 = pcoll2 | FlatMap('do3', lambda x: [x + 1])
transform = PipelineTest.CustomTransform()
pcoll5 = pcoll4 | transform
visitor = PipelineTest.Visitor(visited=[])
pipeline.visit(visitor)
self.assertEqual(set([pcoll1, pcoll2, pcoll3, pcoll4, pcoll5]),
set(visitor.visited))
self.assertEqual(set(visitor.enter_composite),
set(visitor.leave_composite))
self.assertEqual(2, len(visitor.enter_composite))
self.assertEqual(visitor.enter_composite[1].transform, transform)
self.assertEqual(visitor.leave_composite[0].transform, transform)
def test_apply_custom_transform(self):
pipeline = Pipeline(self.runner_name)
pcoll = pipeline | Create('pcoll', [1, 2, 3])
result = pcoll | PipelineTest.CustomTransform()
assert_that(result, equal_to([2, 3, 4]))
pipeline.run()
def test_reuse_custom_transform_instance(self):
pipeline = Pipeline(self.runner_name)
pcoll1 = pipeline | Create('pcoll1', [1, 2, 3])
pcoll2 = pipeline | Create('pcoll2', [4, 5, 6])
transform = PipelineTest.CustomTransform()
pcoll1 | transform
with self.assertRaises(RuntimeError) as cm:
pipeline.apply(transform, pcoll2)
self.assertEqual(
cm.exception.message,
'Transform "CustomTransform" does not have a stable unique label. '
'This will prevent updating of pipelines. '
'To clone a transform with a new label use: '
'transform.clone("NEW LABEL").')
def test_reuse_cloned_custom_transform_instance(self):
pipeline = Pipeline(self.runner_name)
pcoll1 = pipeline | Create('pcoll1', [1, 2, 3])
pcoll2 = pipeline | Create('pcoll2', [4, 5, 6])
transform = PipelineTest.CustomTransform()
result1 = pcoll1 | transform
result2 = pcoll2 | transform.clone('new label')
assert_that(result1, equal_to([2, 3, 4]), label='r1')
assert_that(result2, equal_to([5, 6, 7]), label='r2')
pipeline.run()
def test_apply_custom_callable(self):
pipeline = Pipeline(self.runner_name)
pcoll = pipeline | Create('pcoll', [1, 2, 3])
result = pipeline.apply(PipelineTest.custom_callable, pcoll)
assert_that(result, equal_to([2, 3, 4]))
pipeline.run()
def test_transform_no_super_init(self):
class AddSuffix(PTransform):
def __init__(self, suffix):
# No call to super(...).__init__
self.suffix = suffix
def apply(self, pcoll):
return pcoll | Map(lambda x: x + self.suffix)
self.assertEqual(
['a-x', 'b-x', 'c-x'],
sorted(['a', 'b', 'c'] | AddSuffix('-x')))
def test_cached_pvalues_are_refcounted(self):
"""Test that cached PValues are refcounted and deleted.
The intermediary PValues computed by the workflow below contain
one million elements so if the refcounting does not work the number of
objects tracked by the garbage collector will increase by a few millions
by the time we execute the final Map checking the objects tracked.
Anything that is much larger than what we started with will fail the test.
"""
def check_memory(value, count_threshold):
gc.collect()
objects_count = len(gc.get_objects())
if objects_count > count_threshold:
raise RuntimeError(
'PValues are not refcounted: %s, %s' % (
objects_count, count_threshold))
return value
def create_dupes(o, _):
yield o
yield SideOutputValue('side', o)
pipeline = Pipeline('DirectPipelineRunner')
gc.collect()
count_threshold = len(gc.get_objects()) + 10000
biglist = pipeline | Create('oom:create', ['x'] * 1000000)
dupes = (
biglist
| Map('oom:addone', lambda x: (x, 1))
| FlatMap('oom:dupes', create_dupes,
AsIter(biglist)).with_outputs('side', main='main'))
result = (
(dupes.side, dupes.main, dupes.side)
| Flatten('oom:flatten')
| CombinePerKey('oom:combine', sum)
| Map('oom:check', check_memory, count_threshold))
assert_that(result, equal_to([('x', 3000000)]))
pipeline.run()
self.assertEqual(
pipeline.runner.debug_counters['element_counts'],
{
'oom:flatten': 3000000,
('oom:combine/GroupByKey/reify_windows', None): 3000000,
('oom:dupes/oom:dupes', 'side'): 1000000,
('oom:dupes/oom:dupes', None): 1000000,
'oom:create': 1000000,
('oom:addone', None): 1000000,
'oom:combine/GroupByKey/group_by_key': 1,
('oom:check', None): 1,
'assert_that/singleton': 1,
('assert_that/Map(match)', None): 1,
('oom:combine/GroupByKey/group_by_window', None): 1,
('oom:combine/Combine/ParDo(CombineValuesDoFn)', None): 1})
def test_pipeline_as_context(self):
def raise_exception(exn):
raise exn
with self.assertRaises(ValueError):
with Pipeline(self.runner_name) as p:
# pylint: disable=expression-not-assigned
p | Create([ValueError]) | Map(raise_exception)
def test_eager_pipeline(self):
p = Pipeline('EagerPipelineRunner')
self.assertEqual([1, 4, 9], p | Create([1, 2, 3]) | Map(lambda x: x*x))
class DiskCachedRunnerPipelineTest(PipelineTest):
def setUp(self):
self.runner_name = 'DiskCachedPipelineRunner'
def test_cached_pvalues_are_refcounted(self):
# Takes long with disk spilling.
pass
def test_eager_pipeline(self):
# Tests eager runner only
pass
class Bacon(PipelineOptions):
@classmethod
def _add_argparse_args(cls, parser):
parser.add_argument('--slices', type=int)
class Eggs(PipelineOptions):
@classmethod
def _add_argparse_args(cls, parser):
parser.add_argument('--style', default='scrambled')
class Breakfast(Bacon, Eggs):
pass
class PipelineOptionsTest(unittest.TestCase):
def test_flag_parsing(self):
options = Breakfast(['--slices=3', '--style=sunny side up', '--ignored'])
self.assertEquals(3, options.slices)
self.assertEquals('sunny side up', options.style)
def test_keyword_parsing(self):
options = Breakfast(
['--slices=3', '--style=sunny side up', '--ignored'],
slices=10)
self.assertEquals(10, options.slices)
self.assertEquals('sunny side up', options.style)
def test_attribute_setting(self):
options = Breakfast(slices=10)
self.assertEquals(10, options.slices)
options.slices = 20
self.assertEquals(20, options.slices)
def test_view_as(self):
generic_options = PipelineOptions(['--slices=3'])
self.assertEquals(3, generic_options.view_as(Bacon).slices)
self.assertEquals(3, generic_options.view_as(Breakfast).slices)
generic_options.view_as(Breakfast).slices = 10
self.assertEquals(10, generic_options.view_as(Bacon).slices)
with self.assertRaises(AttributeError):
generic_options.slices # pylint: disable=pointless-statement
with self.assertRaises(AttributeError):
generic_options.view_as(Eggs).slices # pylint: disable=expression-not-assigned
def test_defaults(self):
options = Breakfast(['--slices=3'])
self.assertEquals(3, options.slices)
self.assertEquals('scrambled', options.style)
def test_dir(self):
options = Breakfast()
self.assertEquals(
['from_dictionary', 'get_all_options', 'slices', 'style', 'view_as'],
[attr for attr in dir(options) if not attr.startswith('_')])
self.assertEquals(
['from_dictionary', 'get_all_options', 'style', 'view_as'],
[attr for attr in dir(options.view_as(Eggs))
if not attr.startswith('_')])
if __name__ == '__main__':
logging.getLogger().setLevel(logging.DEBUG)
unittest.main()