def test_D_ExpireBadOperations(self):
        # make 9 operations, each just append their number to 'results' and
        # then fail by raising an Exception
        # N.B. loopOnce() expects a set

        results = set()

        def bad_do(s):
            results.add(s)
            raise Exception("bad_do")

        def reportNothing(_):
            pass

        def makeOperation(s):
            delays = [datetime.timedelta()]  # expire immediately
            return phlsys_scheduleunreliables.DelayedRetryNotifyOperation(
                functools.partial(bad_do, s), delays, reportNothing)

        data = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
        operations = set([makeOperation(i) for i in data])

        bad_operations = phlsys_scheduleunreliables.make_timed_queue()

        phlsys_scheduleunreliables.loop_once(
            operations, bad_operations)
        self.assertSetEqual(data, results)

        # we should fill the results with the same data again
        results = set()
        phlsys_scheduleunreliables.loop_once(
            operations, bad_operations)
        self.assertSetEqual(data, results)
    def test_C_MakeBadOperations(self):
        # make 10 bad operations which fail immediately
        # N.B. loopOnce() expects a set

        def bad_do():
            raise Exception("bad_do")

        def reportNothing(_):
            pass

        def makeOperation():
            delays = [datetime.timedelta()]  # expire immediately
            return phlsys_scheduleunreliables.DelayedRetryNotifyOperation(
                bad_do, delays, reportNothing)

        num_operations = 10
        operations = set([makeOperation() for _ in range(0, num_operations)])
        self.assertEqual(num_operations, len(operations))

        bad_operations = phlsys_scheduleunreliables.make_timed_queue()
        phlsys_scheduleunreliables.loop_once(
            operations, bad_operations)

        # loopOnce() should have moved all our operations into bad_operations
        # now we will try to extract them, as they have a delay of zero
        self.assertEqual(0, len(operations))
        expired_operations = bad_operations.pop_expired()
        self.assertEqual(num_operations, len(expired_operations))
    def test_E_DropBadOperations(self):
        # make 10 operations, each just fail by raising an Exception.
        # they will have no associated delay and so should not appear in
        # 'operations' or in 'bad_operations'

        num_operations = 10

        def bad_do():
            raise Exception("bad_do")

        def reportNothing(_):
            pass

        def makeOperation():
            delays = []
            return phlsys_scheduleunreliables.DelayedRetryNotifyOperation(
                functools.partial(bad_do), delays, reportNothing)

        # N.B. loopOnce() expects a set
        operations = set([makeOperation() for _ in range(0, num_operations)])
        self.assertEqual(num_operations, len(operations))

        bad_operations = phlsys_scheduleunreliables.make_timed_queue()

        phlsys_scheduleunreliables.loop_once(
            operations, bad_operations)
        self.assertEqual(0, len(operations))
        self.assertEqual(0, len(bad_operations.pop_expired()))
    def test_B_AllOperations(self):
        results = set()

        def do(s):
            results.add(s)

        def makeOperation(s):
            delays = []
            return phlsys_scheduleunreliables.DelayedRetryNotifyOperation(
                functools.partial(do, s), delays)

        # make 9 operations, each just append their number to 'results'
        # N.B. loopOnce() expects a set
        data = set([1, 2, 3, 4, 5, 6, 7, 8, 9])
        operations = set([makeOperation(i) for i in data])

        phlsys_scheduleunreliables.loop_once(
            operations, phlsys_scheduleunreliables.make_timed_queue())

        self.assertSetEqual(data, results)