Debian Codegraph @

tests/test_transfer.py

raw
1 # -*- coding: utf-8 -*-
2 # coding=utf-8
3 # --------------------------------------------------------------------------
4 # Copyright (c) Microsoft Corporation. All rights reserved.
5 # Licensed under the MIT License. See License.txt in the project root for
6 # license information.
7 # --------------------------------------------------------------------------
8
9 import os
10 import time
11
12 from azure.datalake.store.core import AzureDLPath
13 from azure.datalake.store.transfer import ADLTransferClient
14 from tests.testing import azure, posix
15
16
17 def test_shutdown(azure):
18 def transfer(adlfs, src, dst, offset, size, blocksize, buffersize, retries=5, shutdown_event=None):
19 while shutdown_event and not shutdown_event.is_set():
20 time.sleep(0.1)
21 return size, None
22
23 client = ADLTransferClient(azure, transfer=transfer, chunksize=1,
24 chunked=False)
25 client.submit('foo', 'bar', 16)
26 client.run(monitor=False)
27 client.shutdown()
28
29 assert client.progress[0].state == 'finished'
30
31
32 def test_submit_and_run(azure):
33 def transfer(adlfs, src, dst, offset, size, blocksize, buffersize, shutdown_event=None):
34 return size, None
35
36 client = ADLTransferClient(azure, transfer=transfer, chunksize=8,
37 chunked=False)
38
39 client.submit('foo', 'bar', 16)
40 client.submit('abc', '123', 8)
41
42 nfiles = len(client.progress)
43 assert nfiles == 2
44 assert len([client.progress[i].chunks for i in range(nfiles)])
45
46 assert all([client.progress[i].state == 'pending' for i in range(nfiles)])
47 assert all([chunk.state == 'pending' for f in client.progress
48 for chunk in f.chunks])
49
50 expected = {('bar', 0), ('bar', 8), ('123', 0)}
51 assert {(chunk.name, chunk.offset) for f in client.progress
52 for chunk in f.chunks} == expected
53
54 client.run()
55
56 assert all([client.progress[i].state == 'finished' for i in range(nfiles)])
57 assert all([chunk.state == 'finished' for f in client.progress
58 for chunk in f.chunks])
59 assert all([chunk.expected == chunk.actual for f in client.progress
60 for chunk in f.chunks])
61
62
63 def test_update_progress(azure):
64 """
65 Upload a 32 bytes file in chunks of 8 and test that the progress is incrementally
66 updated.
67 """
68 calls = []
69
70 def recording_callback(progress, total):
71 calls.append((progress, total))
72
73 def transfer(adlfs, src, dst, offset, size, blocksize, buffersize, shutdown_event=None):
74 return size, None
75
76 client = ADLTransferClient(azure, transfer=transfer, chunksize=8,
77 chunked=True, progress_callback=recording_callback)
78
79 client.submit('foo', AzureDLPath('bar'), 32)
80 client.run()
81
82 assert calls == [(8, 32), (16, 32), (24, 32), (32, 32)]
83
84
85 def test_merge(azure):
86
87 calls = []
88
89 def merge(adlfs, outfile, files, shutdown_event=None, overwrite=False):
90 calls.append(files)
91
92 def transfer(adlfs, src, dst, offset, size, blocksize, buffersize, shutdown_event=None):
93 return size, None
94
95 class XLoaderMock(object):
96 _overwrite = False
97
98 file_size = 32
99 chunk_size = 8
100 client = ADLTransferClient(azure, parent=XLoaderMock(), transfer=transfer, merge=merge,
101 chunksize=chunk_size, chunked=True)
102
103 client.submit('foo', AzureDLPath('bar'), file_size)
104 client.run()
105
106 assert len(calls[0]) == file_size / chunk_size
107
108
109 def test_temporary_path(azure):
110 def transfer(adlfs, src, dst, offset, size, blocksize, buffersize):
111 return size, None
112
113 client = ADLTransferClient(azure, transfer=transfer, chunksize=8,
114 unique_temporary=False)
115 client.submit('foo', AzureDLPath('bar'), 16)
116
117 assert os.path.dirname(posix(client.progress[0].chunks[0].name)) == 'bar.segments'