Commits

Jason R. Coombs  committed a5ac530

PeriodicCommand now raises a ValueError if created with a negative or zero delay. Fixes #12

  • Participants
  • Parent commits 8c7b640
  • Tags 7.0

Comments (0)

Files changed (3)

 7.0
 ===
 
+* PeriodicCommand now raises a ValueError if it's created with a negative or
+  zero delay (meaning all subsequent commands are immediately due). This fixes
+  #12.
 * Renamed the parameters to the IRC object. If you use a custom event loop
   and your code constructs the IRC object with keyword parameters, you will
   need to update your code to use the new names, so::

File irc/client.py

     def due(self):
         return self.now() >= self
 
-class PeriodicCommand(DelayedCommand):
-    """
-    Like a deferred command, but expect this command to run every delay
-    seconds.
-    """
+class PeriodicCommandBase(DelayedCommand):
     def next(self):
         return PeriodicCommand(self.delay, self.function,
             self.arguments)
 
-class PeriodicCommandFixedDelay(PeriodicCommand):
+    def _check_delay(self):
+        if not self.delay > datetime.timedelta():
+            raise ValueError("A PeriodicCommand must have a positive, "
+                "non-zero delay.")
+
+class PeriodicCommand(PeriodicCommandBase):
+    """
+    Like a delayed command, but expect this command to run every delay
+    seconds.
+    """
+    def __init__(self, *args, **kwargs):
+        super(PeriodicCommand, self).__init__(*args, **kwargs)
+        self._check_delay()
+
+class PeriodicCommandFixedDelay(PeriodicCommandBase):
     """
     Like a periodic command, but don't calculate the delay based on
     the current time. Instead use a fixed delay following the initial
     run.
     """
+
     @classmethod
     def at_time(cls, at, delay, function, arguments):
         cmd = super(PeriodicCommandFixedDelay, cls).at_time(
         if not isinstance(delay, datetime.timedelta):
             delay = datetime.timedelta(seconds=delay)
         cmd.delay = delay
+        cmd._check_delay()
         return cmd
 
 _rfc_1459_command_regexp = re.compile("^(:(?P<prefix>[^ ]+) +)?(?P<command>[^ ]+)( *(?P<argument> .+))?")

File irc/tests/test_client.py

 	])
 	assert [c.delay.seconds for c in cmds] == sorted(delays)
 
+def test_periodic_command_delay():
+	"A PeriodicCommand must have a positive, non-zero delay."
+	with pytest.raises(ValueError) as exc_info:
+		irc.client.PeriodicCommand(0, None, None)
+	assert str(exc_info.value) == test_periodic_command_delay.__doc__
+
 def test_periodic_command_fixed_delay():
 	"""
 	Test that we can construct a periodic command with a fixed initial