From 421f2e783af773712a5e7a799f7cbaca69ab7a21 Mon Sep 17 00:00:00 2001 From: Haidong Ji Date: Thu, 21 Feb 2019 21:32:22 -0600 Subject: Priority queue parallel job processing done! Not too bad since I worked it out in Java. Good practice of heapq module in Python! A lot of fun :) --- tests/job_queueTest.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 tests/job_queueTest.py (limited to 'tests') diff --git a/tests/job_queueTest.py b/tests/job_queueTest.py new file mode 100644 index 0000000..ac95c35 --- /dev/null +++ b/tests/job_queueTest.py @@ -0,0 +1,28 @@ +import unittest + +from collections import namedtuple + +AssignedJob = namedtuple("AssignedJob", ["worker", "started_at"]) + +from sources.job_queue import assign_jobs, assign_jobs_improved + + +class MyTestCase(unittest.TestCase): + def test(self): + num_workers = 2 + jobs = [1, 2, 3, 4, 5] + result = [AssignedJob(0, 0), AssignedJob(1, 0), AssignedJob(0, 1), AssignedJob(1, 2), AssignedJob(0, 4)] + self.assertListEqual(result, assign_jobs_improved(num_workers, jobs)) + + def test1(self): + num_workers = 4 + jobs = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] + result = [AssignedJob(0, 0), AssignedJob(1, 0), AssignedJob(2, 0), AssignedJob(3, 0), AssignedJob(0, 1), + AssignedJob(1, 1), AssignedJob(2, 1), AssignedJob(3, 1), AssignedJob(0, 2), AssignedJob(1, 2), + AssignedJob(2, 2), AssignedJob(3, 2), AssignedJob(0, 3), AssignedJob(1, 3), AssignedJob(2, 3), + AssignedJob(3, 3), AssignedJob(0, 4), AssignedJob(1, 4), AssignedJob(2, 4), AssignedJob(3, 4)] + self.assertListEqual(result, assign_jobs_improved(num_workers, jobs)) + + +if __name__ == '__main__': + unittest.main() -- cgit v1.2.3