1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
"""
Test asynchronous interrupts.
"""
import re
import pytest
import simpy
def test_interruption(env):
"""Processes can be interrupted while waiting for other events."""
def interruptee(env):
try:
yield env.timeout(10)
pytest.fail('Expected an interrupt')
except simpy.Interrupt as interrupt:
assert interrupt.cause == 'interrupt!'
def interruptor(env):
child_process = env.process(interruptee(env))
yield env.timeout(5)
child_process.interrupt('interrupt!')
env.process(interruptor(env))
env.run()
def test_concurrent_interrupts(env, log):
"""Concurrent interrupts are scheduled in the order in which they
occurred.
"""
def fox(env, log):
while True:
try:
yield env.timeout(10)
except simpy.Interrupt as interrupt:
log.append((env.now, interrupt.cause))
def farmer(env, name, fox):
fox.interrupt(name)
yield env.timeout(1)
fantastic_mr_fox = env.process(fox(env, log))
for name in ('boggis', 'bunce', 'beans'):
env.process(farmer(env, name, fantastic_mr_fox))
env.run(20)
assert log == [(0, 'boggis'), (0, 'bunce'), (0, 'beans')]
def test_concurrent_interrupts_and_events(env, log):
"""Interrupts interrupt a process while waiting for an event. Even if the
event has happened concurrently with the interrupt."""
def fox(env, coup, log):
while True:
try:
yield coup
log.append('coup completed at %d' % env.now)
env.exit()
except simpy.Interrupt:
log.append('coup interrupted at %d' % env.now)
def master_plan(env, fox, coup):
yield env.timeout(1)
# Succeed and interrupt concurrently.
coup.succeed()
fox.interrupt()
coup = env.event()
fantastic_mr_fox = env.process(fox(env, coup, log))
env.process(master_plan(env, fantastic_mr_fox, coup))
env.run(5)
assert log == ['coup interrupted at 1', 'coup completed at 1']
def test_init_interrupt(env):
"""An interrupt should always be executed after the Initialize event at the
same time."""
def child(env):
try:
yield env.timeout(10)
pytest.fail('Should have been interrupted.')
except simpy.Interrupt:
assert env.now == 0
def root(env):
child_proc = env.process(child(env))
child_proc.interrupt()
yield env.timeout(1)
env.process(root(env))
env.run()
def test_interrupt_terminated_process(env):
"""Dead processes cannot be interrupted."""
def child(env):
yield env.timeout(1)
def parent(env):
child_proc = env.process(child(env))
# Wait long enough so that child_proc terminates.
yield env.timeout(2)
ei = pytest.raises(RuntimeError, child_proc.interrupt)
assert re.match(r'<Process\(child\) object at 0x.*> has terminated '
r'and cannot be interrupted.', ei.value.args[0])
yield env.timeout(1)
env.process(parent(env))
env.run()
def test_multiple_interrupts(env):
"""Interrupts on dead processes are discarded. If there are multiple
concurrent interrupts on a process and the latter dies after
handling the first interrupt, the remaining ones are silently
ignored.
"""
def child(env):
try:
yield env.timeout(1)
except simpy.Interrupt as i:
env.exit(i.cause)
def parent(env):
c = env.process(child(env))
yield env.timeout(0)
c.interrupt(1)
c.interrupt(2)
result = yield c
assert result == 1
env.process(parent(env))
env.run()
def test_interrupt_self(env):
"""A process should not be able to interrupt itself."""
def pem(env):
pytest.raises(RuntimeError, env.active_process.interrupt)
yield env.timeout(0)
env.process(pem(env))
env.run()
def test_immediate_interrupt(env, log):
"""Processes are immediately interruptable."""
def child(env, log):
try:
yield env.event()
except simpy.Interrupt:
log.append(env.now)
def parent(env, log):
child_proc = env.process(child(env, log))
child_proc.interrupt()
yield env.exit()
env.process(parent(env, log))
env.run()
# Confirm that child has been interrupted immediately at timestep 0.
assert log == [0]
def test_interrupt_event(env):
"""A process should be interruptable while waiting for an Event."""
def child(env):
try:
yield env.event()
except simpy.Interrupt:
assert env.now == 5
def parent(env):
child_proc = env.process(child(env))
yield env.timeout(5)
child_proc.interrupt()
env.process(parent(env))
env.run()
def test_concurrent_behaviour(env):
def proc_a(env):
timeouts = [env.timeout(0) for i in range(2)]
while timeouts:
try:
yield timeouts.pop(0)
assert False, 'Expected an interrupt'
except simpy.Interrupt:
pass
def proc_b(env, proc_a):
for i in range(2):
proc_a.interrupt()
yield env.exit()
proc_a = env.process(proc_a(env))
env.process(proc_b(env, proc_a))
env.run()
|