def test_claim(self): workload_id = uuid.uuid4() t = Thread(42) self.assertEqual(None, t.get_workload_id()) t.claim(workload_id) self.assertEqual(workload_id, t.get_workload_id()) t.free() self.assertEqual(None, t.get_workload_id())
def test_free_unknown(self): t = Thread(42) self.assertEqual(0, len(t.get_workload_ids())) self.assertFalse(t.is_claimed()) t.free("unknown_id") self.assertEqual(0, len(t.get_workload_ids())) self.assertFalse(t.is_claimed()) workload_id = "a" t.claim(workload_id) t.free("unknown_id") self.assertEqual(1, len(t.get_workload_ids())) self.assertEqual(workload_id, t.get_workload_ids()[0]) self.assertTrue(t.is_claimed())
def test_get_empty_threads(self): t0 = Thread(0) t1 = Thread(1) t2 = Thread(2) t3 = Thread(3) c0 = Core(0, [t0, t2]) c1 = Core(1, [t1, t3]) p = Package(1, [c0, c1]) self.assertEqual([t0, t2, t1, t3], p.get_empty_threads()) t1.claim(uuid.uuid4()) self.assertEqual([t0, t2, t3], p.get_empty_threads()) t1.free() self.assertEqual([t0, t2, t1, t3], p.get_empty_threads())
def test_get_emptiest_package(self): t0 = Thread(0) t1 = Thread(1) t2 = Thread(2) t3 = Thread(3) t4 = Thread(4) t5 = Thread(5) t6 = Thread(6) t7 = Thread(7) p0 = Package(0, [ Core(0, [t0, t4]), Core(1, [t1, t5])]) p1 = Package(1, [ Core(0, [t2, t6]), Core(1, [t3, t7])]) cpu = Cpu([p0, p1]) # The first package should be the emptiest self.assertEqual(p0, cpu.get_emptiest_package()) # The second package should be the emptiest after we claim a thread on the first t5.claim(uuid.uuid4()) self.assertEqual(p1, cpu.get_emptiest_package()) # The first package should be the emptiest again, after we release the claimed thread t5.free() self.assertEqual(p0, cpu.get_emptiest_package()) # The first package should be emptiest when we claim a thread on the second t3.claim(uuid.uuid4()) self.assertEqual(p0, cpu.get_emptiest_package()) # When an equal number of threads are claimed on both packages, the first should be returned t4.claim(uuid.uuid4()) self.assertEqual(p0, cpu.get_emptiest_package())
def test_multiple_claims(self): workload_id_a = "a" workload_id_b = "b" t = Thread(42) t.claim(workload_id_a) self.assertEqual(1, len(t.get_workload_ids())) self.assertEqual(workload_id_a, t.get_workload_ids()[0]) self.assertTrue(t.is_claimed()) t.claim(workload_id_b) self.assertEqual(2, len(t.get_workload_ids())) self.assertTrue(workload_id_a in t.get_workload_ids()) self.assertTrue(workload_id_b in t.get_workload_ids()) self.assertTrue(t.is_claimed()) t.free(workload_id_a) self.assertEqual(1, len(t.get_workload_ids())) self.assertEqual(workload_id_b, t.get_workload_ids()[0]) self.assertTrue(t.is_claimed()) t.free(workload_id_b) self.assertFalse(t.is_claimed()) self.assertEqual(0, len(t.get_workload_ids()))