Commits

Marc-Alexandre Chan committed 2e5dcd0

[TEST] EventMock - added start- and end-time tracking, repr

  • Participants
  • Parent commits 9c407fb

Comments (0)

Files changed (1)

File minibot/config/localtest/minibot-setup.py

 import random
 from os.path import dirname, abspath
 from os import chdir
+import time
 
 try:
     current_script = __file__
 random.seed()
 
 class EventMock(object):
-    S_INIT = 0
-    S_START = 1
-    S_RUN = 2
-    S_END = 3
 
     ERR_NONE = 0
     ERR_RETRY = 1
 
     required_res = []
 
-    def __init__(self):
+    def __init__(self, name="Event", start_time=0, duration=0, interval=0,
+        max_runs=0):
         # all of these variables keep track of the event state for debugging
+        self.name = name
+
+        self.start_time=start_time
+        self.duration=duration
+        self.interval=interval
+        self.max_runs = max_runs
+
+        self.run_times = [] # includes start()
+        self.started = None # time started
+        self.ended = None # time ended
         self.run_count = 0
-        self.state = self.S_INIT
+
         self.error_raised = self.ERR_NONE
         self.error_handled = self.ERR_NONE
 
         self.raise_error = self.ERR_NONE
 
     def start(self):
-        self.state = self.S_START
+        self.started = time.time()
 
     def run(self):
+        self.run_times.append(time.time())
         self.run_count += 1
-        self.state = self.S_RUN
         self.error_raised = self.raise_error
         print "run at {}".format(str(datetime.now()))
 
-        if run_count > 10:
-            self.owner.exit()
+        if self.max_runs > 0 and self.run_count == self.max_runs:
+            self.duration = self.interval # to stop the event on next resched
 
         if self.raise_error == self.ERR_RETRY:
             raise ValueError('retry possible')
             raise IndexError('oh god no')
 
     def end(self):
-        self.state = self.S_END
+        self.start_time = time.time()
 
     def handle_exception(self, e):
         if self.error_raised == self.ERR_RETRY:
             self.delay = 4
+            self.raise_error = self.ERR_NONE # don't raise again on next run
             return self.owner.EXC_HANDLED_RETRY
         elif self.error_raised == self.ERR_FINAL:
             return self.owner.EXC_HANDLED_FINAL
         elif self.error_raised == self.ERR_UNHANDLED:
             return self.owner.EXC_UNHANDLED
 
+    def __repr__(self):
+        return "<EventMock:{name}>".format(name=self.name)
+
 def scheduler_msg_events(include_msg_id=None): # yields event mocks with ev.msg.id set
     def generator():
         class MessageMock(object):