Package SimPy :: Module testSimPy

[hide private]
[frames] | no frames]

# Source Code for Module SimPy.testSimPy

       1  #!/usr / bin / env python
       2  # coding=utf-8
       3  from SimPy.Simulation  import *
       4  from SimPy.MonitorTest import *
       5  import unittest
       6  from random import random
       7  # $Revision: 437 $ $Date: 2010-04-02 05:48:28 +0200 (Fri, 02 Apr 2010) $
       8  """testSimPy.py
       9  SimPy version 2.1
      10  Unit tests for Simulation.py.
      11
      12  #'$Revision: 437 $ $Date: 2010-04-02 05:48:28 +0200 (Fri, 02 Apr 2010) $ kgm'
      13
      14  """
      15
      16  simulationVersion=version
      17  print "Under test: Simulation.py %s"%simulationVersion
      18  __version__ = '2.1 $Revision: 437 $ $Date: 2010-04-02 05:48:28 +0200 (Fri, 02 Apr 2010) $ '
      19  print 'testSimpy.py %s'%__version__
      20  ## -------------------------------------------------------------
      21  ##                    TEST SIMULATION
      22  ## -------------------------------------------------------------
      23 -class P(Process):
      24     """ P class for testing"""
      25 -   def __init__(self, name = '', T = 0):
      26          Process.__init__(self)
      27          self.name = name
      28          self.T = T
      29
      30 -   def execute(self):
      31          yield hold, self, self.T
      32
      33 -class PActions(Process):
      34     """ PActions class for testing"""
      35 -   def __init__(self, name = '', T = 0):
      36          Process.__init__(self)
      37          self.name = name
      38          self.T = T
      39
      40 -   def ACTIONS(self):
      41          yield hold, self, self.T
      42
      43 -class ToStop(Process):
      44      """For testing stopSimulation
      45      """
      46 -    def run(self,stopTime):
      47          yield hold,self,now()+stopTime
      48          stopSimulation()
      49          yield hold,self,10
      50
      51 -class ToCollect(Process):
      52      """For testing startCollection
      53      """
      54 -    def run(self,mon1,mon2,tal1,tal2):
      55          while True:
      56              yield hold,self,1
      57              mon1.observe(now())
      58              mon2.observe(now())
      59              tal1.observe(now())
      60              tal2.observe(now())
      61
      62 -class ForEvtTimes(Process):
      63      """For testing allEventTimes
      64      """
      65 -    def run(self):
      66          yield hold,self
      67
      68 -class makeSimulationtestcase(unittest.TestCase):
      69     """ Tests of simulation
      70     """
      71 -   def testInit(self):
      72          """Test initialisation
      73          """
      74          initialize()
      75          simulate(until = 10)
      76          assert(now() == 0),'time not 0'
      77
      78 -   def testActivate(self):
      79          """Test activate()
      80          """
      81          P1 = P(name = 'P1', T = 100.0)
      82          initialize()
      83          activate(P1, P1.execute(),0)
      84          simulate(until = 5)
      85          assert(now() == 5),'Simulate stopped at %s not %s' % (now(),5)
      86
      87 -   def testStart(self):
      88         """Test start method
      89         """
      90         P1 = P(name = 'P1', T = 100.0)
      91         initialize()
      92         P1.start(P1.execute(),0)
      93         simulate(until = 5)
      94         assert(now() == 5),'Simulate stopped at %s not %s' % (now(),5)
      95
      96 -   def testStartActions(self):
      97        """Test start method with ACTIONS PEM
      98        """
      99        P1 = PActions(name = 'P1', T = 100.0)
     100        initialize()
     101        P1.start()
     102        simulate(until = 5)
     103        assert(now() == 5),'Simulate stopped at %s not %s' % (now(),5)
     104
     105 -   def testYield(self):
     106          """Test yield hold and simulate(until)
     107          """
     108          P1 = P(name = 'P1', T = 10)
     109          initialize()
     110          activate(P1, P1.execute(),0)
     111          simulate(until = 5)
     112          assert(now() == 5),'Simulate stopped at %s not %s' % (now(),5)
     113          ## should stop at 0 for next event is at 10s
     114          P2 = P(name = 'P2', T = 10)
     115          initialize()
     116          activate(P2, P2.execute(),0)
     117          simulate(until = 20)
     118          assert(now() == 10),'P1 hold to %s not %s' % (now(),10)
     119
     120
     121     ## ------------------------------------------------------------------------
     122     ## Tests of simulation utility functions/methods
     123     ## ------------------------------------------------------------------------
     124
     125 -   def testStopSimulation(self):
     126          """Test stopSimulation function/method
     127          """
     128          timeToStop = 7
     129          initialize()
     130          ts = ToStop()
     131          activate(ts,ts.run(stopTime = timeToStop))
     132          simulate(until = 50)
     133          assert(now()==timeToStop),\
     134          "stopSimulation not working; now = %s instead of %s"%(now(),timeToStop)
     135
     136 -   def testStartCollection(self):
     137         """Test startCollection function/method
     138         """
     139         initialize()
     140         tStart = 9
     141         mon1 = Monitor("mon1")
     142         mon2 = Monitor("mon2")
     143         tal1 = Tally("tal1")
     144         tal2 = Tally("tal2")
     145         startCollection(when = tStart,monitors=[mon1,mon2],tallies=[tal1,tal2])
     146         tc = ToCollect()
     147         activate(tc,tc.run(mon1,mon2,tal1,tal2))
     148         simulate(until=50)
     149         assert(mon1[0]==mon2[0]==[tStart,tStart]),\
     150                "startCollection not working correctly for Monitors"
     151         assert(tal1.count()==tal2.count()==50-tStart+1),\
     152                "startCollection not working for Tally"
     153
     154 -   def testAllEventTimes(self):
     155         """Test allEventTimes function/method
     156         """
     157         initialize()
     158         for i in range(3):
     159             f = ForEvtTimes()
     160             activate(f,f.run(),at=i)
     161         assert(allEventTimes()==[0,1,2]),\
     162                "allEventTimes not working"
     163
     164
     165
     166
     167 -def makeSSuite():
     168      suite = unittest.TestSuite()
     169      testInit = makeSimulationtestcase('testInit')
     170      testActivate = makeSimulationtestcase('testActivate')
     171      testStart = makeSimulationtestcase('testStart')
     172      testStartActions = makeSimulationtestcase('testStartActions')
     173      testYield = makeSimulationtestcase('testYield')
     174      testStopSimulation = makeSimulationtestcase('testStopSimulation')
     175      testStartCollection = makeSimulationtestcase('testStartCollection')
     176      testAllEventTimes = makeSimulationtestcase('testAllEventTimes')
     177      ##testrequest3 = makeSimulationtestcase('testrequest3')
     178      ##testrequest4 = makeSimulationtestcase('testrequest4')
     179      suite.addTests([testInit, testActivate, testStart, testStartActions,
     180                      testYield,testStopSimulation,testStartCollection,
     181                      testAllEventTimes])
     182      return suite
     183
     184  ## -------------------------------------------------------------
     185  ##                    TEST RESOURCES
     186  ## -------------------------------------------------------------
     187
     188 -class Job(Process):
     189     """ Job class for testing"""
     190 -   def __init__(self, server = None, name = ''):
     191          Process.__init__(self)
     192          self.name = name
     193          self.R = server
     194
     195 -   def execute(self):
     196          yield request, self, self.R
     197
     198
     199 -class makeResourcetestcase(unittest.TestCase):
     200     """ First simple tests of Resources
     201     """
     202 -   def testInit(self):
     203          """Test initialisation"""
     204          R = Resource()
     205          assert R.name == 'a_resource', 'Not null name'
     206          assert R.capacity == 1, 'Not unit capacity'
     207          assert R.unitName == 'units', 'Not the correct unit name'
     208          R = Resource(name = '', capacity = 1)
     209          assert R.name == '', 'Not null name'
     210          assert R.capacity == 1, 'Not unit capacity'
     211          assert R.unitName == 'units', 'Not the correct unit name'
     212          R = Resource(capacity = 3, name = '3 - version', unitName = 'blobs')
     213          assert R.name == '3 - version', 'Wrong name, it is' + R.name
     214          assert R.capacity == 3, 'Not capacity 3, it is '+`R.capacity`
     215          assert R.unitName == 'blobs', 'Not the correct unit name'
     216          ## next test 0 capacity is allowed
     217          R = Resource(capacity = 0, name = '0 - version')
     218          assert R.capacity == 0, 'Not capacity 0, it is '+`R.capacity`
     219
     220 -   def testrequest(self):
     221          """Test request"""
     222          ## NB this next call should be changed to
     223          ## R = Resource() when Simulation is fixed
     224          R0 = Resource(name = '', capacity = 0)
     225          assert R0.name == '', 'Not null name'
     226          assert R0.capacity == 0, 'Not capacity 0, it is ' + 'R0.capacity'
     227          ## now test requesting: ------------------------------------
     228          initialize()
     229          R1 = Resource(capacity = 0, name = '3 - version', unitName = 'blobs')
     230          J = Job(name = 'job', server = R1)
     231          activate(J, J.execute(), at = 0.0) # this requests a unit of R1
     232          ## when simulation starts
     233          simulate(until = 10.0)
     234          assert R1.n == 0, 'Should be 0, it is ' + str(R1.n)
     235          lenW = len(R1.waitQ)
     236          assert lenW == 1, 'Should be 1, it is ' + str(lenW)
     237          assert len(R1.activeQ) == 0, 'len activeQ Should be 0, it is '+\
     238          str(len(R1.activeQ))
     239
     240 -   def testrequest2(self):
     241          """Test request2 with capacity = 1"""
     242          ## now test requesting: ------------------------------------
     243          initialize()
     244          R2 = Resource(capacity = 1, name = '3 - version', unitName = 'blobs')
     245          J2 = Job(name = 'job', server = R2)
     246          activate(J2, J2.execute(), at = 0.0) # requests a unit of R2
     247          ## when simulation starts
     248          simulate(until = 10.0)
     249          assert R2.n == 0, 'Should be 0, it is ' + str(R2.n)
     250          lenW = len(R2.waitQ)
     251          lenA = len(R2.activeQ)
     252          assert lenW == 0, 'lenW Should be 0, it is ' + str(lenW)
     253          assert lenA == 1, 'lenA Should be 1, it is ' + str(lenA)
     254
     255 -   def testrequest3(self):
     256          """Test request3 with capacity = 1 several requests"""
     257          ## now test requesting: ------------------------------------
     258          initialize()
     259          R3 = Resource(capacity = 1, name = '3 - version', unitName = 'blobs')
     260          J2 = Job(name = 'job', server = R3)
     261          J3 = Job(name = 'job', server = R3)
     262          J4 = Job(name = 'job', server = R3)
     263          activate(J2, J2.execute(), at = 0.0) # requests a unit of R3
     264          activate(J3, J3.execute(), at = 0.0) # requests a unit of R3
     265          activate(J4, J4.execute(), at = 0.0) # requests a unit of R3
     266          ## when simulation starts
     267          simulate(until = 10.0)
     268          assert R3.n == 0, 'Should be 0, it is ' + str(R3.n)
     269          lenW = len(R3.waitQ)
     270          lenA = len(R3.activeQ)
     271          assert lenW == 2, 'lenW Should be 2, it is ' + str(lenW)
     272          assert R3.waitQ == [J3, J4],'WaitQ wrong' + str(R3.waitQ)
     273          assert lenA == 1, 'lenA Should be 1, it is ' + str(lenA)
     274          assert R3.activeQ == [J2],'activeQ wrong, it is ' + str(R3.activeQ[0])
     275
     276 -   def testrequest4(self):
     277          """Test request4 with capacity = 2 several requests"""
     278          ## now test requesting: ------------------------------------
     279          initialize()
     280          R3 = Resource(capacity = 2, name = '4 - version', unitName = 'blobs')
     281          J2 = Job(name = 'job', server = R3)
     282          J3 = Job(name = 'job', server = R3)
     283          J4 = Job(name = 'job', server = R3)
     284          activate(J2, J2.execute(), at = 0.0) # requests a unit of R3
     285          activate(J3, J3.execute(), at = 0.0) # requests a unit of R3
     286          activate(J4, J4.execute(), at = 0.0) # requests a unit of R3
     287          ## when simulation starts
     288          simulate(until = 10.0)
     289          assert R3.n == 0, 'Should be 0, it is ' + str(R3.n)
     290          lenW = len(R3.waitQ)
     291          lenA = len(R3.activeQ)
     292          assert lenW == 1, 'lenW Should be 1, it is ' + str(lenW)
     293          assert R3.waitQ == [J4],'WaitQ wrong' + str(R3.waitQ)
     294          assert lenA == 2, 'lenA Should be 2, it is ' + str(lenA)
     295          assert R3.activeQ == [J2, J3],'activeQ wrong, it is ' + str(R3.activeQ[0])
     296
     297  #------- Test Priority Queues
     298
     299 -   def testrequestPriority(self):
     300          """Test PriorityQ, with no preemption, 0 capacity"""
     301          class Job(Process):
     302             """ Job class for testing"""
     303             def __init__(self, server = None, name = ''):
     304                Process.__init__(self)
     305                self.name = name
     306                self.R = server
     307
     308             def execute(self, priority):
     309                yield request, self, self.R, priority
     310
     311          initialize()
     312          Rp = Resource(capacity = 0, qType = PriorityQ)
     313          J5 = Job(name = 'job 5', server = Rp)
     314          J6 = Job(name = 'job 6', server = Rp)
     315          J7 = Job(name = 'job 7', server = Rp)
     316          activate(J5, J5.execute(priority = 3))
     317          activate(J6, J6.execute(priority = 0))
     318          activate(J7, J7.execute(priority = 1))
     319          simulate(until = 100)
     320          assert Rp.waitQ == [J5, J7, J6],'WaitQ wrong' + str([(x.name, x.priority[Rp]) for x in Rp.waitQ])
     321
     322          """Test PriorityQ mechanism"""
     323
     324          def sorted(q):
     325             if not q or len(q) == 1:
     326                sortok = 1
     327                return sortok
     328             sortok = q[0] >= q[1] and sorted(q[2:])
     329             return sortok
     330
     331          initialize()
     332          Rp = Resource(capacity = 0, qType = PriorityQ)
     333          for i in range(10):
     334             J = Job(name = 'job ' + str(i),server = Rp)
     335             activate(J, J.execute(priority = random()))
     336          simulate(until = 1000)
     337          qp = [x._priority[Rp] for x in Rp.waitQ]
     338          assert sorted(qp),'waitQ not sorted by priority: ' + str([(x.name, x._priority[Rp]) for x in Rp.waitQ])
     339
     340
     341 -   def testrequestPriority1(self):
     342          """Test PriorityQ, with no preemption, capacity == 1"""
     343          class Job(Process):
     344             """ Job class for testing"""
     345             def __init__(self, server = None, name = ''):
     346                Process.__init__(self)
     347                self.name = name
     348                self.R = server
     349
     350             def execute(self, priority):
     351                yield request, self, self.R, priority
     352
     353          initialize()
     354          Rp = Resource(capacity = 1, qType = PriorityQ)
     355          J5 = Job(name = 'job 5', server = Rp)
     356          J6 = Job(name = 'job 6', server = Rp)
     357          J7 = Job(name = 'job 7', server = Rp)
     358          activate(J5, J5.execute(priority = 2))
     359          activate(J6, J6.execute(priority = 4))
     360          activate(J7, J7.execute(priority = 3))
     361          simulate(until = 100)
     362          assert Rp.waitQ == [J6, J7],'WaitQ wrong ' + str([(x.name, x._priority[Rp]) for x in Rp.waitQ])
     363
     364 -   def testrequestPriority2(self):
     365         """Test PriorityQ, with preemption, capacity == 1"""
     366         class nuJob(Process):
     367            def __init__(self, name):
     368               Process.__init__(self, name)
     369
     370            def execute(self, res, priority):
     371               self.preempt = len(res.activeQ) > 0 and priority > res.activeQ[-1]._priority[res]
     372               t = now()
     373               yield request, self, res, priority
     374               if self.preempt:
     375                  assert len(res.waitQ) == 1, 'No preemption ' + 'activeQ = ' + str(res.activeQ[0].name)
     376               yield hold, self, 30
     377               t1 = now()
     378               if self.preempt:
     379                  assert t + 30 == t1, 'Wrong completion time for preemptor ' + self.name
     380               else:
     381                  assert t + 60 == t1, 'Wrong completion time for preempted ' + self.name + ' ' + str(now())
     382               yield release, self, res
     383
     384         initialize()
     385         res = Resource(name = 'server', capacity = 1, qType = PriorityQ, preemptable = 1)
     386         n1 = nuJob(name = 'nuJob 1')
     387         n2 = nuJob(name = 'nuJob 2')
     388         activate(n1, n1.execute(res, priority = 0))
     389         activate(n2, n2.execute(res, priority = 1),at = 15)
     390         simulate(until = 100)
     391
     392 -   def testrequestPriority3(self):
     393         """Test preemption of preemptor"""
     394         class nuJob(Process):
     395            seqOut = []
     396            def __init__(self, name):
     397               Process.__init__(self, name)
     398               self.serviceTime = 30
     399
     400            def execute(self, res, priority):
     401               self.preempt = len(res.activeQ) > 0 and priority > res.activeQ[-1]._priority[res]
     402               nrwaiting = len(res.waitQ)
     403               yield request, self, res, priority
     404               if self.preempt:
     405                  assert len(res.waitQ) == nrwaiting + 1, 'No preemption ' + 'activeQ = ' + str(res.activeQ[0].name)
     406               yield hold, self, self.serviceTime
     407               yield release, self, res
     408               nuJob.seqOut.append((self, now()))
     409
     410
     411
     412         initialize()
     413         res = Resource(name = 'server', capacity = 1, qType = PriorityQ, preemptable = 1)
     414         n1 = nuJob(name = 'nuJob 1')
     415         n2 = nuJob(name = 'nuJob 2')
     416         n3 = nuJob(name = 'nuJob 3')
     417         activate(n1, n1.execute(res, priority=-1))
     418         start2 = 10
     419         activate(n2, n2.execute(res, priority = 0),at = start2)
     420         start3 = 20
     421         activate(n3, n3.execute(res, priority = 1),at = start3)
     422         simulate(until = 100)
     423         assert [x[1] for x in nuJob.seqOut] == [start3 + n3.serviceTime, start2 + 2 * n2.serviceTime, 90],\
     424                'Wrong service sequence / times: ' + str([x for x in nuJob.seqOut])
     425
     426 -   def testrequestNestedPreempt(self):
     427        """Test that a process can preempt another process holding multiple resources
     428        """
     429        class Requestor(Process):
     430            def run(self, res1, res2, res3, priority = 1):
     431                yield request, self, res1, priority
     432                yield request, self, res2, priority
     433                yield request, self, res3, priority
     434                record.observe(y = self.name)
     435                yield hold, self, 100
     436                record.observe(y = self.name)
     437                yield release, self, res3
     438                yield release, self, res2
     439                yield release, self, res1
     440
     441        initialize()
     442        outer = Resource(name = 'outer', qType = PriorityQ, preemptable = True)
     443        inner = Resource(name = 'inner', qType = PriorityQ, preemptable = True)
     444        innermost = Resource(name = 'innermost', qType = PriorityQ, preemptable = True)
     445        record = Monitor()
     446        r1 = Requestor('r1')
     447        activate(r1, r1.run(res1 = outer, res2 = inner, res3 = innermost, priority = 1))
     448        r2 = Requestor('r2')
     449        activate(r2, r2.run(res1 = outer, res2 = inner, res3 = innermost, priority = 10),at = 50)
     450        simulate(until = 200)
     451        assert record == [[0, 'r1'],[50, 'r2'],[150, 'r2'],[200, 'r1']],\
     452                'was %s; preempt did not work'%record
     453
     454
     455 -   def testmonitored(self):
     456        """ test monitoring of number in the two queues, waitQ and activeQ
     457        """
     458        class Job(Process):
     459            def __init__(self, name):
     460               Process.__init__(self, name)
     461
     462            def execute(self, res):
     463               yield request, self, res
     464               yield hold, self, 2
     465               yield release, self, res
     466
     467        initialize()
     468        res = Resource(name = 'server', capacity = 1, monitored = 1)
     469        n1 = Job(name = 'Job 1')
     470        n2 = Job(name = 'Job 2')
     471        n3 = Job(name = 'Job 3')
     472        activate(n1, n1.execute(res),at = 2)
     473        activate(n2, n2.execute(res),at = 2)
     474        activate(n3, n3.execute(res),at = 2) # 3 arrive at 2
     475        simulate(until = 100)
     476        assert res.waitMon == [[2, 1], [2, 2], [4, 1], [6, 0]],'Wrong waitMon:%s'%res.waitMon
     477        assert res.actMon == [[2, 1], [4, 0], [4, 1], [6, 0], [6, 1], [8, 0]],'Wrong actMon:%s'%res.actMon
     478        #print res.actMon
     479        assert res.waitMon.timeAverage() == (0 * 2 + 2 * 2 + 1 * 2) / 6.0, 'Wrong waitMon.timeAverage:%s'%res.waitMon.timeAverage()
     480
     481
     482 -def makeRSuite():
     483      suite = unittest.TestSuite()
     484      testInit = makeResourcetestcase('testInit')
     485      testrequest = makeResourcetestcase('testrequest')
     486      testrequest2 = makeResourcetestcase('testrequest2')
     487      testrequest3 = makeResourcetestcase('testrequest3')
     488      testrequest4 = makeResourcetestcase('testrequest4')
     489      testrequestPriority = makeResourcetestcase('testrequestPriority')
     490      testrequestPriority1 = makeResourcetestcase('testrequestPriority1')
     491      testrequestPriority2 = makeResourcetestcase('testrequestPriority2')
     492      testrequestPriority3 = makeResourcetestcase('testrequestPriority3')
     493      testrequestNestedPreempt = makeResourcetestcase('testrequestNestedPreempt')
     494      testmonitored = makeResourcetestcase('testmonitored')
     495      suite.addTests([testInit, testrequest, testrequest2, testrequest3, testrequest4, testrequestPriority,
     496                      testrequestPriority1, testrequestPriority2, testrequestPriority3,
     497                      testrequestNestedPreempt, testmonitored])
     498      return suite
     499
     500
     501  ##=====================================================
     502  ##                   Test Interrupts
     503  ##=====================================================
     504
     505
     506 -class Interruptor(Process):
     507 -   def __init__(self):
     508        Process.__init__(self)
     509
     510 -   def breakin(self, waitbefore, howoften = 1):
     511        for i in range(howoften):
     512           yield hold, self, waitbefore
     513           self.interrupt(victim)
     514
     515 -class Interrupted(Process):
     516 -   def __init__(self):
     517        Process.__init__(self)
     518
     519 -   def myActivity(self, howlong, theEnd = 200):
     520        global igothit
     521        igothit={}
     522        while now() <= theEnd:
     523           yield hold, self, howlong
     524           if self.interrupted():
     525              byWhom = self.interruptCause
     526              igothit[now()] = byWhom
     527           else:
     528              pass
     529
     530 -class makeInterrupttestcase(unittest.TestCase):
     531     """
     532     Tests interrupts as defined in SEP001v17
     533     """
     534 -   def testInterrupt1(self):
     535        """
     536        Test single interrupt during victim activity
     537        """
     538        global victim
     539        initialize()
     540        breaker = Interruptor()
     541        activate(breaker, breaker.breakin(10))
     542        victim = Interrupted()
     543        activate(victim, victim.myActivity(100))
     544        simulate(until = 200)
     545        assert igothit[10] == breaker, 'Not interrupted at 10 by breaker'
     546        assert len(igothit) == 1, 'Interrupted more than once'
     547
     548 -   def testInterrupt2(self):
     549        """
     550        Test multiple interrupts during victim activity
     551        """
     552        global victim
     553        initialize()
     554        breaker = Interruptor()
     555        activate(breaker, breaker.breakin(10, howoften = 3))
     556        victim = Interrupted()
     557        activate(victim, victim.myActivity(100))
     558        simulate(until = 200)
     559        for i in (10, 20, 30):
     560           assert igothit[i] == breaker, 'Not interrupted at %s by breaker' %i
     561        assert len(igothit) == 3, 'Interrupted wrong number of times'
     562
     563 -   def testInterrupt3(self):
     564        """
     565        Test interrupts after victim activity
     566        """
     567        global victim
     568        initialize()
     569        breaker = Interruptor()
     570        activate(breaker, breaker.breakin(50, howoften = 5))
     571        victim = Interrupted()
     572        activate(victim, victim.myActivity(10, theEnd = 10))
     573        simulate(until = 200)
     574        assert len(igothit) == 0, 'There has been an interrupt after victim lifetime'
     575
     576 -   def testInterrupt4(self):
     577        """
     578        Test multiple interrupts by multiple processes during victim activity
     579        """
     580        global victim
     581        initialize()
     582        breaker1 = Interruptor()
     583        activate(breaker1, breaker1.breakin(15, howoften = 3))
     584        breaker2 = Interruptor()
     585        activate(breaker2, breaker2.breakin(20, howoften = 3))
     586        victim = Interrupted()
     587        activate(victim, victim.myActivity(100))
     588        simulate(until = 200)
     589        for i in (15, 30, 45):
     590           assert igothit[i] == breaker1, 'Not interrupted at %s by breaker1' %i
     591        for i in (20, 40, 60):
     592           assert igothit[i] == breaker2, 'Not interrupted at %s by breaker2' %i
     593        assert len(igothit) == 6, 'Interrupted wrong number of times'
     594
     595 -   def testInterrupt5(self):
     596        """
     597        Test reset of 'interrupted' state.
     598        """
     599        global victim
     600        initialize()
     601        breaker = Interruptor()
     602        victim = Interrupted()
     603
     604        def newProcess(self):
     605           while True:
     606              assert not self.interrupted(),'Incorrectly interrupted'
     607              yield hold, self, 100
     608              if self.interrupted():
     609                 self.interruptReset()
     610                 assert not self.interrupted(),'Incorrectly interrupted'
     611
     612        victim.newProcess = newProcess
     613        activate(victim, newProcess(victim))
     614        activate(breaker, breaker.breakin(10, howoften = 3))
     615        simulate(until = 1000)
     616
     617 -def makeISuite():
     618     suite = unittest.TestSuite()
     619     testInterrupt1 = makeInterrupttestcase('testInterrupt1')
     620     testInterrupt2 = makeInterrupttestcase('testInterrupt2')
     621     testInterrupt3 = makeInterrupttestcase('testInterrupt3')
     622     testInterrupt4 = makeInterrupttestcase('testInterrupt4')
     623     testInterrupt5 = makeInterrupttestcase('testInterrupt5')
     624     suite.addTests([testInterrupt1, testInterrupt2, testInterrupt3, testInterrupt4, testInterrupt5])
     625     return suite
     626
     627  ## -------------------------------------------------------------
     628  ##                    TEST PROCESS STATES
     629  ## -------------------------------------------------------------
     630
     631 -class PS1(Process):
     632 -   def __init__(self):
     633        Process.__init__(self)
     634
     635 -   def life1(self):
     636        yield hold, self, 10
     637
     638 -   def life2(self):
     639        yield hold, self, 10
     640        yield passivate, self
     641        yield hold, self, 10
     642
     643 -class Observer1(Process):
     644 -   def __init__(self):
     645        Process.__init__(self)
     646
     647 -   def look1(self, p):
     648        assert p.active(),'p not active'
     649        assert not p.passive(), 'p passive'
     650        assert not p.terminated(),'p terminated'
     651        assert not p.interrupted(),'p interrupted'
     652        yield hold, self, 11
     653        assert not p.active(),'p active'
     654        assert not p.passive(),'p passive'
     655        assert p.terminated(),'p not terminated'
     656        assert not p.interrupted(),'p interrupted'
     657
     658 -   def look2(self, p):
     659        assert not p.active(),'p active'
     660        assert p.passive(),'p not passive'
     661        assert not p.terminated(),'p not terminated'
     662        assert not p.interrupted(),'p interrupted'
     663        activate(p, p.life1())
     664        yield hold, self, 11
     665        assert not p.active(),'p active'
     666        assert not p.passive(),'p not passive'
     667        assert p.terminated(),'p not terminated'
     668        assert not p.interrupted(),'p interrupted'
     669
     670 -   def look3(self, p):
     671        assert not p.active(),'p active'
     672        assert p.passive(),'p not passive'
     673        assert not p.terminated(),'p not terminated'
     674        assert not p.interrupted(),'p interrupted'
     675        activate(p, p.life2())
     676        yield hold, self, 11
     677        assert not p.active(),'p active'
     678        assert p.passive(),'p not passive'
     679        assert not p.terminated(),'p terminated'
     680        assert not p.interrupted(),'p interrupted'
     681
     682 -   def look4(self, p):
     683        yield hold, self, 5
     684        assert p.active(),'p not active'
     685        assert not p.passive(),'p passive'
     686        assert not p.terminated(),'p terminated'
     687        assert not p.interrupted(),'p interrupted'
     688        self.cancel(p)
     689        assert not p.active(),'p active'
     690        assert p.passive(),'p not passive'
     691        assert not p.terminated(),'p terminated'
     692        assert not p.interrupted(),'p interrupted'
     693        reactivate(p)
     694        assert p.active(),'p not active'
     695        assert not p.passive(),'p passive'
     696        assert not p.terminated(),'p terminated'
     697        assert not p.interrupted(),'p interrupted'
     698        yield hold, self
     699        assert not p.active(),'p active'
     700        assert not p.passive(),'p passive'
     701        assert p.terminated(),'p terminated'
     702        assert not p.interrupted(),'p interrupted'
     703
     704 -   def look5(self, p):
     705        yield hold, self, 11
     706        assert not p.active(),'p active'
     707        assert p.passive(),'p not passive'
     708        assert not p.terminated(),'p terminated'
     709        assert not p.interrupted(),'p interrupted'
     710        self.cancel(p)
     711        assert not p.active(),'p active'
     712        assert p.passive(),'p not passive'
     713        assert not p.terminated(),'p terminated'
     714        assert not p.interrupted(),'p interrupted'
     715
     716 -class PS2(Process):
     717 -   def __init__(self):
     718        Process.__init__(self)
     719
     720 -   def life1(self, res):
     721        yield hold, self, 1
     722        yield request, self, res
     723        yield hold, self, 5
     724        yield request, self, res
     725
     726 -   def life2(self, res):
     727        yield request, self, res
     728        assert self.interrupted(),'p not interrupted'
     729        assert self.queuing(res)
     730        self.interruptReset()
     731        assert not self.interrupted(), 'p interrupted'
     732        assert self.queuing(res)
     733
     734 -class Observer2(Process):
     735 -   def __init__(self):
     736        Process.__init__(self)
     737
     738 -   def look1(self, p1, p2, res):
     739        assert p1.active(), 'p1 not active'
     740        assert not p1.queuing(res), 'p1 queuing'
     741        assert p2.active(), 'p2 noit active'
     742        assert not p2.queuing(res), 'p2 queuing'
     743        yield hold, self, 2
     744        assert p1.active(), 'p1 not active'
     745        assert not p1.queuing(res), 'p1 queuing'
     746        assert p2.passive(), 'p2 active'
     747        assert p2.queuing(res), 'p2 not queuing'
     748
     749 -   def look2(self, p,res):
     750        yield request, self, res
     751        yield hold, self, 5
     752        assert p.passive(),'p not passive'
     753        assert p.queuing(res),'p not queuing for resource'
     754        assert not p.interrupted(), 'p interrupted'
     755        self.interrupt(p)
     756        yield hold, self
     757
     758 -class makePStatetestcase(unittest.TestCase):
     759     """
     760     Tests states and state transitions as defined in SEP003
     761     """
     762
     763 -   def testState1(self):
     764        """
     765        Tests state transitions by hold
     766        """
     767        ## active => hold => terminated
     768        initialize()
     769        p = PS1()
     770        activate(p, p.life1())
     771        ob = Observer1()
     772        activate(ob, ob.look1(p),prior = True)
     773        simulate(until = 12)
     774
     775 -   def testState2(self):
     776        """
     777        Tests state transitions by activate and passivate
     778        """
     779        ## passive => activate => hold => terminated
     780        initialize()
     781        p = PS1()
     782        ob1 = Observer1()
     783        activate(ob1, ob1.look2(p))
     784        simulate(until = 12)
     785        ## passive => activate => hold => active => passivate => passive
     786        initialize()
     787        p1 = PS1()
     788        ob2 = Observer1()
     789        activate(ob2, ob2.look3(p1),prior = True)
     790        simulate(until = 12)
     791
     792 -   def testState3(self):
     793        """
     794        Tests state transitions by cancel()
     795        """
     796        ## active => cancel => passive => reactivate => active => terminated
     797        initialize()
     798        p2 = PS1()
     799        activate(p2, p2.life1())
     800        ob3 = Observer1()
     801        activate(ob3, ob3.look4(p2))
     802        simulate(until = 12)
     803
     804        ## passive => cancel => passive
     805        initialize()
     806        p3 = PS1()
     807        activate(p3, p3.life2())
     808        ob4 = Observer1()
     809        activate(ob4, ob4.look5(p3))
     810        simulate(until = 12)
     811
     812 -   def testState4(self):
     813        """
     814        Test request / release state transitions
     815        """
     816        ## not queuing, active => request => queuing, passive => release => not queuing, active
     817        initialize()
     818        res = Resource(capacity = 1)
     819        pq1 = PS2()
     820        activate(pq1, pq1.life1(res))
     821        pq2 = PS2()
     822        activate(pq2, pq2.life1(res))
     823        obq1 = Observer2()
     824        activate(obq1, obq1.look1(pq1, pq2, res))
     825        simulate(until = 12)
     826
     827        ## queuing, passive => interrupt =>  queuing, interrupted => interruptRest => queuing, not interrupted
     828        initialize()
     829        res = Resource(capacity = 1)
     830        pq3 = PS2()
     831        activate(pq3, pq3.life2(res))
     832        obq2 = Observer2()
     833        activate(obq2, obq2.look2(pq3, res),prior = True)
     834        simulate(until = 12)
     835
     836
     837
     838 -def makePSuite():
     839     suite = unittest.TestSuite()
     840     testState1 = makePStatetestcase('testState1')
     841     testState2 = makePStatetestcase('testState2')
     842     testState3 = makePStatetestcase('testState3')
     843     testState4 = makePStatetestcase('testState4')
     844     suite.addTests([testState1, testState2, testState3, testState4])
     845     return suite
     846
     847  ## -------------------------------------------------------------
     848  ##                    TEST Events / Signals
     849  ## -------------------------------------------------------------
     850
     851 -class SignalProcess(Process):
     852 -   def makeSignal(self, ev1, ev2):
     853        yield hold, self, 1
     854        ev1.signal('from SignalProcess')
     855        while ev2.queues:
     856           nq0 = len(ev2.queues)
     857           ev2.signal('from SignalProcess')
     858           assert len(ev2.queues) == (nq0 - 1),'wrong number of processes dequeued'
     859
     860 -class WaitProcess(Process):
     861 -   def waitForSig(self, ev1):
     862        yield waitevent, self, ev1
     863        assert ev1.waits == [],'not all processes waiting for event out of waiting list'
     864        assert ev1 in self.eventsFired, 'did not record firing event'
     865
     866 -class QueueProcess(Process):
     867 -   def queueForSig(self, ev2):
     868        yield queueevent, self, ev2
     869        assert ev2 in self.eventsFired, 'did not record firing event'
     870
     871 -class SignalProcessOR(Process):
     872 -   def makeSignal(self, ev1, ev2):
     873        yield hold, self, 1
     874        ev1.signal('from SignalProcess')
     875        yield hold, self, 3
     876        assert len(ev2.queues) == QueueProcessOR.nrProcesses, 'wrong number of processes queuing for event ev2'
     877        while ev2.queues:
     878           nq0 = len(ev2.queues)
     879           ev2.signal('from SignalProcess')
     880           assert len(ev2.queues) == (nq0 - 1),'wrong number of processes dequeued'
     881        assert not ev2.queues, 'not all processes queuing for ev2 dequeued'
     882
     883 -class WaitProcessOR(Process):
     884 -   def waitForSig(self, evset):
     885        yield waitevent, self, evset
     886        for e in evset:
     887           assert e.waits == [],'process not out of waiting list for all events in OR'
     888
     889 -class WaitProcessOR1(Process):
     890 -    def signalandwait(self):
     891        e1 = SimEvent()
     892        e1.signal()
     893        e2 = SimEvent()
     894        e2.signal()
     895        yield waitevent, self,[e1, e2]
     896        assert self.eventsFired == [e1, e2],'eventsFired does not report all events'
     897
     898
     899 -class QueueProcessOR(Process):
     900      nrProcesses = 0
     901 -    def __init__(self):
     902          Process.__init__(self)
     903          QueueProcessOR.nrProcesses += 1
     904 -    def queueForSig(self, evset):
     905        yield queueevent, self, evset
     906        occurred = False
     907        for e in evset:
     908            occurred = occurred or (e in self.eventsFired)
     909        assert occurred, 'queuing process activated by wrong event(s)'
     910
     911 -class QueueProcessOR1(Process):
     912 -    def signalandqueue(self):
     913          e1 = SimEvent()
     914          e1.signal()
     915          e2 = SimEvent()
     916          e2.signal()
     917          yield queueevent, self,[e1, e2]
     918          assert self.eventsFired == [e1, e2],\
     919                  '(queueevent) eventsFired does not report all fired events'
     920
     921 -class makeEtestcase(unittest.TestCase):
     922     """
     923     Test SimEvent / signal as introduced with SimPy 1.5
     924     """
     925
     926 -   def testSimEvents1(self):
     927        """
     928        Tests basic signal semantics
     929        """
     930        e = SimEvent()
     931        e.signal('param')
     932        assert e.occurred, 'signal does not set \'occurred\' to True'
     933        assert e.signalparam == 'param', 'signal parameter wrong'
     934        e.signal()
     935        assert e.signalparam is None, 'signal with no parameter did not overwrite signalparam'
     936        e.signal()
     937        assert e.occurred, 'multiple calls to signal do not set \'occurred\''
     938
     939 -   def testSimEvents2(self):
     940        """
     941        Tests basic waiting and queuing semantics
     942        """
     943        initialize()
     944        ev1 = SimEvent('ev1')
     945        ev2 = SimEvent('ev2')
     946        w1 = WaitProcess()
     947        activate(w1, w1.waitForSig(ev1))
     948        w2 = WaitProcess()
     949        activate(w2, w2.waitForSig(ev1))
     950        for i in range(3):
     951           q = QueueProcess()
     952           activate(q, q.queueForSig(ev2))
     953        simulate(until = 2)
     954
     955 -   def testSimEvents3(self):
     956        """
     957        Tests waiting, queuing for at least one event out of a list / tuple.
     958        """
     959        initialize()
     960        e1 = SimEvent('e1')
     961        e2 = SimEvent('e2')
     962        e3 = SimEvent('e3')
     963        s = SignalProcessOR()
     964        activate(s, s.makeSignal(e1, e3))
     965        w = WaitProcessOR()
     966        activate(w, w.waitForSig([e1, e2]))
     967        for i in range(5):
     968           q = QueueProcessOR()
     969           activate(q, q.queueForSig([e2, e3]))
     970        simulate(until = 10)
     971
     972 -   def testSimEvents4(self):
     973        """Tests that eventsFired reports all events which fired
     974        """
     975        initialize()
     976        w = WaitProcessOR1()
     977        activate(w, w.signalandwait())
     978        simulate(until = 5)
     979
     980 -   def testSimEvents5(self):
     981        """Tests that eventsFired reports all events which fired
     982        """
     983        initialize()
     984        w = QueueProcessOR1()
     985        activate(w, w.signalandqueue())
     986        simulate(until = 5)
     987
     988 -def makeESuite():
     989     suite = unittest.TestSuite()
     990     testSimEvents1 = makeEtestcase('testSimEvents1')
     991     testSimEvents2 = makeEtestcase('testSimEvents2')
     992     testSimEvents3 = makeEtestcase('testSimEvents3')
     993     testSimEvents4 = makeEtestcase('testSimEvents4')
     994     testSimEvents5 = makeEtestcase('testSimEvents5')
     995     suite.addTests([testSimEvents1, testSimEvents2, testSimEvents3, testSimEvents4, testSimEvents5])
     996     return suite
     997
     998  ## -------------------------------------------------------------
     999  ##                    TEST waituntil
    1000  ## -------------------------------------------------------------
    1001
    1002 -class Signaller(Process):
    1003 -    def makeconditions(self, waiter):
    1004          global a, b,c
    1005          a = True
    1006          yield hold, self, 1
    1007          b = True
    1008          yield hold, self, 1
    1009          c = True
    1010          yield hold, self, 1
    1011          assert waiter.terminated(),'waituntil did not fire'
    1012
    1013 -class Waiter(Process):
    1014 -    def waitforit(self):
    1015          def waitcond():
    1016              return a and b and c
    1017          yield waituntil, self, waitcond
    1018
    1019 -class makeWtestcase(unittest.TestCase):
    1020     """
    1021     Test waituntil as introduced with SimPy 1.5
    1022     """
    1023
    1024 -   def testwaituntil1(self):
    1025         global a, b,c
    1026         a = b=c = False
    1027         initialize()
    1028         w = Waiter()
    1029         activate(w, w.waitforit())
    1030         s = Signaller()
    1031         activate(s, s.makeconditions(w))
    1032         simulate(until = 5)
    1033
    1034 -def makeWSuite():
    1035     suite = unittest.TestSuite()
    1036     testwaituntil1 = makeWtestcase('testwaituntil1')
    1037     suite.addTests([testwaituntil1])
    1038     return suite
    1039
    1040  ## -------------------------------------------------------------
    1041  ##                    TEST COMPOUND 'YIELD REQUEST' COMMANDS
    1042  ## -------------------------------------------------------------
    1043
    1044  ## -------------------------------------------------------------
    1045  ##             TEST 'yield (request, self, res),(hold, self, delay)'
    1046  ##                   == timeout renege
    1047  ##             for both unmonitored and monitored resources
    1048  ## -------------------------------------------------------------
    1049
    1050 -class JobTO(Process):
    1051     """ Job class for testing timeout reneging
    1052     """
    1053 -   def __init__(self, server = None, name = ''):
    1054          Process.__init__(self, name)
    1055          self.res = server
    1056          self.gotResource = None
    1057
    1058 -   def execute(self, timeout, usetime):
    1059          yield (request, self, self.res),(hold, self, timeout)
    1060          if self.acquired(self.res):
    1061              self.gotResource = True
    1062              yield hold, self, usetime
    1063              yield release, self, self.res
    1064          else:
    1065              self.gotResource = False
    1066
    1067 -class JobTO_P(Process):
    1068     """ Job class for testing timeout reneging with priorities
    1069     """
    1070 -   def __init__(self, server = None, name = ''):
    1071          Process.__init__(self, name)
    1072          self.res = server
    1073          self.gotResource = None
    1074
    1075 -   def execute(self, timeout, usetime, priority):
    1076          yield (request, self, self.res, priority),(hold, self, timeout)
    1077          if self.acquired(self.res):
    1078              self.gotResource = True
    1079              yield hold, self, usetime
    1080              yield release, self, self.res
    1081          else:
    1082              self.gotResource = False
    1083
    1084 -class makeTimeoutTestcase(unittest.TestCase):
    1085      """ Tests of 'yield (request, self, res),(hold, self, delay)'
    1086          timeout reneging command
    1087      """
    1088 -    def testNoTimeout(self):
    1089          """Test that resource gets acquired without timeout
    1090          """
    1091          res = Resource(name = 'Server', capacity = 1)
    1092          initialize()
    1093          usetime = 5
    1094          timeout = 1000000
    1095          j1 = JobTO(server = res, name = 'Job_1')
    1096          activate(j1, j1.execute(timeout = timeout, usetime = usetime))
    1097          j2 = JobTO(server = res, name = 'Job_2')
    1098          activate(j2, j2.execute(timeout = timeout, usetime = usetime))
    1099          simulate(until = 2 * usetime)
    1100          assert now() == 2 * usetime, 'time not == 2 * usetime'
    1101          assert j1.gotResource and j2.gotResource,\
    1102              'at least one job failed to get resource'
    1103          assert not (res.waitQ or res.activeQ),\
    1104              'job waiting or using resource'
    1105
    1106 -    def testNoTimeoutM(self):
    1107          """Test that resource gets acquired without timeout.
    1108             Resource monitored.
    1109          """
    1110          res = Resource(name = 'Server', capacity = 1, monitored = True)
    1111          initialize()
    1112          usetime = 5
    1113          timeout = 1000000
    1114          j1 = JobTO(server = res, name = 'Job_1')
    1115          activate(j1, j1.execute(timeout = timeout, usetime = usetime))
    1116          j2 = JobTO(server = res, name = 'Job_2')
    1117          activate(j2, j2.execute(timeout = timeout, usetime = usetime))
    1118          simulate(until = 2 * usetime)
    1119          assert now() == 2 * usetime, 'time not == 2 * usetime'
    1120          assert j1.gotResource and j2.gotResource,\
    1121              'at least one job failed to get resource'
    1122          assert not (res.waitQ or res.activeQ),\
    1123              'job waiting or using resource'
    1124          assert res.waitMon == [[0, 1],[usetime, 0]],'res.waitMon wrong: %s'%res.waitMon
    1125
    1126 -    def testTimeout1(self):
    1127          """Test that timeout occurs when resource busy
    1128          """
    1129          res = Resource(name = 'Server', capacity = 1)
    1130          initialize()
    1131          usetime = 5
    1132          timeout = 3
    1133          j1 = JobTO(server = res, name = 'Job_1')
    1134          activate(j1, j1.execute(timeout = timeout, usetime = usetime))
    1135          j2 = JobTO(server = res, name = 'Job_2')
    1136          activate(j2, j2.execute(timeout = timeout, usetime = usetime))
    1137          simulate(until = 2 * usetime)
    1138          assert(now() == usetime),'time not == usetime'
    1139          assert(j1.gotResource),'Job_1 did not get resource'
    1140          assert(not j2.gotResource),'Job_2 did not renege'
    1141          assert not (res.waitQ or res.activeQ),\
    1142              'job waiting or using resource'
    1143
    1144 -    def testTimeout1M(self):
    1145          """Test that timeout occurs when resource busy.
    1146             Resource monitored.
    1147          """
    1148          res = Resource(name = 'Server', capacity = 1, monitored = True)
    1149          initialize()
    1150          usetime = 5
    1151          timeout = 3
    1152          j1 = JobTO(server = res, name = 'Job_1')
    1153          activate(j1, j1.execute(timeout = timeout, usetime = usetime))
    1154          j2 = JobTO(server = res, name = 'Job_2')
    1155          activate(j2, j2.execute(timeout = timeout, usetime = usetime))
    1156          simulate(until = 2 * usetime)
    1157          assert(now() == usetime),'time not == usetime'
    1158          assert(j1.gotResource),'Job_1 did not get resource'
    1159          assert(not j2.gotResource),'Job_2 did not renege'
    1160          assert not (res.waitQ or res.activeQ),\
    1161              'job waiting or using resource'
    1162          assert res.waitMon == [[0, 1],[timeout, 0]],'res.waitMon wrong: %s'%res.waitMon
    1163
    1164 -    def testTimeout_MP(self):
    1165          """Test that timeout occurs when resource busy.
    1166             Resource monitored. Requests with priority and preemption.
    1167          """
    1168          res = Resource(name = 'Server', capacity = 1, monitored = True, qType = PriorityQ, preemptable = True)
    1169          initialize()
    1170          usetime = 5
    1171          timeout = 3
    1172          j1 = JobTO_P(server = res, name = 'Job_1')
    1173          activate(j1, j1.execute(timeout = timeout, usetime = usetime, priority = 1))
    1174          j2 = JobTO_P(server = res, name = 'Job_2')
    1175          j2_arrival = 1
    1176          activate(j2, j2.execute(timeout = timeout, usetime = usetime, priority = 5),at = j2_arrival)
    1177          j3 = JobTO_P(server = res, name = 'Job_2')
    1178          j3_arrival = 2
    1179          activate(j3, j3.execute(timeout = timeout, usetime = usetime, priority = 10),at = j3_arrival)
    1180          simulate(until = 3 * usetime)
    1181          assert(now() == 3 * usetime),'time not == 2 * usetime, but %s'%now()
    1182          assert(j1.gotResource),'Job_1 did not get resource'
    1183          assert(j2.gotResource),'Job_2 did renege'
    1184          assert(j2.gotResource),'Job_3 did renege'
    1185          assert not (res.waitQ or res.activeQ),\
    1186              'job waiting or using resource'
    1187          assert res.waitMon == [[j2_arrival, 1],[j3_arrival, 2],[usetime + j3_arrival, 1],[usetime + j2_arrival + usetime, 0]],\
    1188               'res.waitMon wrong: %s'%res.waitMon
    1189
    1190 -    def testTimeout2(self):
    1191          """Test that timeout occurs when resource has no capacity free
    1192          """
    1193          res = Resource(name = 'Server', capacity = 0)
    1194          initialize()
    1195          usetime = 5
    1196          timeout = 3
    1197          j1 = JobTO(server = res, name = 'Job_1')
    1198          activate(j1, j1.execute(timeout = timeout, usetime = usetime))
    1199          j2 = JobTO(server = res, name = 'Job_2')
    1200          activate(j2, j2.execute(timeout = timeout, usetime = usetime))
    1201          simulate(until = 2 * usetime)
    1202          assert now() == timeout, 'time %s not == timeout'%now()
    1203          assert not j1.gotResource, 'Job_1 got resource'
    1204          assert not j2.gotResource, 'Job_2 got resource'
    1205          assert not (res.waitQ or res.activeQ),\
    1206              'job waiting or using resource'
    1207
    1208 -    def testTimeout2M(self):
    1209          """Test that timeout occurs when resource has no capacity free.
    1210             Resource monitored.
    1211          """
    1212          res = Resource(name = 'Server', capacity = 0, monitored = True)
    1213          initialize()
    1214          usetime = 5
    1215          timeout = 3
    1216          j1 = JobTO(server = res, name = 'Job_1')
    1217          activate(j1, j1.execute(timeout = timeout, usetime = usetime))
    1218          j2 = JobTO(server = res, name = 'Job_2')
    1219          activate(j2, j2.execute(timeout = timeout, usetime = usetime))
    1220          simulate(until = 2 * usetime)
    1221          assert now() == timeout, 'time %s not == timeout'%now()
    1222          assert not j1.gotResource, 'Job_1 got resource'
    1223          assert not j2.gotResource, 'Job_2 got resource'
    1224          assert not (res.waitQ or res.activeQ),\
    1225              'job waiting or using resource'
    1226          assert res.waitMon == [[0, 1],[0, 2],[timeout, 1],[timeout, 0]],\
    1227              'res.waitMon is wrong: %s'%res.waitMon
    1228
    1229 -def makeTOSuite():
    1230      suite = unittest.TestSuite()
    1231      testNoTimeout = makeTimeoutTestcase('testNoTimeout')
    1232      testNoTimeoutM = makeTimeoutTestcase('testNoTimeoutM')
    1233      testTimeout1 = makeTimeoutTestcase('testTimeout1')
    1234      testTimeout1M = makeTimeoutTestcase('testTimeout1M')
    1235      testTimeout_MP = makeTimeoutTestcase('testTimeout_MP')
    1236      testTimeout2 = makeTimeoutTestcase('testTimeout2')
    1237      testTimeout2M = makeTimeoutTestcase('testTimeout2M')
    1238      suite.addTests([testNoTimeout, testNoTimeoutM,
    1239                      testTimeout1, testTimeout1M, testTimeout_MP,
    1240                     testTimeout2, testTimeout2M])
    1241      return suite
    1242
    1243  ## ------------------------------------------------------------------
    1244  ##             TEST 'yield (request, self, res),(waitevent, self, event)'
    1245  ##                   == event renege
    1246  ##             for both unmonitored and monitored resources
    1247  ## ------------------------------------------------------------------
    1248
    1249
    1250 -class JobEvt(Process):
    1251     """ Job class for testing event reneging
    1252     """
    1253 -   def __init__(self, server = None, name = ''):
    1254          Process.__init__(self, name)
    1255          self.res = server
    1256          self.gotResource = None
    1257
    1258 -   def execute(self, event, usetime):
    1259          yield (request, self, self.res),(waitevent, self, event)
    1260          if self.acquired(self.res):
    1261              self.gotResource = True
    1262              yield hold, self, usetime
    1263              yield release, self, self.res
    1264          else:
    1265              self.gotResource = False
    1266
    1267 -class JobEvtMulti(Process):
    1268     """ Job class for testing event reneging with multi - event lists
    1269     """
    1270 -   def __init__(self, server = None, name = ''):
    1271          Process.__init__(self, name)
    1272          self.res = server
    1273          self.gotResource = None
    1274
    1275 -   def execute(self, eventlist, usetime):
    1276          yield (request, self, self.res),(waitevent, self, eventlist)
    1277          if self.acquired(self.res):
    1278              self.gotResource = True
    1279              yield hold, self, usetime
    1280              yield release, self, self.res
    1281          else:
    1282              self.gotResource = False
    1283
    1284 -class FireEvent(Process):
    1285      """Fires reneging event
    1286      """
    1287 -    def fire(self, fireDelay, event):
    1288          yield hold, self, fireDelay
    1289          event.signal()
    1290
    1291 -class makeEventRenegeTestcase(unittest.TestCase):
    1292      """Tests of 'yield (request, self, res),(waiteevent, self, event)'
    1293         event reneging command
    1294      """
    1295 -    def testNoEvent(self):
    1296          """Test that processes acquire resource normally if no event fires
    1297          """
    1298          res = Resource(name = 'Server', capacity = 1)
    1299          event = SimEvent('Renege_trigger') #never gets fired
    1300          initialize()
    1301          usetime = 5
    1302          j1 = JobEvt(server = res, name = 'Job_1')
    1303          activate(j1, j1.execute(event = event, usetime = usetime))
    1304          j2 = JobEvt(server = res, name = 'Job_2')
    1305          activate(j2, j2.execute(event = event, usetime = usetime))
    1306          simulate(until = 2 * usetime)
    1307          # Both jobs should get server (in sequence)
    1308          assert now() == 2 * usetime, 'time not == 2 * usetime'
    1309          assert j1.gotResource and j2.gotResource,\
    1310              'at least one job failed to get resource'
    1311          assert not (res.waitQ or res.activeQ),\
    1312              'job waiting or using resource'
    1313
    1314 -    def testNoEventM(self):
    1315          """Test that processes acquire resource normally if no event fires.
    1316             Resource monitored.
    1317          """
    1318          res = Resource(name = 'Server', capacity = 1, monitored = True)
    1319          event = SimEvent('Renege_trigger') #never gets fired
    1320          initialize()
    1321          usetime = 5
    1322          j1 = JobEvt(server = res, name = 'Job_1')
    1323          activate(j1, j1.execute(event = event, usetime = usetime))
    1324          j2 = JobEvt(server = res, name = 'Job_2')
    1325          activate(j2, j2.execute(event = event, usetime = usetime))
    1326          simulate(until = 2 * usetime)
    1327          # Both jobs should get server (in sequence)
    1328          assert now() == 2 * usetime, 'time not == 2 * usetime'
    1329          assert j1.gotResource and j2.gotResource,\
    1330              'at least one job failed to get resource'
    1331          assert not (res.waitQ or res.activeQ),\
    1332              'job waiting or using resource'
    1333          assert res.waitMon == [[0, 1],[usetime, 0]],'res.waitMoni is wrong: %s'%res.waitMon
    1334
    1335 -    def testWaitEvent1(self):
    1336          """Test that signalled event leads to renege when resource busy
    1337          """
    1338          res = Resource(name = 'Server', capacity = 1)
    1339          initialize()
    1340          event = SimEvent('Renege_trigger')
    1341          usetime = 5
    1342          eventtime = 1
    1343          j1 = JobEvt(server = res, name = 'Job_1')
    1344          activate(j1, j1.execute(event = event, usetime = usetime))
    1345          j2 = JobEvt(server = res, name = 'Job_2')
    1346          activate(j2, j2.execute(event = event, usetime = usetime))
    1347          f = FireEvent(name = 'FireEvent')
    1348          activate(f, f.fire(fireDelay = eventtime, event = event))
    1349          simulate(until = 2 * usetime)
    1350          # Job_1 should get server, Job_2 renege
    1351          assert(now() == usetime),'time not == usetime'
    1352          assert(j1.gotResource),'Job_1 did not get resource'
    1353          assert(not j2.gotResource),'Job_2 did not renege'
    1354          assert not (res.waitQ or res.activeQ),\
    1355              'job waiting or using resource'
    1356
    1357 -    def testWaitEvent1M(self):
    1358          """Test that signalled event leads to renege when resource busy.
    1359             Resource monitored.
    1360          """
    1361          res = Resource(name = 'Server', capacity = 1, monitored = True)
    1362          initialize()
    1363          event = SimEvent('Renege_trigger')
    1364          usetime = 5
    1365          eventtime = 1
    1366          j1 = JobEvt(server = res, name = 'Job_1')
    1367          activate(j1, j1.execute(event = event, usetime = usetime))
    1368          j2 = JobEvt(server = res, name = 'Job_2')
    1369          activate(j2, j2.execute(event = event, usetime = usetime))
    1370          f = FireEvent(name = 'FireEvent')
    1371          activate(f, f.fire(fireDelay = eventtime, event = event))
    1372          simulate(until = 2 * usetime)
    1373          # Job_1 should get server, Job_2 renege
    1374          assert(now() == usetime),'time not == usetime'
    1375          assert(j1.gotResource),'Job_1 did not get resource'
    1376          assert(not j2.gotResource),'Job_2 did not renege'
    1377          assert not (res.waitQ or res.activeQ),\
    1378              'job waiting or using resource'
    1379          assert res.waitMon == [[0, 1],[eventtime, 0]],'res.waitMon is wrong: %s'%res.waitMon
    1380
    1381 -    def testWaitEvent2(self):
    1382          """Test that renege - triggering event can be one of an event list
    1383          """
    1384          res = Resource(name = 'Server', capacity = 1)
    1385          initialize()
    1386          event1 = SimEvent('Renege_trigger_1')
    1387          event2 = SimEvent('Renege_trigger_2')
    1388          usetime = 5
    1389          eventtime = 1 #for both events
    1390          j1 = JobEvtMulti(server = res, name = 'Job_1')
    1391          activate(j1, j1.execute(eventlist = [event1, event2],usetime = usetime))
    1392          j2 = JobEvtMulti(server = res, name = 'Job_2')
    1393          activate(j2, j2.execute(eventlist = [event1, event2],usetime = usetime))
    1394          f1 = FireEvent(name = 'FireEvent_1')
    1395          activate(f1, f1.fire(fireDelay = eventtime, event = event1))
    1396          f2 = FireEvent(name = 'FireEvent_2')
    1397          activate(f2, f2.fire(fireDelay = eventtime, event = event2))
    1398          simulate(until = 2 * usetime)
    1399          # Job_1 should get server, Job_2 should renege
    1400          assert(now() == usetime),'time not == usetime'
    1401          assert(j1.gotResource),'Job_1 did not get resource'
    1402          assert(not j2.gotResource),'Job_2 did not renege'
    1403          assert not (res.waitQ or res.activeQ),\
    1404              'job waiting or using resource'
    1405
    1406 -    def testWaitEvent2M(self):
    1407          """Test that renege - triggering event can be one of an event list.
    1408             Resource monitored.
    1409          """
    1410          res = Resource(name = 'Server', capacity = 1, monitored = True)
    1411          initialize()
    1412          event1 = SimEvent('Renege_trigger_1')
    1413          event2 = SimEvent('Renege_trigger_2')
    1414          usetime = 5
    1415          eventtime = 1 #for both events
    1416          j1 = JobEvtMulti(server = res, name = 'Job_1')
    1417          activate(j1, j1.execute(eventlist = [event1, event2],usetime = usetime))
    1418          j2 = JobEvtMulti(server = res, name = 'Job_2')
    1419          activate(j2, j2.execute(eventlist = [event1, event2],usetime = usetime))
    1420          f1 = FireEvent(name = 'FireEvent_1')
    1421          activate(f1, f1.fire(fireDelay = eventtime, event = event1))
    1422          f2 = FireEvent(name = 'FireEvent_2')
    1423          activate(f2, f2.fire(fireDelay = eventtime, event = event2))
    1424          simulate(until = 2 * usetime)
    1425          # Job_1 should get server, Job_2 should renege
    1426          assert(now() == usetime),'time not == usetime'
    1427          assert(j1.gotResource),'Job_1 did not get resource'
    1428          assert(not j2.gotResource),'Job_2 did not renege'
    1429          assert not (res.waitQ or res.activeQ),\
    1430              'job waiting or using resource'
    1431          assert res.waitMon == [[0, 1],[eventtime, 0]],'res.waitMon is wrong: %s'%res.waitMon
    1432
    1433 -def makeEvtRenegeSuite():
    1434      suite = unittest.TestSuite()
    1435      testNoEvent = makeEventRenegeTestcase('testNoEvent')
    1436      testNoEventM = makeEventRenegeTestcase('testNoEventM')
    1437      testWaitEvent1 = makeEventRenegeTestcase('testWaitEvent1')
    1438      testWaitEvent1M = makeEventRenegeTestcase('testWaitEvent1M')
    1439      testWaitEvent2 = makeEventRenegeTestcase('testWaitEvent2')
    1440      testWaitEvent2M = makeEventRenegeTestcase('testWaitEvent2M')
    1441
    1442      suite.addTests([testNoEvent, testNoEventM, testWaitEvent1, testWaitEvent1M,
    1443                      testWaitEvent2, testWaitEvent2M])
    1444      return suite
    1445
    1446  #---Buffer tests (post 1.6.1)-------------------------------------
    1447  ## ------------------------------------------------------------------
    1448  ##             TEST 'yield get, self, level, whatToGet' and
    1449  ##                  'yield put, self, level, whatToPut, priority'
    1450  ##             for Level instances
    1451  ## ------------------------------------------------------------------
    1452 -class Producer(Process):
    1453      produced = 0
    1454 -    def produce(self, buffer):
    1455          for i in range(4):
    1456              Producer.produced += 1
    1457              yield put, self, buffer
    1458              yield hold, self, 1
    1459 -    def producePriority(self, buffer, priority):
    1460          """PriorityQ for Producers"""
    1461          Producer.produced += 4
    1462          yield put, self, buffer, 4,priority
    1463          yield hold, self, 1
    1464          self.done = now()
    1465          doneList.append(self.name)
    1466 -    def produce1(self, buffer):
    1467          for i in range(4):
    1468              yield put, self, buffer, 4
    1469              yield hold, self, 1
    1470 -class Consumer(Process):
    1471      consumed = 0
    1472 -    def consume(self, buffer):
    1473          """FIFO"""
    1474          yield get, self, buffer
    1475          Consumer.consumed += 1
    1476          assert self.got == 1, 'wrong self.got: %s'%self.got
    1477          yield get, self, buffer, 3
    1478          Consumer.consumed += 3
    1479          assert self.got == 3, 'wrong self.got: %s'%self.got
    1480
    1481 -    def consume1(self, buffer):
    1482          """producer PriorityQ, consumer FIFO"""
    1483          while True:
    1484              yield get, self, buffer, 2
    1485              yield hold, self, 1
    1486 -    def consumePriority(self, buffer, priority):
    1487          """PriorityQ for Consumers"""
    1488          yield get, self, buffer, 4,priority
    1489          doneList.append(self.name)
    1490
    1491  ### Begin classes for testConPrinciple (Level) ###
    1492 -class  ProducerPrincL(Process):
    1493 -    def produce(self, buffer, productionTime):
    1494          while True:
    1495              assert not(buffer.amount > 0 and len(buffer.getQ) > 0),\
    1496                  'Consumer(s) waiting while buffer not empty'
    1497              yield hold, self, productionTime
    1498              yield put, self, buffer, 1
    1499
    1500 -class ConsumerPrincL(Process):
    1501 -    def consume(self, buffer, consumptionTime):
    1502          while True:
    1503              assert not(buffer.amount == 0 and len(buffer.putQ) > 0),\
    1504                  'Producer(s) waiting while buffer empty'
    1505              yield get, self, buffer, 1
    1506              yield hold, self, consumptionTime
    1507
    1508  ### End classes for testConPrinciple (Level) ###
    1509
    1510 -class makeLevelTestcase(unittest.TestCase):
    1511 -    def testStatic(self):
    1512          """Tests initialization of Level instances
    1513          """
    1514          a = Level()
    1515          assert a.capacity == sys.maxint, 'wrong capacity:%s'%a
    1516          assert a.amount == 0, 'wrong buffer content: %s'%a
    1517          assert a.name == 'a_level', 'wrong name: %s'%a
    1518          assert not a.monitored, 'should not be monitored: %s'%a
    1519          assert a.putQMon is None, 'should not have putQMon: %s'%a
    1520          assert a.getQMon is None, 'should not have getQMon: %s'%a
    1521          assert a.bufferMon is None, 'should not have bufferMon: %s'%a
    1522          assert a.putQType.__name__ == 'FIFO' and a.getQType.__name__ == 'FIFO',\
    1523                 'putQType and getQType should be FIFO: %s'%a
    1524
    1525          b = Level(name = 'b', initialBuffered = 10.0, monitored = True, capacity = 12,
    1526                      putQType = PriorityQ)
    1527          a = Level()
    1528          assert b.capacity == 12, 'wrong capacity:%s'%b
    1529          assert b.amount == 10, 'wrong buffer content: %s'%b
    1530          assert b.name == 'b', 'wrong name: %s'%b
    1531          assert b.monitored, 'should be monitored: %s'%b
    1532          assert not (b.putQMon is None),'should have putQMon: %s'%b
    1533          assert not (b.getQMon is None),'should have getQMon: %s'%b
    1534          assert not (b.bufferMon is None),'should have bufferMon: %s'%b
    1535          assert b.putQType.__name__ == 'PriorityQ',\
    1536                 'putQType should be PriorityQ: %s'%b
    1537          assert b.getQType.__name__ == 'FIFO',\
    1538                  'getQType should be PriorityQ: %s'%b
    1539
    1540 -    def testConProdPrinciple(self):
    1541          """Level: tests basic Producer / Consumer principles:
    1542          -   Consumers must not be waiting while Level buffer value > 0,
    1543          -   Producers must not be waiting while Level buffer value == 0
    1544          """
    1545          bufferSize = 1
    1546          productionTime = 1
    1547          consumptionTime = 5
    1548          endtime = 50
    1549
    1550          initialize()
    1551          buffer = Level(capacity = bufferSize)
    1552          consumer = ConsumerPrincL()
    1553          activate(consumer, consumer.consume(buffer, consumptionTime))
    1554          producer = ProducerPrincL()
    1555          activate(producer, producer.produce(buffer, productionTime))
    1556          simulate(until = endtime)
    1557
    1558 -    def testConProd1(self):
    1559          """Level: tests put / get in 1 Producer / 1 Consumer scenario"""
    1560          initialize()
    1561          buffer = Level(initialBuffered = 0)
    1562          p = Producer()
    1563          activate(p, p.produce(buffer))
    1564          c = Consumer()
    1565          activate(c, c.consume(buffer))
    1566          simulate(until = 100)
    1567          assert Producer.produced - Consumer.consumed == buffer.amount,\
    1568              'items produced / consumed / buffered do not tally: %s %s %s'\
    1569                  %(Producer.produced, Consumer.consumed, buffer.amount)
    1570
    1571 -    def testConProdM(self):
    1572          """Level: tests put / get in multiple Producer / Consumer scenario"""
    1573          initialize()
    1574          buffer = Level(initialBuffered = 0)
    1575          Producer.produced = 0
    1576          Consumer.consumed = 0
    1577          for i in range(2):
    1578              c = Consumer()
    1579              activate(c, c.consume(buffer))
    1580          for i in range(3):
    1581              p = Producer()
    1582              activate(p, p.produce(buffer))
    1583          simulate(until = 10)
    1584          assert Producer.produced - Consumer.consumed == buffer.amount,\
    1585              'items produced / consumed / buffered do not tally: %s %s %s'\
    1586                  %(Producer.produced, Consumer.consumed, buffer.amount)
    1587
    1588 -    def testConProdPriorM(self):
    1589          """Level: tests put / get in multiple Producer / Consumer scenario,
    1590          with Producers having different priorities.
    1591          How: Producers forced to queue; all after first should be done in
    1592          priority order
    1593          """
    1594          global doneList
    1595          doneList = []
    1596          initialize()
    1597          buffer = Level(capacity = 7, putQType = PriorityQ, monitored = True)
    1598          for i in range(4):
    1599              p = Producer(str(i))
    1600              pPriority = i
    1601              activate(p, p.producePriority(buffer = buffer, priority = pPriority))
    1602          c = Consumer()
    1603          activate(c, c.consume1(buffer = buffer))
    1604          simulate(until = 100)
    1605          assert doneList == ["0","3","2","1"],'puts were not done in priority order: %s'\
    1606                                      %doneList
    1607
    1608 -    def testConPriorProdM(self):
    1609          """Level: tests put / get in multiple Producer / Consumer scenario, with
    1610          Consumers having different priorities.
    1611          How: Consumers forced to queue; all after first should be done in
    1612          priority order
    1613          """
    1614          global doneList
    1615          doneList = []
    1616          initialize()
    1617          buffer = Level(capacity = 7, getQType = PriorityQ, monitored = True)
    1618          for i in range(4):
    1619              c = Consumer(str(i))
    1620              cPriority = i
    1621              activate(c, c.consumePriority(buffer = buffer, priority = cPriority))
    1622          p = Producer()
    1623          activate(p, p.produce1(buffer = buffer))
    1624          simulate(until = 100)
    1625          assert doneList == ["3","2","1","0"],'gets were not done in priority order: %s'\
    1626                                      %doneList
    1627
    1628 -def makeLevelSuite():
    1629      suite = unittest.TestSuite()
    1630      testStatic = makeLevelTestcase('testStatic')
    1631      testConProdPrinciple = makeLevelTestcase('testConProdPrinciple')
    1632      testConProd1 = makeLevelTestcase('testConProd1')
    1633      testConProdM = makeLevelTestcase('testConProdM')
    1634      testConProdPriorM = makeLevelTestcase('testConProdPriorM')
    1635      testConPriorProdM = makeLevelTestcase('testConPriorProdM')
    1636      suite.addTests([testStatic, testConProdPrinciple, testConProd1,
    1637                      testConProdM, testConProdPriorM,
    1638                      testConPriorProdM])
    1639      return suite
    1640
    1641  ## ------------------------------------------------------------------
    1642  ##             TEST 'yield get, self, store, whatToGet' and
    1643  ##                  'yield put, self, store, whatToPut'
    1644  ##             for Store instances
    1645  ## ------------------------------------------------------------------
    1646
    1647 -class ProducerWidget(Process):
    1648      produced = 0
    1649 -    def produce(self, buffer):
    1650          for i in range(4):
    1651              ProducerWidget.produced += 1
    1652              yield put, self, buffer,[Widget(weight = 5)]
    1653              yield hold, self, 1
    1654 -    def producePriority(self, buffer, priority):
    1655          """PriorityQ for Producers"""
    1656          ProducerWidget.produced += 4
    1657          toStore = [Widget(weight = 5)] * 4
    1658          yield put, self, buffer, toStore, priority
    1659          yield hold, self, 1
    1660          self.done = now()
    1661          doneList.append(self.name)
    1662 -    def produce1(self, buffer):
    1663          for i in range(4):
    1664              yield put, self, buffer,[Widget(weight = 5)] * 4
    1665              yield hold, self, 1
    1666 -    def produceUnordered(self, buffer):
    1667          produced = [Widget(weight = i) for i in [9, 1,8, 2,7, 3,6, 4,5]]
    1668          yield put, self, buffer, produced
    1669
    1670 -class ConsumerWidget(Process):
    1671      consumed = 0
    1672 -    def consume(self, buffer):
    1673          """FIFO"""
    1674          yield get, self, buffer
    1675          ConsumerWidget.consumed += 1
    1676          assert len(self.got) == 1, 'wrong self.got: %s'%self.got
    1677          yield get, self, buffer, 3
    1678          ConsumerWidget.consumed += 3
    1679          assert len(self.got) == 3, 'wrong self.got: %s'%self.got
    1680
    1681 -    def consume1(self, buffer):
    1682          """producer PriorityQ, consumer FIFO"""
    1683          while True:
    1684              yield get, self, buffer, 2
    1685              yield hold, self, 1
    1686
    1687 -    def consumePriority(self, buffer, priority):
    1688          """PriorityQ for Consumers"""
    1689          yield get, self, buffer, 4,priority
    1690          doneList.append(self.name)
    1691
    1692 -    def consumeSorted(self, buffer, gotten):
    1693          yield get, self, buffer
    1694          gotten.append(self.got[0].weight)
    1695
    1696 -class Widget:
    1697 -    def __init__(self, weight):
    1698          self.weight = weight
    1699
    1700 -def mySortFunc(self, par):
    1701      """Sorts Widget instances by weight attribute."""
    1702      tmplist = [(x.weight, x) for x in par]
    1703      tmplist.sort()
    1704      return [x for (key, x) in tmplist]
    1705
    1706  ### Begin classes for testConPrinciple (Store) ###
    1707 -class  ProducerPrincS(Process):
    1708 -    def produce(self, buffer, productionTime):
    1709          while True:
    1710              assert not(buffer.nrBuffered > 0 and len(buffer.getQ) > 0),\
    1711                  'Consumer(s) waiting while buffer not empty'
    1712              yield hold, self, productionTime
    1713              product = WidgetPrinc()
    1714              yield put, self, buffer,[product]
    1715
    1716 -class ConsumerPrincS(Process):
    1717 -    def consume(self, buffer, consumptionTime):
    1718          while True:
    1719              assert not(buffer.nrBuffered == 0 and buffer.putQ),\
    1720                  'Producer(s) waiting while buffer empty'
    1721              yield get, self, buffer, 1
    1722              yield hold, self, consumptionTime
    1723
    1724 -class WidgetPrinc:
    1725      pass
    1726
    1727 -class FilterConsumer(Process):
    1728      """Used in testBufferFilter"""
    1729 -    class Widget:
    1730 -        def __init__(self, weighs):
    1731              self.weight = weighs
    1732
    1733 -    def getItems(self, store, a,b):
    1734          """get all items with weight between a and b"""
    1735          def between_a_and_b(buf):
    1736              res = []
    1737              for item in buf:
    1738                  if a < item.weight < b:
    1739                      res.append(item)
    1740
    1741          all = store.buffered
    1742          yield get, self, store, between_a_and_b
    1743          'All retrieved items weight in range?'
    1744          for it in self.got:
    1745              assert a < it.weight < b, 'weight %s not in range %s..%s'\
    1746                                   %(it.weight, a,b)
    1747          'Any item fitting filter pred left in buffer?'
    1748          for it in store.buffer:
    1749              assert not (a < it.weight < b),\
    1750                  'item left in buffer which fits filter (%s<%s<%s)'\
    1751                  %(a, it.weight, b)
    1752          'All items either in store.buffer of self.got?'
    1753          for it in all:
    1754              assert (it in self.buffer) or (it in self.got),\
    1755                     'item w. weight %s neither in store nor in got'%it.weight
    1756
    1757  ### End classes for testConPrinciple (Store) ###
    1758
    1759 -class makeStoreTestcase(unittest.TestCase):
    1760 -    def testStatic(self):
    1761          """Store: tests initialization of Store instances
    1762          """
    1763          a = Store()
    1764          assert a.capacity == sys.maxint, 'wrong capacity:%s'%a
    1765          assert a.nrBuffered == 0, 'wrong buffer content: %s'%a
    1766          assert a.name == 'a_store', 'wrong name: %s'%a
    1767          assert not a.monitored, 'should not be monitored: %s'%a
    1768          assert a.putQMon is None, 'should not have putQMon: %s'%a
    1769          assert a.getQMon is None, 'should not have getQMon: %s'%a
    1770          assert a.bufferMon is None, 'should not have bufferMon: %s'%a
    1771          assert a.putQType.__name__ == 'FIFO' and a.getQType.__name__ == 'FIFO',\
    1772                 'putQType and getQType should be FIFO: %s'%a
    1773
    1774          stored = [Widget(weight = 5)] * 10
    1775          b = Store(name = 'b', initialBuffered = stored, monitored = True, capacity = 12,
    1776                      putQType = PriorityQ)
    1777          assert b.capacity == 12, 'wrong capacity:%s'%b
    1778          assert b.nrBuffered == 10, 'wrong buffer content: %s'%b
    1779          assert b.name == 'b', 'wrong name: %s'%b
    1780          assert b.monitored, 'should be monitored: %s'%b
    1781          assert not (b.putQMon is None),'should have putQMon: %s'%b
    1782          assert not (b.getQMon is None),'should have getQMon: %s'%b
    1783          assert not (b.bufferMon is None),'should have bufferMon: %s'%b
    1784          assert b.putQType.__name__ == 'PriorityQ',\
    1785                 'putQType should be PriorityQ: %s'%b
    1786          assert b.getQType.__name__ == 'FIFO',\
    1787                  'getQType should be PriorityQ: %s'%b
    1788
    1789 -    def testConProdPrinciple(self):
    1790          """Store: tests basic Producer / Consumer principles:
    1791          -   Consumers must not be waiting while items in Store buffer,
    1792          -   Producers must not be waiting while space available in Store buffer
    1793          """
    1794          bufferSize = 1
    1795          productionTime = 1
    1796          consumptionTime = 5
    1797          endtime = 50
    1798
    1799          initialize()
    1800          buffer = Store(capacity = bufferSize)
    1801          consumer = ConsumerPrincS()
    1802          activate(consumer, consumer.consume(buffer, consumptionTime))
    1803          producer = ProducerPrincS()
    1804          activate(producer, producer.produce(buffer, productionTime))
    1805          simulate(until = endtime)
    1806
    1807 -    def testConProd1(self):
    1808          """Store: tests put / get in 1 Producer / 1 Consumer scenario"""
    1809          initialize()
    1810          buffer = Store(initialBuffered = [])
    1811          p = ProducerWidget()
    1812          activate(p, p.produce(buffer))
    1813          c = ConsumerWidget()
    1814          activate(c, c.consume(buffer))
    1815          simulate(until = 100)
    1816          assert \
    1817             ProducerWidget.produced - ConsumerWidget.consumed == buffer.nrBuffered,\
    1818             'items produced / consumed / buffered do not tally: %s %s %s'\
    1819             %(ProducerWidget.produced, ConsumerWidget.consumed, buffer.nrBuffered)
    1820
    1821 -    def testConProdM(self):
    1822          """Store: tests put / get in multiple Producer / Consumer scenario"""
    1823          initialize()
    1824          buffer = Store(initialBuffered = [])
    1825          ProducerWidget.produced = 0
    1826          ConsumerWidget.consumed = 0
    1827          for i in range(2):
    1828              c = ConsumerWidget()
    1829              activate(c, c.consume(buffer))
    1830          for i in range(3):
    1831              p = ProducerWidget()
    1832              activate(p, p.produce(buffer))
    1833          simulate(until = 10)
    1834          assert ProducerWidget.produced - ConsumerWidget.consumed == buffer.nrBuffered,\
    1835              'items produced / consumed / buffered do not tally: %s %s %s'\
    1836              %(ProducerWidget.produced, ConsumerWidget.consumed, buffer.nrBuffered)
    1837
    1838 -    def testConProdPriorM(self):
    1839          """Store: Tests put / get in multiple Producer / Consumer scenario,
    1840          with Producers having different priorities.
    1841          How; Producers forced to queue; all after first should be done in
    1842          priority order
    1843          """
    1844          global doneList
    1845          doneList = []
    1846          initialize()
    1847          buffer = Store(capacity = 7, putQType = PriorityQ, monitored = True)
    1848          for i in range(4):
    1849              p = ProducerWidget(str(i))
    1850              pPriority = i
    1851              activate(p, p.producePriority(buffer = buffer, priority = pPriority))
    1852          c = ConsumerWidget()
    1853          activate(c, c.consume1(buffer = buffer))
    1854          simulate(until = 100)
    1855          assert doneList == ["0","3","2","1"],'puts were not done in priority order: %s'\
    1856                                      %doneList
    1857
    1858 -    def testConPriorProdM(self):
    1859          """Tests put / get in multiple Producer / Consumer scenario, with
    1860          Consumers having different priorities.
    1861          How; Consumers forced to queue; all after first should be done in
    1862          priority order
    1863          """
    1864          global doneList
    1865          doneList = []
    1866          initialize()
    1867          buffer = Store(capacity = 7, getQType = PriorityQ, monitored = True)
    1868          for i in range(4):
    1869              c = ConsumerWidget(str(i))
    1870              cPriority = i
    1871              activate(c, c.consumePriority(buffer = buffer, priority = cPriority))
    1872          p = ProducerWidget()
    1873          activate(p, p.produce1(buffer = buffer))
    1874          simulate(until = 100)
    1875          assert doneList == ['3', '2', '1', '0'],\
    1876                'gets were not done in priority order: %s'%doneList
    1877
    1878 -    def testBufferSort(self):
    1879          """Tests the optional sorting of theBuffer by applying a user - defined
    1880          sort function."""
    1881          initialize()
    1882          gotten = []
    1883          sortedStore = Store()
    1884          sortedStore.addSort(mySortFunc)
    1885          p = ProducerWidget()
    1886          activate(p, p.produceUnordered(sortedStore))
    1887          for i in range(9):
    1888              c = ConsumerWidget()
    1889              activate(c, c.consumeSorted(buffer = sortedStore, gotten = gotten),at = 1)
    1890          simulate(until = 10)
    1891          assert gotten == [1, 2,3, 4,5, 6,7, 8,9],'sort wrong: %s'%gotten
    1892
    1893 -    def testBufferFilter(self):
    1894          """Tests get from a Store with a filter function
    1895          """
    1896          initialize()
    1897          ItClass = FilterConsumer.Widget
    1898          all = [ItClass(1),ItClass(4),ItClass(6),ItClass(12)]
    1899          st = Store(initialBuffered = all)
    1900          fc = FilterConsumer()
    1901          minw = 2;maxw = 10
    1902          activate(fc, fc.getItems(store = st, a = minw, b = maxw))
    1903          simulate(until = 1)
    1904
    1905 -def makeStoreSuite():
    1906      suite = unittest.TestSuite()
    1907      testStatic = makeStoreTestcase('testStatic')
    1908      testConProdPrinciple = makeStoreTestcase('testConProdPrinciple')
    1909      testConProd1 = makeStoreTestcase('testConProd1')
    1910      testConProdM = makeStoreTestcase('testConProdM')
    1911      testConProdPriorM = makeStoreTestcase('testConProdPriorM')
    1912      testConPriorProdM = makeStoreTestcase('testConPriorProdM')
    1913      testBufferSort = makeStoreTestcase('testBufferSort')
    1914      testBufferFilter = makeStoreTestcase('testBufferFilter')
    1915      suite.addTests([testStatic, testConProdPrinciple, testConProd1,
    1916                      testConProdM, testConProdPriorM,
    1917                      testConPriorProdM, testBufferSort,
    1918                      testBufferFilter])
    1919      return suite
    1920
    1921  ## ------------------------------------------------------------------
    1922  ##
    1923  ##  Store: Tests for compound get / put
    1924  ##
    1925  ## ------------------------------------------------------------------
    1926 -class TBT(Process):
    1927      """Store: For testBasicTime"""
    1928 -    def tbt(self, store):
    1929          yield get, self, store, 1
    1930          assert self.got, 'Did not get Item'
    1931          yield (get, self, store, 1),(hold, self, 5)
    1932          if self.acquired(store):
    1933              assert len(self.got) == 1, 'did not get 1 Item'
    1934          else:
    1935              assert not self.got and now() == 5 and not store.getQ,\
    1936                     'time renege not working'
    1937
    1938 -class TBE(Process):
    1939      """Store: For testBasicEvent"""
    1940 -    def tbe(self, store, trigger):
    1941          yield get, self, store, 1
    1942          assert self.got, 'Did not get Item'
    1943          yield (get, self, store, 1),(waitevent, self, trigger)
    1944          if self.acquired(store):
    1945              assert False, 'should have reneged'
    1946          else:
    1947              assert self.eventsFired[0] == trigger and now() == 5 \
    1948                  and not store.getQ, 'event renege not working'
    1949
    1950 -class TBEtrigger(Process):
    1951      """Store: For testBasicEvent"""
    1952 -    def fire(self, trigger):
    1953          yield hold, self, 5
    1954          trigger.signal()
    1955
    1956 -class TBTput(Process):
    1957      """Store: for testBasicTimePut"""
    1958 -    def tbt(self, store):
    1959          class Item:pass
    1960          yield (put, self, store,[Item()]),(hold, self, 4)
    1961          if self.stored(store):
    1962              assert store.nrBuffered == 1 and not store.putQ,\
    1963                     'put did not execute'
    1964          else:
    1965              assert False, 'should not have reneged'
    1966          yield (put, self, store,[Item()]),(hold, self, 5)
    1967          if self.stored(store):
    1968              assert False, 'should have reneged'
    1969          else:
    1970              assert store.nrBuffered == 1 and not store.putQ,\
    1971                     'renege not working correctly'
    1972
    1973 -class TBEput(Process):
    1974      """Store: for testBasicEventPut"""
    1975 -    def tbe(self, store, trigger):
    1976          class Item:pass
    1977          yield (put, self, store,[Item()]),(waitevent, self, trigger)
    1978          if self.stored(store):
    1979              assert store.nrBuffered == 1 and not store.putQ,\
    1980                     'put did not execute'
    1981          else:
    1982              assert False, 'should have not have reneged'
    1983          yield (put, self, store,[Item()]),(waitevent, self, trigger)
    1984          if self.stored(store):
    1985              assert False, 'should have reneged'
    1986          else:
    1987              assert now() == 5 and self.eventsFired[0] == trigger\
    1988                     and not store.putQ, 'renege not working correctly'
    1989
    1990 -class TBEtriggerPut(Process):
    1991      """Store: For testBasicEventPut"""
    1992 -    def fire(self, trigger):
    1993          yield hold, self, 5
    1994          trigger.signal()
    1995
    1996 -class makeStoreCompTestcase(unittest.TestCase):
    1997      """Store: Testcase for compound get statements"""
    1998      ## ------------------------------------------------------------------
    1999      ##             TEST 'yield (get, self, store),(hold, self, time)'
    2000      ##                   == timeout renege
    2001      ##             for both unmonitored and monitored Stores
    2002      ## ------------------------------------------------------------------
    2003
    2004 -    def testBasicTime(self):
    2005          """Store (unmonitored):
    2006          test 'yield (get, self, store),(hold, self, timeout)"""
    2007          class Item:pass
    2008          initialize()
    2009          st = Store(initialBuffered = [Item()])
    2010          t = TBT()
    2011          activate(t, t.tbt(store = st))
    2012          simulate(until = 10)
    2013
    2014
    2015      ## ------------------------------------------------------------------
    2016      ##             TEST 'yield (put, self, store),(hold, self, time)'
    2017      ##                   == timeout renege
    2018      ##             for both unmonitored and monitored Stores
    2019      ## ------------------------------------------------------------------
    2020 -    def testBasicTimePut(self):
    2021          """Store (unmonitored):
    2022          test 'yield (put, self, store),(hold, self, time)"""
    2023          initialize()
    2024          st = Store(capacity = 1)
    2025          t = TBTput()
    2026          activate(t, t.tbt(store = st))
    2027          simulate(until = 10)
    2028
    2029 -    def testBasicTimePutM(self):
    2030          """Store (monitored):
    2031          test monitors with 'yield (put, self, store),(hold, self, time)"""
    2032          initialize()
    2033          st = Store(capacity = 1, monitored = True)
    2034          t = TBTput()
    2035          activate(t, t.tbt(store = st))
    2036          simulate(until = 10)
    2037          #First put succeeds, second attempt reneges at t = 5?
    2038          assert st.putQMon == [[0, 0],[0, 1],[5, 0]],'putQMon wrong: %s'\
    2039                                                 %st.putQMon
    2040          #First Item goes into buffer at t = 0, second not (renege)?
    2041          assert st.bufferMon == [[0, 0],[0, 1]],'bufferMon wrong: %s'%st.bufferMon
    2042
    2043
    2044      ## ------------------------------------------------------------------
    2045      ##             TEST 'yield (get, self, store),(waitevent, self, event)'
    2046      ##                   == event renege
    2047      ##             for both unmonitored and monitored Stores
    2048      ## ------------------------------------------------------------------
    2049 -    def testBasicEvent(self):
    2050          """Store (unmonitored):
    2051          test 'yield (get, self, store),(waitevent, self, event)"""
    2052          class Item:pass
    2053          initialize()
    2054          st = Store(initialBuffered = [Item()])
    2055          trig = SimEvent()
    2056          t = TBE()
    2057          activate(t, t.tbe(store = st, trigger = trig))
    2058          tr = TBEtrigger()
    2059          activate(tr, tr.fire(trigger = trig))
    2060          simulate(until = 10)
    2061
    2062
    2063      ## ------------------------------------------------------------------
    2064      ##             TEST 'yield (put, self, store),(waitevent, self, event)'
    2065      ##                   == event renege
    2066      ##             for both unmonitored and monitored Stores
    2067      ## ------------------------------------------------------------------
    2068 -    def testBasicEventPut(self):
    2069          """Store (unmonitored):
    2070          test 'yield (put, self, store),(waitevent, self, event)"""
    2071          initialize()
    2072          s = SimEvent()
    2073          store = Store(capacity = 1)
    2074          t = TBEtriggerPut()
    2075          activate(t, t.fire(trigger = s))
    2076          tb = TBEput()
    2077          activate(tb, tb.tbe(store = store, trigger = s))
    2078          simulate(until = 10)
    2079
    2080 -    def testBasicEventPutM(self):
    2081          """Store (monitored):
    2082          test monitors with 'yield (put, self, store),(waitevent, self, event)"""
    2083          initialize()
    2084          s = SimEvent()
    2085          st = Store(capacity = 1, monitored = True)
    2086          t = TBEtriggerPut()
    2087          activate(t, t.fire(trigger = s))
    2088          tb = TBEput()
    2089          activate(tb, tb.tbe(store = st, trigger = s))
    2090          simulate(until = 10)
    2091          #First put succeeds, second attempt reneges at t = 5?
    2092          assert st.putQMon == [[0, 0],[0, 1],[5, 0]],'putQMon wrong: %s'\
    2093                                                 %st.putQMon
    2094          #First Item goes into buffer at t = 0, second not (renege)?
    2095          assert st.bufferMon == [[0, 0],[0, 1]],'bufferMon wrong: %s'%st.bufferMon
    2096
    2097 -def makeStoreCompSuite():
    2098      suite = unittest.TestSuite()
    2099      ## Unmonitored Stores
    2100      testBasicTime  = makeStoreCompTestcase('testBasicTime')
    2101      testBasicEvent = makeStoreCompTestcase('testBasicEvent')
    2102      testBasicTimePut  = makeStoreCompTestcase('testBasicTimePut')
    2103      testBasicEventPut = makeStoreCompTestcase('testBasicEventPut')
    2104      ## Monitored Stores
    2105      testBasicTimePutM = makeStoreCompTestcase('testBasicTimePutM')
    2106      testBasicEventPutM = makeStoreCompTestcase('testBasicEventPutM')
    2107
    2108      suite.addTests([testBasicTime,
    2109                      testBasicEvent,
    2110                      testBasicTimePut,
    2111                      testBasicEventPut,
    2112                      testBasicTimePutM,
    2113                      testBasicEventPutM])
    2114      return suite
    2115
    2116  ## ------------------------------------------------------------------
    2117  ##
    2118  ##  Level: Tests for compound get
    2119  ##
    2120  ## ------------------------------------------------------------------
    2121 -class TBTLev(Process):
    2122      """Level: For testBasicTime"""
    2123 -    def tbt(self, level):
    2124          yield get, self, level, 1
    2125          assert self.got, 'did not get 1 unit'
    2126          yield (get, self, level, 1),(hold, self, 5)
    2127          if self.acquired(level):
    2128              assert self.got == 1, 'did not get 1 unit'
    2129          else:
    2130              assert not self.got and now() == 5, 'time renege not working'
    2131
    2132 -class TBELev(Process):
    2133      """Level: For testBasicEvent"""
    2134 -    def tbe(self, level, trigger):
    2135          yield get, self, level, 1
    2136          assert self.got, 'did not get 1 unit'
    2137          yield (get, self, level, 1),(waitevent, self, trigger)
    2138          if self.acquired(level):
    2139              assert self.got == 1, 'did not get 1 Item'
    2140          else:
    2141              assert now() == 5.5 and self.eventsFired[0] == trigger,\
    2142                     'event renege not working'
    2143
    2144 -class TBEtriggerLev(Process):
    2145      """Level: For testBasicEvent"""
    2146 -    def fire(self, trigger):
    2147          yield hold, self, 5.5
    2148          trigger.signal()
    2149
    2150 -class TBTLevPut(Process):
    2151      """Level: For testBasicTimePut"""
    2152 -    def tbt(self, level):
    2153          yield put, self, level, 1
    2154          assert level.amount, 'did not put 1 unit'
    2155          yield (put, self, level, 1),(hold, self, 5)
    2156          if self.stored(level):
    2157              assert False, 'should have reneged'
    2158          else:
    2159              assert level.amount == 1 and now() == 5, 'time renege not working'
    2160
    2161 -class TBELevPut(Process):
    2162      """Level: For testBasicEventPut and testBasicEventPutM"""
    2163 -    def tbe(self, level, trigger):
    2164          yield (put, self, level, 1),(waitevent, self, trigger)
    2165          if self.stored(level):
    2166              assert level.amount == 1, 'did not put 1 unit'
    2167          else:
    2168              assert False, 'should not have reneged'
    2169          yield (put, self, level, 1),(waitevent, self, trigger)
    2170          if self.stored(level):
    2171              assert False, 'should have reneged'
    2172          else:
    2173              assert now() == 5.5 and self.eventsFired[0] == trigger ,\
    2174                     'renege not working'
    2175
    2176 -class TBEtriggerLevPut(Process):
    2177      """Level: For testBasicEventPut"""
    2178 -    def fire(self, trigger):
    2179          yield hold, self, 5.5
    2180          trigger.signal()
    2181
    2182 -class makeLevelCompTestcase(unittest.TestCase):
    2183      """Level: Testcase for compound get and put statements"""
    2184      ## ------------------------------------------------------------------
    2185      ##             TEST 'yield (get, self, level),(hold, self, time)'
    2186      ##                   == timeout renege
    2187      ##             for both unmonitored and monitored Levels
    2188      ## ------------------------------------------------------------------
    2189
    2190 -    def testBasicTime(self):
    2191          """Level (unmonitored): test 'yield (get, self, level),(hold, self, timeout)"""
    2192          initialize()
    2193          l = Level(initialBuffered = 1)
    2194          t = TBTLev()
    2195          activate(t, t.tbt(level = l))
    2196          simulate(until = 10)
    2197
    2198      ## ------------------------------------------------------------------
    2199      ##             TEST 'yield (put, self, store),(hold, self, time)'
    2200      ##                   == timeout renege
    2201      ##             for both unmonitored and monitored Stores
    2202      ## ------------------------------------------------------------------
    2203 -    def testBasicTimePut(self):
    2204          """Level (unmonitored):
    2205          test 'yield (put, self, level),(hold, self, timeout)"""
    2206          initialize()
    2207          l = Level(capacity = 1)
    2208          t = TBTLevPut()
    2209          activate(t, t.tbt(level = l))
    2210          simulate(until = 10)
    2211
    2212
    2213      ## ------------------------------------------------------------------
    2214      ##             TEST 'yield (get, self, store),(waitevent, self, event)'
    2215      ##                   == event renege
    2216      ##             for both unmonitored and monitored Levels
    2217      ## ------------------------------------------------------------------
    2218 -    def testBasicEvent(self):
    2219          """Level (unmonitored):
    2220          test 'yield (get, self, level),(waitevent, self, event)"""
    2221          initialize()
    2222          l = Level(initialBuffered = 1)
    2223          trig = SimEvent()
    2224          t = TBELev()
    2225          activate(t, t.tbe(level = l, trigger = trig))
    2226          tr = TBEtriggerLev()
    2227          activate(tr, tr.fire(trigger = trig))
    2228          simulate(until = 10)
    2229
    2230 -    def testBasicEventM(self):
    2231          """Level (monitored):
    2232          test monitors with 'yield (get, self, level),(waitevent, self, event)"""
    2233          initialize()
    2234          l = Level(initialBuffered = 1, monitored = True)
    2235          trig = SimEvent()
    2236          t = TBELev()
    2237          activate(t, t.tbe(level = l, trigger = trig))
    2238          tr = TBEtriggerLev()
    2239          activate(tr, tr.fire(trigger = trig))
    2240          simulate(until = 10)
    2241          #First get (t = 0) succeeded and second timed out at t = 5.5?
    2242          assert l.getQMon == [[0, 0],[0, 1],[5.5, 0]],'getQMon not working: %s'\
    2243                                                 %l.getQMon
    2244          #Level amount incr. then decr. by 1 (t = 0), 2nd get reneged at t = 5.5?
    2245          assert l.bufferMon == [[0, 1],[0, 0]],\
    2246                 'bufferMon not working: %s'%l.bufferMon
    2247
    2248      ## ------------------------------------------------------------------
    2249      ##             TEST 'yield (put, self, store),(waitevent, self, event)'
    2250      ##                   == event renege
    2251      ##             for both unmonitored and monitored Levels
    2252      ## ------------------------------------------------------------------
    2253 -    def testBasicEventPut(self):
    2254          """Level (unmonitored):
    2255          test 'yield (put, self, level),(waitevent, self, event)"""
    2256          initialize()
    2257          l = Level(capacity = 1)
    2258          trig = SimEvent()
    2259          t = TBELevPut()
    2260          activate(t, t.tbe(level = l, trigger = trig))
    2261          tr = TBEtriggerLevPut()
    2262          activate(tr, tr.fire(trigger = trig))
    2263          simulate(until = 10)
    2264
    2265 -    def testBasicEventPutM(self):
    2266          """Level (monitored):
    2267          test monitors with 'yield (put, self, level),(waitevent, self, event)"""
    2268          initialize()
    2269          l = Level(capacity = 1, monitored = True)
    2270          trig = SimEvent()
    2271          t = TBELevPut()
    2272          activate(t, t.tbe(level = l, trigger = trig))
    2273          tr = TBEtriggerLevPut()
    2274          activate(tr, tr.fire(trigger = trig))
    2275          simulate(until = 10)
    2276          'First put succeeds, second reneges at t = 5.5?'
    2277          assert l.putQMon == [[0, 0],[0, 1],[5.5, 0]],'putQMon wrong: %s'\
    2278                                                  %l.putQMon
    2279          '1 unit added at t = 0, renege at t = 5 before 2nd unit added?'
    2280          assert l.bufferMon == [[0, 0],[0, 1]],'bufferMon wrong: %s'%l.bufferMon
    2281
    2282 -def makeLevelCompSuite():
    2283      suite = unittest.TestSuite()
    2284      ## Unmonitored Levels
    2285      testBasicTime  = makeLevelCompTestcase('testBasicTime')
    2286      testBasicEvent = makeLevelCompTestcase('testBasicEvent')
    2287      testBasicTimePut = makeLevelCompTestcase('testBasicTimePut')
    2288      testBasicEventPut = makeLevelCompTestcase('testBasicEventPut')
    2289      ## Monitored Levels
    2290      testBasicEventM = makeLevelCompTestcase('testBasicEventM')
    2291      testBasicEventPutM = makeLevelCompTestcase('testBasicEventPutM')
    2292
    2293      suite.addTests([testBasicTime,
    2294                      testBasicEvent,
    2295                      testBasicTimePut,
    2296                      testBasicEventPut,
    2297                      testBasicEventM,
    2298                      testBasicEventPutM])
    2299      return suite
    2300
    2301  if __name__ == '__main__':
    2302      alltests = unittest.TestSuite((makeSSuite(),makeRSuite(),
    2303                                     makeMSuite(),#makeHSuite(),
    2304                                     makeISuite(),makePSuite(),
    2305                                     makeESuite(),makeWSuite(),
    2306                                     makeTOSuite(),makeEvtRenegeSuite(),
    2307                                     makeLevelSuite(),
    2308                                     makeStoreSuite(),
    2309                                     makeStoreCompSuite(),
    2310                                     makeLevelCompSuite()
    2311                                  ))
    2312      runner = unittest.TextTestRunner()
    2313      runner.run(alltests)
    2314