def test_async_fail(self): l = ["b", "c", "a"] self.patch(log, "err", lambda f: None) class SortFail(Exception): pass with self.assertRaises(SortFail): yield async_sort( l, lambda x: defer.succeed(x) if x != "a" else defer.fail(SortFail("ono"))) self.assertEqual(len(self.flushLoggedErrors(SortFail)), 1) self.assertEqual(l, ["b", "c", "a"])
def _defaultSorter(self, master, builders): timer = metrics.Timer("BuildRequestDistributor._defaultSorter()") timer.start() @defer.inlineCallbacks def key(bldr): # Sort by time of oldest build request time = yield bldr.getOldestRequestTime() if time is None: # for builders that do not have pending buildrequest, we just use large number time = math.inf else: if isinstance(time, datetime): time = time.timestamp() return (time, bldr.name) yield async_sort(builders, key) timer.stop() return builders
def test_async_call(self): l = ["b", "c", "a"] yield async_sort(l, defer.succeed) self.assertEqual(l, ["a", "b", "c"])
def test_sync_call(self): l = ["b", "c", "a"] yield async_sort(l, lambda x: x) return self.assertEqual(l, ["a", "b", "c"])