Exemplo n.º 1
0
 def testTwentySendsSameAddressSameMessageTypeCancelCallbacks(self):
     self.sends = []
     self.fails = []
     hs = HysteresisDelaySender(self.send,
                                hysteresis_min_period = timedelta(milliseconds=2),
                                hysteresis_max_period = timedelta(milliseconds=10),
                                hysteresis_rate = 2)
     intents = [TransmitIntent('addr1', 'msg1')]
     for num in range(20):
         intents.append(TransmitIntent('addr1', 'msg',
                                       onSuccess = self.successfulIntent,
                                       onError = self.failedIntent))
     for each in intents:
         hs.sendWithHysteresis(each)
     # First was sent immediately, all others are delayed
     self.assertEqual(1, len(getattr(self, 'sends', [])))
     self.assertEqual(intents[0], self.sends[0])
     self.assertNotEqual(timedelta(seconds=0), hs.delay.remaining())
     self.assertGreaterEqual(timedelta(milliseconds=10), hs.delay.remaining())
     self.assertLess(timedelta(milliseconds=9), hs.delay.remaining())
     hs.cancelSends('addr1')
     self.assertEqual(1, len(getattr(self, 'sends', [])))
     self.assertEqual(intents[0], self.sends[0])
     self.assertEqual(0, len(getattr(self, 'successes', [])))
     self.assertEqual(20, len(getattr(self, 'fails', [])))
     hs.checkSends()
     self.assertEqual(1, len(getattr(self, 'sends', [])))
     self.assertEqual(intents[0], self.sends[0])
     self.assertEqual(0, len(getattr(self, 'successes', [])))
     self.assertEqual(20, len(getattr(self, 'fails', [])))
Exemplo n.º 2
0
 def testTwentySendsDifferentAddressesCancelOne(self):
     self.sends = []
     hs = HysteresisDelaySender(self.send,
                                hysteresis_min_period = timedelta(milliseconds=2),
                                hysteresis_max_period = timedelta(milliseconds=10),
                                hysteresis_rate = 2)
     intents = [TransmitIntent('addr1', 'msg1')]
     for num in range(20):
         intents.append(TransmitIntent('addr%d'%num, 'msg'))
     for each in intents:
         hs.sendWithHysteresis(each)
     # First was sent immediately, all others are delayed
     self.assertEqual(1, len(getattr(self, 'sends', [])))
     self.assertEqual(intents[0], self.sends[0])
     self.assertNotEqual(timedelta(seconds=0), hs.delay.remaining())
     self.assertGreaterEqual(timedelta(milliseconds=10), hs.delay.remaining())
     self.assertLess(timedelta(milliseconds=9), hs.delay.remaining())
     hs.cancelSends('addr10')
     delayTime = hs.delay.remainingSeconds()
     print('Remaining seconds: %s (%s)'%(delayTime, type(delayTime)))
     sleep(delayTime)
     hs.checkSends()
     self.assertEqual(len(intents)-1, len(getattr(self, 'sends', [])))
     adj = 0
     for num in range(len(intents)):
         if num == 11:
             adj = 1
             continue
         self.assertEqual(intents[num], self.sends[num - adj])
     # Ensure that there are no more send attempts
     sleep(hs.delay.remainingSeconds())
     self.assertEqual(len(intents)-1, len(getattr(self, 'sends', [])))
     sleep(delayTime)
     self.assertEqual(len(intents)-1, len(getattr(self, 'sends', [])))