Repository navigation
Expand file tree
/
Copy pathtest_taskqueue.py
More file actions
163 lines (122 loc) · 4.04 KB
/
Copy pathtest_taskqueue.py
File metadata and controls
163 lines (122 loc) · 4.04 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
import json
import os
import time
from moto import mock_sqs
from six.moves import range
import pytest
import taskqueue
from taskqueue import RegisteredTask, TaskQueue, MockTask, PrintTask, LocalTaskQueue
from taskqueue.paths import ExtractedPath, mkpath
@pytest.fixture(scope='function')
def aws_credentials():
"""Mocked AWS Credentials for moto."""
os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
os.environ['AWS_SECURITY_TOKEN'] = 'testing'
os.environ['AWS_SESSION_TOKEN'] = 'testing'
os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'
@pytest.fixture(scope='function')
def sqs(aws_credentials):
with mock_sqs():
import boto3
client = boto3.client('sqs')
client.create_queue(QueueName='test-pull-queue')
yield client
QURLS = {
'sqs': 'test-pull-queue',
'fq': '/tmp/removeme/taskqueue/fq',
}
PROTOCOL = ('fq', 'sqs')
def getpath(protocol):
global QURLS
qurl = QURLS[protocol]
return mkpath(ExtractedPath(protocol, qurl))
class ExecutePrintTask(RegisteredTask):
def __init__(self):
super(ExecutePrintTask, self).__init__()
def execute(self, wow, wow2):
print(wow + wow2)
return wow + wow2
def test_task_creation():
task = MockTask(this="is", a=[1, 4, 2], simple={"test": "to", "check": 4},
serialization=('i', 's', 's', 'u', 'e', 's'), wow=4, with_kwargs=None)
task.wow = 5
payload = task.payload()
payload = json.dumps(payload)
task_deserialized = MockTask.deserialize(payload)
assert task_deserialized._args == {
"this": "is", "a": [1, 4, 2],
"simple": {"test": "to", "check": 4},
"serialization": ["i", "s", "s", "u", "e", "s"],
"wow": 5,
"with_kwargs": None
}
@pytest.mark.parametrize('protocol', PROTOCOL)
def test_get(sqs, protocol):
path = getpath(protocol)
tq = TaskQueue(path, n_threads=0)
n_inserts = 5
tq.purge()
tq.insert(( PrintTask() for _ in range(n_inserts) ))
for i in range(n_inserts):
t = tq.lease()
tq.delete(t)
@pytest.mark.parametrize('protocol', PROTOCOL)
def test_single_threaded_insertion(sqs, protocol):
path = getpath(protocol)
tq = TaskQueue(path, n_threads=0)
tq.purge()
n_inserts = 5
tq.insert(( PrintTask() for i in range(n_inserts) ))
assert all(map(lambda x: type(x) == PrintTask, tq.list()))
tq.purge()
@pytest.mark.parametrize('protocol', PROTOCOL)
@pytest.mark.parametrize('green', (True, False))
@pytest.mark.parametrize('threads', (1, 2, 10, 20, 40))
def test_multi_threaded_insertion(sqs, protocol, green, threads):
path = getpath(protocol)
tq = TaskQueue(path, n_threads=threads, green=green)
n_inserts = 40
tq.purge()
tq.insert(( PrintTask() for i in range(n_inserts)))
tq.purge()
# def test_multiprocess_upload():
# global QURL
# with TaskQueue(QURL) as tq:
# tq.purge()
# time.sleep(1)
# num_tasks = 1000
# tasks = [ PrintTask(i) for i in range(num_tasks) ]
# taskqueue.upload(QURL, tasks, parallel=4)
# time.sleep(1)
# try:
# assert tq.enqueued == num_tasks
# finally:
# with TaskQueue(QURL) as tq:
# tq.purge()
@pytest.mark.parametrize('protocol', PROTOCOL)
def test_400_errors(sqs, protocol):
path = getpath(protocol)
tq = TaskQueue(path, n_threads=0)
tq.delete('nonexistent')
def test_local_taskqueue():
tq = LocalTaskQueue(parallel=True, progress=False)
tasks = ( MockTask(arg=i) for i in range(20000) )
tq.insert(tasks)
tq = LocalTaskQueue(parallel=1, progress=False)
tasks = ( (ExecutePrintTask(), [i], { 'wow2': 4 }) for i in range(200) )
tq.insert(tasks)
tq = LocalTaskQueue(parallel=True, progress=False)
tasks = ( (ExecutePrintTask(), [i], { 'wow2': 4 }) for i in range(200) )
tq.insert(tasks)
tq = LocalTaskQueue(parallel=True, progress=False)
epts = [ PrintTask(i) for i in range(200) ]
tq.insert(epts)
@pytest.mark.parametrize('protocol', PROTOCOL)
def test_parallel_insert_all(sqs, protocol):
import pathos_issue
path = getpath(protocol)
tq = TaskQueue(path, green=True)
tasks = pathos_issue.crt_tasks(5, 20)
tq.insert(tasks, parallel=2)
tq.purge()