Skip to content

Commit 1535988

Browse files
committed
Fix for issue #5
Job creation should not make two rest calls to Job Server.
1 parent 32e6c5b commit 1535988

File tree

5 files changed

+43
-32
lines changed

5 files changed

+43
-32
lines changed

run_functional_tests.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
export TESTSJS_SPARKJOB_SERVER_URL=${DEMETER_SPARKJOB_SERVER_URL:-http://localhost:8090}
3+
export TESTSJS_SPARKJOB_SERVER_URL=${TESTSJS_SPARKJOB_SERVER_URL:-http://localhost:8090}
44
export TESTSJS_SPARK_TEST_CTX=${TESTSJS_SPARK_TEST_CTX:-functional}
55
export TESTSJS_SPARKJOB_JAR_URL=${TESTSJS_SPARKJOB_JAR_URL:-http://localhost/job-server-tests_2.10-0.6.1-SNAPSHOT.jar}
66
export TESTSJS_SPARKJOB_JARS_DIR=/tmp/testsjs_sparkjob_jars

sjsclient/job.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ def create(self, app, class_path, conf=None, ctx=None):
5656
params['context'] = ctx.name
5757

5858
resp = self.client._post(url, data=conf, params=params).json()
59-
return self.get(resp['result']['jobId'])
59+
result = {'status': resp['status']}
60+
result.update(resp['result'])
61+
return self._create_resource(result)
6062

6163
def get(self, job_id):
6264
"""Get a specific Job. This returns more information than create.

sjsclient/tests/functional/test_job.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ def test_job_create(self):
3434
class_path = "spark.jobserver.VeryShortDoubleJob"
3535
job = self._create_job(test_app, class_path,
3636
ctx=self._get_functional_context())
37-
created_job = self.client.jobs.get(job.jobId)
38-
self.assertEqual(job.jobId, created_job.jobId)
39-
status = created_job.status
40-
self.assertTrue(status == "RUNNING" or status == "FINIHSED")
41-
self._wait_till_job_is_done(created_job)
37+
time.sleep(3)
38+
self.assertTrue(len(job.jobId) > 0)
39+
self.assertTrue(job.status == "STARTED")
40+
self._wait_till_job_is_done(job)
4241

4342
def test_job_result(self):
4443
(app_name, test_app) = self._create_app()
4544
class_path = "spark.jobserver.VeryShortDoubleJob"
4645
job = self.client.jobs.create(test_app, class_path,
4746
ctx=self._get_functional_context())
47+
time.sleep(3)
4848
self._wait_till_job_is_done(job)
4949
job = self.client.jobs.get(job.jobId)
5050
self.assertEqual("FINISHED", job.status)
@@ -57,6 +57,7 @@ def test_job_result_with_conf(self):
5757
job = self._create_job(test_app, class_path,
5858
conf=conf,
5959
ctx=self._get_functional_context())
60+
time.sleep(3)
6061
created_job = self.client.jobs.get(job.jobId)
6162
self.assertEqual(job.jobId, created_job.jobId)
6263
status = created_job.status
@@ -79,6 +80,7 @@ def test_job_delete(self):
7980
job = self.client.jobs.create(test_app, class_path,
8081
conf=conf,
8182
ctx=self._get_functional_context())
83+
time.sleep(3)
8284
resp = self.client.jobs.delete(job.jobId)
8385
self.assertEqual(200, resp.status_code)
8486
resp = resp.json()
@@ -93,6 +95,7 @@ def test_job_delete_completed_job(self):
9395
class_path = "spark.jobserver.VeryShortDoubleJob"
9496
job = self.client.jobs.create(test_app, class_path,
9597
ctx=self._get_functional_context())
98+
time.sleep(3)
9699
self._wait_till_job_is_done(job)
97100
self.assertRaises(exceptions.NotFoundException,
98101
self.client.jobs.delete, job.jobId)

sjsclient/tests/unit/test_job.py

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
job_id = "65efab5c-8b75-4454-a42c-882ff4c48786"
1212
start_time = "2015-04-13T12:25:57.691+05:30"
1313
duration = "10.68 secs"
14-
status = "STARTED"
14+
started = "STARTED"
15+
finished = "FINISHED"
16+
running = "RUNNING"
1517
context = "test-context"
1618
class_path = "test.class.path"
1719

@@ -23,7 +25,7 @@
2325
"context": "%s"
2426
}
2527
}
26-
""" % (status, job_id, context)
28+
""" % (started, job_id, context)
2729

2830
job_get_result_response = """
2931
{
@@ -35,7 +37,7 @@
3537
"jobId": "%s",
3638
"result": [1, 2, 3]
3739
}
38-
""" % (duration, class_path, start_time, context, status, job_id)
40+
""" % (duration, class_path, start_time, context, finished, job_id)
3941

4042
job_get_status_not_found_response = """
4143
{
@@ -53,7 +55,7 @@
5355
"status": "%s",
5456
"jobId": "%s"
5557
}
56-
""" % (duration, class_path, start_time, context, status, job_id)
58+
""" % (duration, class_path, start_time, context, running, job_id)
5759

5860
job_list_response = """
5961
[{
@@ -64,7 +66,7 @@
6466
"status": "%s",
6567
"jobId": "%s"
6668
}]
67-
""" % (duration, class_path, start_time, context, status, job_id)
69+
""" % (duration, class_path, start_time, context, running, job_id)
6870

6971
job_delete_response = """
7072
{
@@ -90,7 +92,7 @@ def setUp(self):
9092
super(TestJob, self).setUp()
9193
self.client = client.Client(self.TEST_ENDPOINT)
9294

93-
def assertJobFields(self, test_job):
95+
def assertJobFields(self, test_job, status):
9496
self.assertEqual(job_repr, repr(test_job))
9597
self.assertIsInstance(test_job, job.Job)
9698
self.assertEqual(duration, test_job.duration)
@@ -112,7 +114,7 @@ def test_job_list(self, mock_req):
112114
mock_req.get(list_url, text=job_list_response)
113115
job_list = self.client.jobs.list()
114116
test_job = next(job_list)
115-
self.assertJobFields(test_job)
117+
self.assertJobFields(test_job, running)
116118

117119
@requests_mock.Mocker()
118120
def test_create(self, mock_req):
@@ -121,19 +123,20 @@ def test_create(self, mock_req):
121123
query = "?classPath=test.class.path&appName=test_app"
122124
post_url = "{}{}".format(post_url, query)
123125

124-
get_url = utils.urljoin(self.TEST_ENDPOINT,
125-
self.client.jobs.base_path,
126-
job_id)
127-
128126
mock_req.post(post_url, text=job_create_response)
129-
mock_req.get(get_url, text=job_get_result_response)
130127

131128
test_job = self.client.jobs.create(FakeApp, "test.class.path")
132129
self.assertEqual(context, test_job.context)
133-
self.assertEqual(status, test_job.status)
130+
self.assertEqual(started, test_job.status)
134131
self.assertEqual(job_id, test_job.jobId)
135-
self.assertEqual("STARTED", test_job.status)
136-
self.assertEqual([1, 2, 3], test_job.result)
132+
133+
get_url = utils.urljoin(self.TEST_ENDPOINT,
134+
self.client.jobs.base_path,
135+
job_id)
136+
mock_req.get(get_url, text=job_get_result_response)
137+
test_job_result = self.client.jobs.get(test_job.jobId)
138+
self.assertEqual(finished, test_job_result.status)
139+
self.assertEqual([1, 2, 3], test_job_result.result)
137140

138141
@requests_mock.Mocker()
139142
def test_create_with_ctx(self, mock_req):
@@ -143,20 +146,22 @@ def test_create_with_ctx(self, mock_req):
143146
"context=test-context")
144147
post_url = "{}{}".format(post_url, query)
145148

146-
get_url = utils.urljoin(self.TEST_ENDPOINT,
147-
self.client.jobs.base_path,
148-
job_id)
149-
150149
mock_req.post(post_url, text=job_create_response)
151-
mock_req.get(get_url, text=job_get_result_response)
152150

153151
test_job = self.client.jobs.create(FakeApp, "test.class.path",
154152
ctx=FakeContext)
155153
self.assertEqual(context, test_job.context)
156-
self.assertEqual(status, test_job.status)
154+
self.assertEqual(started, test_job.status)
157155
self.assertEqual(job_id, test_job.jobId)
158-
self.assertEqual("STARTED", test_job.status)
159-
self.assertEqual([1, 2, 3], test_job.result)
156+
157+
get_url = utils.urljoin(self.TEST_ENDPOINT,
158+
self.client.jobs.base_path,
159+
job_id)
160+
161+
mock_req.get(get_url, text=job_get_result_response)
162+
test_job_result = self.client.jobs.get(test_job.jobId)
163+
self.assertEqual(finished, test_job_result.status)
164+
self.assertEqual([1, 2, 3], test_job_result.result)
160165

161166
@requests_mock.Mocker()
162167
def test_get(self, mock_req):
@@ -169,7 +174,7 @@ def test_get(self, mock_req):
169174
mock_req.get(get_url, text=job_get_result_response)
170175
mock_req.get(status_url, text=job_status_response)
171176
test_job = self.client.jobs.get(job_id)
172-
self.assertJobFields(test_job)
177+
self.assertJobFields(test_job, running)
173178

174179
@requests_mock.Mocker()
175180
def test_get_after_sjs_restart(self, mock_req):
@@ -182,7 +187,7 @@ def test_get_after_sjs_restart(self, mock_req):
182187
mock_req.get(get_url, status_code=404)
183188
mock_req.get(status_url, text=job_status_response)
184189
test_job = self.client.jobs.get(job_id)
185-
self.assertJobFields(test_job)
190+
self.assertJobFields(test_job, running)
186191

187192
@requests_mock.Mocker()
188193
def test_get_non_existing(self, mock_req):

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ setenv =
1010
VIRTUAL_ENV={envdir}
1111
deps = -r{toxinidir}/test-requirements.txt
1212
commands = python setup.py test --slowest --testr-args='{posargs}'
13+
passenv = TESTSJS_*
1314

1415
[testenv:pep8]
1516
commands = flake8

0 commit comments

Comments
 (0)