-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathtest_jobinspect.py
More file actions
144 lines (101 loc) · 4.68 KB
/
Copy pathtest_jobinspect.py
File metadata and controls
144 lines (101 loc) · 4.68 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
import time
import ujson as json
import urllib2
import os
import pytest
def test_current_job_inspect(worker):
worker.start(flags="--trace_io")
job_id = worker.send_task(
"tests.tasks.general.MongoInsert", {"a": 41, "b": 1, "sleep": 3}, block=False)
time.sleep(1)
# Test the HTTP admin API
admin_worker = json.load(urllib2.urlopen("http://localhost:20020"))
assert admin_worker["status"] == "full"
assert len(admin_worker["jobs"]) == 1
# And now the $1M feature: check which function call is currently running!
assert "sleep(" in "\n".join(admin_worker["jobs"][0]["stack"])
assert "tests/tasks/general.py" in "\n".join(
admin_worker["jobs"][0]["stack"])
# print "STACK", "\n".join(admin_worker["jobs"][0]["stack"])
assert admin_worker["jobs"][0]["id"] == str(job_id)
time.sleep(3)
admin_worker = json.load(urllib2.urlopen("http://localhost:20020"))
assert admin_worker["status"] == "wait"
assert len(admin_worker["jobs"]) == 0
assert admin_worker["done_jobs"] == 1
assert len(admin_worker["io"]["types"]) > 0
assert admin_worker["io"]["tasks"][0][0] == "tests.tasks.general.MongoInsert"
assert admin_worker["io"]["tasks"][0][1] > 0
assert admin_worker["io"]["total"] > 0
@pytest.mark.parametrize(["p_testtype", "p_testparams", "p_type", "p_data"], [
["mongodb-insert", {"a": 41, "b": 1}, "mongodb.insert", {'collection': 'mrq.tests_inserts'}],
["mongodb-find", {"a": 41, "b": 1}, "mongodb.cursor", {'collection': 'mrq.tests_inserts'}],
["mongodb-count", {"a": 41, "b": 1}, "mongodb.count", {'collection': 'mrq.tests_inserts'}],
["redis-llen", {"key": "test:key"}, "redis.llen", {"key": "test:key"}],
["redis-lpush", {"key": "test:key"}, "redis.lpush", {"key": "test:key"}],
["urllib2-get", {'url': 'HTTPBIN/?y=z'}, "http.get", {'url': 'HTTPBIN/?y=z'}],
["urllib2-get", {'url': 'HTTPSBIN/?y=z'}, "http.get", {'url': 'HTTPSBIN/?y=z'}],
["requests-get", {'url': 'HTTPBIN/?y=z'}, "http.get", {'url': 'HTTPBIN/?y=z'}],
["requests-get", {'url': 'HTTPSBIN/?y=z'}, "http.get", {'url': 'HTTPSBIN/?y=z'}],
])
def test_current_job_trace_io(worker, p_testtype, p_testparams, p_type, p_data, httpbin, httpbin_secure):
if "url" in p_testparams:
p_testparams["url"] = p_testparams["url"].replace("HTTPBIN", httpbin.url)
p_testparams["url"] = p_testparams["url"].replace("HTTPSBIN", httpbin_secure.url)
if "url" in p_data:
p_data["url"] = p_data["url"].replace("HTTPBIN", httpbin.url)
p_data["url"] = p_data["url"].replace("HTTPSBIN", httpbin_secure.url)
report_file = "/tmp/mrq_test_worker_report.json"
if os.path.isfile(report_file):
os.remove(report_file)
worker.start(flags="--trace_io --no_mongodb_ensure_indexes --add_network_latency=0.2 --report_interval=0.1 --report_file=%s" % report_file)
worker.send_task(
"tests.tasks.io.TestIo",
{"test": p_testtype, "params": p_testparams},
block=False
)
io = False
for i in range(0, 1000):
# Get the worker status via the report_file. Because of the network latency we can't use
# the HTTP admin.
if os.path.isfile(report_file):
with open(report_file, "rb") as f:
try:
read = f.read()
admin_worker = json.loads(read)
except:
admin_worker = {}
if len(admin_worker.get("jobs", [])) > 0:
io = admin_worker["jobs"][0].get("io")
print io
# Don't take MRQ's IOs as regular IO
if io:
if io["type"] == "mongodb" and io["data"]["collection"] in ["mrq.mrq_jobs", "mrq.mrq_logs"]:
io = False
else:
break
time.sleep(0.05)
print io
assert io
assert io["type"] == p_type
assert io["data"] == p_data
def test_trace_long_fetch(worker, httpbin):
report_file = "/tmp/mrq_test_worker_report.json"
if os.path.isfile(report_file):
os.remove(report_file)
worker.start(flags="--trace_io --report_interval=0.1 --report_file=%s" % report_file)
worker.send_task(
"tests.tasks.io.TestIo",
{"test": "urllib2-get", "params": {
"url": "%s/delay/10" % httpbin.url
}},
block=False
)
time.sleep(5)
with open(report_file, "rb") as f:
read = f.read()
admin_worker = json.loads(read)
# Test the HTTP admin API
# admin_worker = json.load(urllib2.urlopen("http://localhost:20020/"))
assert admin_worker["jobs"][0]["io"]["type"] == "http.get"
time.sleep(5)