def test28(self): x = self.ForTestingInterface() def next_request(index): if index < 3: return x.sleep.asynchronous(0.1) else: return None sequence = ASyncRequestSequence(next_request) self.assertFalse(sequence.is_finished) sequence.wait() self.assertTrue(sequence.is_finished) result = sequence.result() self.assertEqual(len(result), 3) x.stop()
def test29(self): pool = AsyncRequestsPool() x = self.ForTestingInterface() y = self.ForTestingInterface() sequenced_requests_indices = [] def next_request(index): if index < 4: sequenced_requests_indices.append(index) return x.sleep.asynchronous(0.5) else: return None request1 = ASyncRequestSequence(next_request) request2 = y.sleep.asynchronous(1.0) finished_requests = [] def handle_result(request, index): self.assertTrue(request.is_result_available()) self.assertTrue(request.is_finished) finished_requests.append(index) pool.add_request(request1, handle_result, [1]) pool.add_request(request2, handle_result, [2]) pool.wait() self.assertEquals(len(finished_requests), 1) self.assertEquals(len(pool), 1) self.assertEquals(finished_requests, [2]) self.assertTrue(len(sequenced_requests_indices) > 0) pool.wait() self.assertEquals(len(finished_requests), 2) self.assertEquals(len(pool), 0) x.sleep(0.1) self.assertEquals(sequenced_requests_indices, [0, 1, 2, 3]) self.assertTrue(request1.is_result_available()) self.assertTrue(request2.is_result_available()) self.assertEquals(request1.result(), [0, 0, 0, 0]) self.assertEquals(request2.result(), 0) y.stop() x.stop()