| 1 |
import errno |
| 2 |
import socket |
| 3 |
from queue import Empty |
| 4 |
from unittest.mock import Mock |
| 5 |
|
| 6 |
import pytest |
| 7 |
from kombu.asynchronous import ERR, READ, WRITE, Hub |
| 8 |
from kombu.exceptions import DecodeError |
| 9 |
|
| 10 |
from celery.bootsteps import CLOSE, RUN |
| 11 |
from celery.exceptions import InvalidTaskError, WorkerLostError, WorkerShutdown, WorkerTerminate |
| 12 |
from celery.platforms import EX_FAILURE, EX_OK |
| 13 |
from celery.worker import state |
| 14 |
from celery.worker.consumer import Consumer |
| 15 |
from celery.worker.loops import _quick_drain, asynloop, synloop |
| 16 |
|
| 17 |
|
| 18 |
class PromiseEqual: |
| 19 |
|
| 20 |
def __init__(self, fun, *args, **kwargs): |
| 21 |
self.fun = fun |
| 22 |
self.args = args |
| 23 |
self.kwargs = kwargs |
| 24 |
|
| 25 |
def __eq__(self, other): |
| 26 |
return (other.fun == self.fun and |
| 27 |
other.args == self.args and |
| 28 |
other.kwargs == self.kwargs) |
| 29 |
|
| 30 |
def __repr__(self): |
| 31 |
return '<promise: {0.fun!r} {0.args!r} {0.kwargs!r}>'.format(self) |
| 32 |
|
| 33 |
|
| 34 |
class X: |
| 35 |
|
| 36 |
def __init__(self, app, heartbeat=None, on_task_message=None, |
| 37 |
transport_driver_type=None): |
| 38 |
hub = Hub() |
| 39 |
( |
| 40 |
self.obj, |
| 41 |
self.connection, |
| 42 |
self.consumer, |
| 43 |
self.blueprint, |
| 44 |
self.hub, |
| 45 |
self.qos, |
| 46 |
self.heartbeat, |
| 47 |
self.clock, |
| 48 |
) = self.args = [Mock(name='obj'), |
| 49 |
Mock(name='connection'), |
| 50 |
Mock(name='consumer'), |
| 51 |
Mock(name='blueprint'), |
| 52 |
hub, |
| 53 |
Mock(name='qos'), |
| 54 |
heartbeat, |
| 55 |
Mock(name='clock')] |
| 56 |
self.connection.supports_heartbeats = True |
| 57 |
self.connection.get_heartbeat_interval.side_effect = ( |
| 58 |
lambda: self.heartbeat |
| 59 |
) |
| 60 |
self.consumer.callbacks = [] |
| 61 |
self.obj.strategies = {} |
| 62 |
self.connection.connection_errors = (socket.error,) |
| 63 |
if transport_driver_type: |
| 64 |
self.connection.transport.driver_type = transport_driver_type |
| 65 |
self.hub.readers = {} |
| 66 |
self.hub.timer = Mock(name='hub.timer') |
| 67 |
self.hub.timer._queue = [Mock()] |
| 68 |
self.hub.fire_timers = Mock(name='hub.fire_timers') |
| 69 |
self.hub.fire_timers.return_value = 1.7 |
| 70 |
self.hub.poller = Mock(name='hub.poller') |
| 71 |
self.hub.close = Mock(name='hub.close()') # asynloop calls hub.close |
| 72 |
self.Hub = self.hub |
| 73 |
self.blueprint.state = RUN |
| 74 |
# need this for create_task_handler |
| 75 |
self._consumer = _consumer = Consumer( |
| 76 |
Mock(), timer=Mock(), controller=Mock(), app=app) |
| 77 |
_consumer.on_task_message = on_task_message or [] |
| 78 |
self.obj.create_task_handler = _consumer.create_task_handler |
| 79 |
self.on_unknown_message = self.obj.on_unknown_message = Mock( |
| 80 |
name='on_unknown_message', |
| 81 |
) |
| 82 |
_consumer.on_unknown_message = self.on_unknown_message |
| 83 |
self.on_unknown_task = self.obj.on_unknown_task = Mock( |
| 84 |
name='on_unknown_task', |
| 85 |
) |
| 86 |
_consumer.on_unknown_task = self.on_unknown_task |
| 87 |
self.on_invalid_task = self.obj.on_invalid_task = Mock( |
| 88 |
name='on_invalid_task', |
| 89 |
) |
| 90 |
_consumer.on_invalid_task = self.on_invalid_task |
| 91 |
self.on_decode_error = self.obj.on_decode_error = Mock( |
| 92 |
name='on_decode_error', |
| 93 |
) |
| 94 |
_consumer.on_decode_error = self.on_decode_error |
| 95 |
_consumer.strategies = self.obj.strategies |
| 96 |
|
| 97 |
def timeout_then_error(self, mock): |
| 98 |
|
| 99 |
def first(*args, **kwargs): |
| 100 |
mock.side_effect = socket.error() |
| 101 |
raise socket.timeout() |
| 102 |
mock.side_effect = first |
| 103 |
|
| 104 |
def close_then_error(self, mock=None, mod=0, exc=None): |
| 105 |
mock = Mock() if mock is None else mock |
| 106 |
|
| 107 |
def first(*args, **kwargs): |
| 108 |
if not mod or mock.call_count > mod: |
| 109 |
self.close() |
| 110 |
raise (socket.error() if exc is None else exc) |
| 111 |
mock.side_effect = first |
| 112 |
return mock |
| 113 |
|
| 114 |
def close(self, *args, **kwargs): |
| 115 |
self.blueprint.state = CLOSE |
| 116 |
|
| 117 |
def closer(self, mock=None, mod=0): |
| 118 |
mock = Mock() if mock is None else mock |
| 119 |
|
| 120 |
def closing(*args, **kwargs): |
| 121 |
if not mod or mock.call_count >= mod: |
| 122 |
self.close() |
| 123 |
mock.side_effect = closing |
| 124 |
return mock |
| 125 |
|
| 126 |
|
| 127 |
def get_task_callback(*args, **kwargs): |
| 128 |
x = X(*args, **kwargs) |
| 129 |
x.blueprint.state = CLOSE |
| 130 |
asynloop(*x.args) |
| 131 |
return x, x.consumer.on_message |
| 132 |
|
| 133 |
|
| 134 |
class test_asynloop: |
| 135 |
|
| 136 |
def setup_method(self): |
| 137 |
@self.app.task(shared=False) |
| 138 |
def add(x, y): |
| 139 |
return x + y |
| 140 |
self.add = add |
| 141 |
|
| 142 |
def test_drain_after_consume(self): |
| 143 |
x, _ = get_task_callback(self.app, transport_driver_type='amqp') |
| 144 |
assert _quick_drain in [p.fun for p in x.hub._ready] |
| 145 |
|
| 146 |
def test_pool_did_not_start_at_startup(self): |
| 147 |
x = X(self.app) |
| 148 |
x.obj.restart_count = 0 |
| 149 |
x.obj.pool.did_start_ok.return_value = False |
| 150 |
with pytest.raises(WorkerLostError): |
| 151 |
asynloop(*x.args) |
| 152 |
|
| 153 |
def test_setup_heartbeat(self): |
| 154 |
x = X(self.app, heartbeat=10) |
| 155 |
x.hub.timer.call_repeatedly = Mock(name='x.hub.call_repeatedly()') |
| 156 |
x.blueprint.state = CLOSE |
| 157 |
asynloop(*x.args) |
| 158 |
x.consumer.consume.assert_called_with() |
| 159 |
x.obj.on_ready.assert_called_with() |
| 160 |
last_call_args, _ = x.hub.timer.call_repeatedly.call_args |
| 161 |
|
| 162 |
assert last_call_args[0] == 10 / 2.0 |
| 163 |
assert last_call_args[2] == (2.0,) |
| 164 |
|
| 165 |
def task_context(self, sig, **kwargs): |
| 166 |
x, on_task = get_task_callback(self.app, **kwargs) |
| 167 |
message = self.task_message_from_sig(self.app, sig) |
| 168 |
strategy = x.obj.strategies[sig.task] = Mock(name='strategy') |
| 169 |
return x, on_task, message, strategy |
| 170 |
|
| 171 |
def test_on_task_received(self): |
| 172 |
x, on_task, msg, strategy = self.task_context(self.add.s(2, 2)) |
| 173 |
on_task(msg) |
| 174 |
strategy.assert_called_with( |
| 175 |
msg, None, |
| 176 |
PromiseEqual(x._consumer.call_soon, msg.ack_log_error), |
| 177 |
PromiseEqual(x._consumer.call_soon, msg.reject_log_error), [], |
| 178 |
) |
| 179 |
|
| 180 |
def test_on_task_received_executes_on_task_message(self): |
| 181 |
cbs = [Mock(), Mock(), Mock()] |
| 182 |
x, on_task, msg, strategy = self.task_context( |
| 183 |
self.add.s(2, 2), on_task_message=cbs, |
| 184 |
) |
| 185 |
on_task(msg) |
| 186 |
strategy.assert_called_with( |
| 187 |
msg, None, |
| 188 |
PromiseEqual(x._consumer.call_soon, msg.ack_log_error), |
| 189 |
PromiseEqual(x._consumer.call_soon, msg.reject_log_error), |
| 190 |
cbs, |
| 191 |
) |
| 192 |
|
| 193 |
def test_on_task_message_missing_name(self): |
| 194 |
x, on_task, msg, strategy = self.task_context(self.add.s(2, 2)) |
| 195 |
msg.headers.pop('task') |
| 196 |
on_task(msg) |
| 197 |
x.on_unknown_message.assert_called_with(msg.decode(), msg) |
| 198 |
|
| 199 |
def test_on_task_pool_raises(self): |
| 200 |
x, on_task, msg, strategy = self.task_context(self.add.s(2, 2)) |
| 201 |
strategy.side_effect = ValueError() |
| 202 |
with pytest.raises(ValueError): |
| 203 |
on_task(msg) |
| 204 |
|
| 205 |
def test_on_task_InvalidTaskError(self): |
| 206 |
x, on_task, msg, strategy = self.task_context(self.add.s(2, 2)) |
| 207 |
exc = strategy.side_effect = InvalidTaskError() |
| 208 |
on_task(msg) |
| 209 |
x.on_invalid_task.assert_called_with(None, msg, exc) |
| 210 |
|
| 211 |
def test_on_task_DecodeError(self): |
| 212 |
x, on_task, msg, strategy = self.task_context(self.add.s(2, 2)) |
| 213 |
exc = strategy.side_effect = DecodeError() |
| 214 |
on_task(msg) |
| 215 |
x.on_decode_error.assert_called_with(msg, exc) |
| 216 |
|
| 217 |
@pytest.mark.parametrize('should_stop', (None, False, True, EX_OK)) |
| 218 |
def test_should_terminate(self, should_stop): |
| 219 |
x = X(self.app) |
| 220 |
state.should_stop = should_stop |
| 221 |
state.should_terminate = True |
| 222 |
try: |
| 223 |
with pytest.raises(WorkerTerminate): |
| 224 |
asynloop(*x.args) |
| 225 |
finally: |
| 226 |
state.should_stop = None |
| 227 |
state.should_terminate = None |
| 228 |
|
| 229 |
def test_should_terminate_hub_close_raises(self): |
| 230 |
x = X(self.app) |
| 231 |
# XXX why aren't the errors propagated?!? |
| 232 |
state.should_terminate = EX_FAILURE |
| 233 |
x.hub.close.side_effect = MemoryError() |
| 234 |
try: |
| 235 |
with pytest.raises(WorkerTerminate): |
| 236 |
asynloop(*x.args) |
| 237 |
finally: |
| 238 |
state.should_terminate = None |
| 239 |
|
| 240 |
def test_should_stop(self): |
| 241 |
x = X(self.app) |
| 242 |
state.should_stop = 303 |
| 243 |
try: |
| 244 |
with pytest.raises(WorkerShutdown): |
| 245 |
asynloop(*x.args) |
| 246 |
finally: |
| 247 |
state.should_stop = None |
| 248 |
|
| 249 |
def test_updates_qos(self): |
| 250 |
x = X(self.app) |
| 251 |
x.qos.prev = 3 |
| 252 |
x.qos.value = 3 |
| 253 |
x.hub.on_tick.add(x.closer(mod=2)) |
| 254 |
x.hub.timer._queue = [1] |
| 255 |
asynloop(*x.args) |
| 256 |
x.qos.update.assert_not_called() |
| 257 |
|
| 258 |
x = X(self.app) |
| 259 |
x.qos.prev = 1 |
| 260 |
x.qos.value = 6 |
| 261 |
x.hub.on_tick.add(x.closer(mod=2)) |
| 262 |
asynloop(*x.args) |
| 263 |
x.qos.update.assert_called_with() |
| 264 |
x.hub.fire_timers.assert_called_with(propagate=(socket.error,)) |
| 265 |
|
| 266 |
def test_poll_empty(self): |
| 267 |
x = X(self.app) |
| 268 |
x.hub.readers = {6: Mock()} |
| 269 |
x.hub.timer._queue = [1] |
| 270 |
x.close_then_error(x.hub.poller.poll) |
| 271 |
x.hub.fire_timers.return_value = 33.37 |
| 272 |
poller = x.hub.poller |
| 273 |
poller.poll.return_value = [] |
| 274 |
with pytest.raises(socket.error): |
| 275 |
asynloop(*x.args) |
| 276 |
poller.poll.assert_called_with(33.37) |
| 277 |
|
| 278 |
def test_poll_readable(self): |
| 279 |
x = X(self.app) |
| 280 |
reader = Mock(name='reader') |
| 281 |
x.hub.add_reader(6, reader, 6) |
| 282 |
x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), mod=4)) |
| 283 |
poller = x.hub.poller |
| 284 |
poller.poll.return_value = [(6, READ)] |
| 285 |
with pytest.raises(socket.error): |
| 286 |
asynloop(*x.args) |
| 287 |
reader.assert_called_with(6) |
| 288 |
poller.poll.assert_called() |
| 289 |
|
| 290 |
def test_poll_readable_raises_Empty(self): |
| 291 |
x = X(self.app) |
| 292 |
reader = Mock(name='reader') |
| 293 |
x.hub.add_reader(6, reader, 6) |
| 294 |
x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), 2)) |
| 295 |
poller = x.hub.poller |
| 296 |
poller.poll.return_value = [(6, READ)] |
| 297 |
reader.side_effect = Empty() |
| 298 |
with pytest.raises(socket.error): |
| 299 |
asynloop(*x.args) |
| 300 |
reader.assert_called_with(6) |
| 301 |
poller.poll.assert_called() |
| 302 |
|
| 303 |
def test_poll_writable(self): |
| 304 |
x = X(self.app) |
| 305 |
writer = Mock(name='writer') |
| 306 |
x.hub.add_writer(6, writer, 6) |
| 307 |
x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), 2)) |
| 308 |
poller = x.hub.poller |
| 309 |
poller.poll.return_value = [(6, WRITE)] |
| 310 |
with pytest.raises(socket.error): |
| 311 |
asynloop(*x.args) |
| 312 |
writer.assert_called_with(6) |
| 313 |
poller.poll.assert_called() |
| 314 |
|
| 315 |
def test_poll_writable_none_registered(self): |
| 316 |
x = X(self.app) |
| 317 |
writer = Mock(name='writer') |
| 318 |
x.hub.add_writer(6, writer, 6) |
| 319 |
x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), 2)) |
| 320 |
poller = x.hub.poller |
| 321 |
poller.poll.return_value = [(7, WRITE)] |
| 322 |
with pytest.raises(socket.error): |
| 323 |
asynloop(*x.args) |
| 324 |
poller.poll.assert_called() |
| 325 |
|
| 326 |
def test_poll_unknown_event(self): |
| 327 |
x = X(self.app) |
| 328 |
writer = Mock(name='reader') |
| 329 |
x.hub.add_writer(6, writer, 6) |
| 330 |
x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), 2)) |
| 331 |
poller = x.hub.poller |
| 332 |
poller.poll.return_value = [(6, 0)] |
| 333 |
with pytest.raises(socket.error): |
| 334 |
asynloop(*x.args) |
| 335 |
poller.poll.assert_called() |
| 336 |
|
| 337 |
def test_poll_keep_draining_disabled(self): |
| 338 |
x = X(self.app) |
| 339 |
x.hub.writers = {6: Mock()} |
| 340 |
poll = x.hub.poller.poll |
| 341 |
|
| 342 |
def se(*args, **kwargs): |
| 343 |
poll.side_effect = socket.error() |
| 344 |
poll.side_effect = se |
| 345 |
|
| 346 |
poller = x.hub.poller |
| 347 |
poll.return_value = [(6, 0)] |
| 348 |
with pytest.raises(socket.error): |
| 349 |
asynloop(*x.args) |
| 350 |
poller.poll.assert_called() |
| 351 |
|
| 352 |
def test_poll_err_writable(self): |
| 353 |
x = X(self.app) |
| 354 |
writer = Mock(name='writer') |
| 355 |
x.hub.add_writer(6, writer, 6, 48) |
| 356 |
x.hub.on_tick.add(x.close_then_error(Mock(), 2)) |
| 357 |
poller = x.hub.poller |
| 358 |
poller.poll.return_value = [(6, ERR)] |
| 359 |
with pytest.raises(socket.error): |
| 360 |
asynloop(*x.args) |
| 361 |
writer.assert_called_with(6, 48) |
| 362 |
poller.poll.assert_called() |
| 363 |
|
| 364 |
def test_poll_write_generator(self): |
| 365 |
x = X(self.app) |
| 366 |
x.hub.remove_writer = Mock(name='hub.remove_writer()') |
| 367 |
|
| 368 |
def Gen(): |
| 369 |
yield 1 |
| 370 |
yield 2 |
| 371 |
gen = Gen() |
| 372 |
|
| 373 |
x.hub.add_writer(6, gen) |
| 374 |
x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), 2)) |
| 375 |
x.hub.poller.poll.return_value = [(6, WRITE)] |
| 376 |
with pytest.raises(socket.error): |
| 377 |
asynloop(*x.args) |
| 378 |
assert gen.gi_frame.f_lasti != -1 |
| 379 |
x.hub.remove_writer.assert_not_called() |
| 380 |
|
| 381 |
def test_poll_write_generator_stopped(self): |
| 382 |
x = X(self.app) |
| 383 |
|
| 384 |
def Gen(): |
| 385 |
if 0: |
| 386 |
yield |
| 387 |
gen = Gen() |
| 388 |
x.hub.add_writer(6, gen) |
| 389 |
x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), 2)) |
| 390 |
x.hub.poller.poll.return_value = [(6, WRITE)] |
| 391 |
x.hub.remove_writer = Mock(name='hub.remove_writer()') |
| 392 |
with pytest.raises(socket.error): |
| 393 |
asynloop(*x.args) |
| 394 |
assert gen.gi_frame is None |
| 395 |
|
| 396 |
def test_poll_write_generator_raises(self): |
| 397 |
x = X(self.app) |
| 398 |
|
| 399 |
def Gen(): |
| 400 |
raise ValueError('foo') |
| 401 |
yield |
| 402 |
gen = Gen() |
| 403 |
x.hub.add_writer(6, gen) |
| 404 |
x.hub.remove = Mock(name='hub.remove()') |
| 405 |
x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), 2)) |
| 406 |
x.hub.poller.poll.return_value = [(6, WRITE)] |
| 407 |
with pytest.raises(ValueError): |
| 408 |
asynloop(*x.args) |
| 409 |
assert gen.gi_frame is None |
| 410 |
x.hub.remove.assert_called_with(6) |
| 411 |
|
| 412 |
def test_poll_err_readable(self): |
| 413 |
x = X(self.app) |
| 414 |
reader = Mock(name='reader') |
| 415 |
x.hub.add_reader(6, reader, 6, 24) |
| 416 |
x.hub.on_tick.add(x.close_then_error(Mock(), 2)) |
| 417 |
poller = x.hub.poller |
| 418 |
poller.poll.return_value = [(6, ERR)] |
| 419 |
with pytest.raises(socket.error): |
| 420 |
asynloop(*x.args) |
| 421 |
reader.assert_called_with(6, 24) |
| 422 |
poller.poll.assert_called() |
| 423 |
|
| 424 |
def test_poll_raises_ValueError(self): |
| 425 |
x = X(self.app) |
| 426 |
x.hub.readers = {6: Mock()} |
| 427 |
poller = x.hub.poller |
| 428 |
x.close_then_error(poller.poll, exc=ValueError) |
| 429 |
asynloop(*x.args) |
| 430 |
poller.poll.assert_called() |
| 431 |
|
| 432 |
def test_heartbeat_error(self): |
| 433 |
x = X(self.app, heartbeat=10) |
| 434 |
x.connection.heartbeat_check = Mock( |
| 435 |
side_effect=RuntimeError("Heartbeat error") |
| 436 |
) |
| 437 |
|
| 438 |
def call_repeatedly(rate, fn, args): |
| 439 |
fn(*args) |
| 440 |
|
| 441 |
x.hub.timer.call_repeatedly = call_repeatedly |
| 442 |
with pytest.raises(RuntimeError): |
| 443 |
asynloop(*x.args) |
| 444 |
|
| 445 |
def test_no_heartbeat_support(self): |
| 446 |
x = X(self.app) |
| 447 |
x.connection.supports_heartbeats = False |
| 448 |
x.hub.timer.call_repeatedly = Mock( |
| 449 |
name='x.hub.timer.call_repeatedly()' |
| 450 |
) |
| 451 |
x.hub.on_tick.add(x.closer(mod=2)) |
| 452 |
asynloop(*x.args) |
| 453 |
|
| 454 |
x.hub.timer.call_repeatedly.assert_not_called() |
| 455 |
|
| 456 |
def test_hub_reset_on_connection_error(self): |
| 457 |
x = X(self.app) |
| 458 |
x.hub.readers = {6: Mock()} |
| 459 |
x.hub.timer._queue = [1] |
| 460 |
x.hub.reset = Mock(name='hub.reset()') |
| 461 |
x.close_then_error(x.hub.poller.poll) |
| 462 |
x.hub.fire_timers.return_value = 33.37 |
| 463 |
poller = x.hub.poller |
| 464 |
poller.poll.return_value = [] |
| 465 |
with pytest.raises(socket.error): |
| 466 |
asynloop(*x.args) |
| 467 |
x.hub.reset.assert_called_once() |
| 468 |
|
| 469 |
def test_hub_timer_cleared_on_connection_error(self): |
| 470 |
# Stale timer entries (e.g. maybe_restore_messages) must be cleared |
| 471 |
# when the event loop exits due to a connection error. Without this, |
| 472 |
# entries accumulated across reconnects can fire against the broken |
| 473 |
# connection and crash the loop again before the new connection is |
| 474 |
# fully established, causing a rapid restart loop. |
| 475 |
x = X(self.app) |
| 476 |
x.hub.readers = {6: Mock()} |
| 477 |
x.hub.timer._queue = [1] |
| 478 |
x.hub.reset = Mock(name='hub.reset()') |
| 479 |
x.close_then_error(x.hub.poller.poll) |
| 480 |
x.hub.fire_timers.return_value = 33.37 |
| 481 |
x.hub.poller.poll.return_value = [] |
| 482 |
with pytest.raises(socket.error): |
| 483 |
asynloop(*x.args) |
| 484 |
x.hub.timer.clear.assert_called_once() |
| 485 |
|
| 486 |
def test_hub_timer_not_cleared_on_graceful_shutdown(self): |
| 487 |
# On graceful shutdown the timer queue must be left intact so that |
| 488 |
# periodic timers (e.g. heartbeat) keep firing while the pool drains. |
| 489 |
x = X(self.app) |
| 490 |
x.hub.reset = Mock(name='hub.reset()') |
| 491 |
x.hub.on_tick.add(x.closer(mod=2)) |
| 492 |
asynloop(*x.args) |
| 493 |
x.hub.timer.clear.assert_not_called() |
| 494 |
|
| 495 |
def test_hub_timer_not_cleared_on_worker_shutdown(self): |
| 496 |
x = X(self.app) |
| 497 |
x.hub.reset = Mock(name='hub.reset()') |
| 498 |
state.should_stop = 303 |
| 499 |
try: |
| 500 |
with pytest.raises(WorkerShutdown): |
| 501 |
asynloop(*x.args) |
| 502 |
finally: |
| 503 |
state.should_stop = None |
| 504 |
x.hub.timer.clear.assert_not_called() |
| 505 |
|
| 506 |
def test_hub_timer_not_cleared_on_worker_terminate(self): |
| 507 |
x = X(self.app) |
| 508 |
x.hub.reset = Mock(name='hub.reset()') |
| 509 |
state.should_terminate = True |
| 510 |
try: |
| 511 |
with pytest.raises(WorkerTerminate): |
| 512 |
asynloop(*x.args) |
| 513 |
finally: |
| 514 |
state.should_terminate = None |
| 515 |
x.hub.timer.clear.assert_not_called() |
| 516 |
|
| 517 |
def test_hub_timer_clear_error_still_reraises_original(self): |
| 518 |
# If hub.timer.clear() itself raises, the original connection error |
| 519 |
# must still be propagated, not the cleanup error. |
| 520 |
x = X(self.app) |
| 521 |
x.hub.readers = {6: Mock()} |
| 522 |
x.hub.timer._queue = [1] |
| 523 |
x.hub.reset = Mock(name='hub.reset()') |
| 524 |
x.hub.timer.clear = Mock( |
| 525 |
name='hub.timer.clear()', side_effect=RuntimeError('clear failed') |
| 526 |
) |
| 527 |
x.close_then_error(x.hub.poller.poll) |
| 528 |
x.hub.fire_timers.return_value = 33.37 |
| 529 |
x.hub.poller.poll.return_value = [] |
| 530 |
with pytest.raises(socket.error): |
| 531 |
asynloop(*x.args) |
| 532 |
x.hub.timer.clear.assert_called_once() |
| 533 |
|
| 534 |
def test_hub_timer_cleared_even_when_reset_raises(self): |
| 535 |
# hub.timer.clear() must still be called even if hub.reset() raises. |
| 536 |
# The two cleanup calls are in separate try/except blocks so that a |
| 537 |
# failure in hub.reset() does not prevent stale timer entries from |
| 538 |
# being discarded, avoiding stale timers persisting after a reset error. |
| 539 |
x = X(self.app) |
| 540 |
x.hub.readers = {6: Mock()} |
| 541 |
x.hub.timer._queue = [1] |
| 542 |
x.hub.reset = Mock(name='hub.reset()', side_effect=RuntimeError('reset failed')) |
| 543 |
x.close_then_error(x.hub.poller.poll) |
| 544 |
x.hub.fire_timers.return_value = 33.37 |
| 545 |
x.hub.poller.poll.return_value = [] |
| 546 |
with pytest.raises(socket.error): |
| 547 |
asynloop(*x.args) |
| 548 |
x.hub.timer.clear.assert_called_once() |
| 549 |
|
| 550 |
def test_hub_not_reset_on_graceful_shutdown(self): |
| 551 |
x = X(self.app) |
| 552 |
x.hub.reset = Mock(name='hub.reset()') |
| 553 |
x.hub.on_tick.add(x.closer(mod=2)) |
| 554 |
asynloop(*x.args) |
| 555 |
x.hub.reset.assert_not_called() |
| 556 |
|
| 557 |
def test_hub_not_reset_on_worker_shutdown(self): |
| 558 |
x = X(self.app) |
| 559 |
x.hub.reset = Mock(name='hub.reset()') |
| 560 |
state.should_stop = 303 |
| 561 |
try: |
| 562 |
with pytest.raises(WorkerShutdown): |
| 563 |
asynloop(*x.args) |
| 564 |
finally: |
| 565 |
state.should_stop = None |
| 566 |
x.hub.reset.assert_not_called() |
| 567 |
|
| 568 |
def test_hub_not_reset_on_worker_terminate(self): |
| 569 |
x = X(self.app) |
| 570 |
x.hub.reset = Mock(name='hub.reset()') |
| 571 |
state.should_terminate = True |
| 572 |
try: |
| 573 |
with pytest.raises(WorkerTerminate): |
| 574 |
asynloop(*x.args) |
| 575 |
finally: |
| 576 |
state.should_terminate = None |
| 577 |
x.hub.reset.assert_not_called() |
| 578 |
|
| 579 |
def test_hub_reset_error_still_reraises_original(self): |
| 580 |
x = X(self.app) |
| 581 |
x.hub.readers = {6: Mock()} |
| 582 |
x.hub.timer._queue = [1] |
| 583 |
x.hub.reset = Mock(name='hub.reset()', side_effect=RuntimeError('reset failed')) |
| 584 |
x.close_then_error(x.hub.poller.poll) |
| 585 |
x.hub.fire_timers.return_value = 33.37 |
| 586 |
poller = x.hub.poller |
| 587 |
poller.poll.return_value = [] |
| 588 |
# The original socket.error should still be raised, not the RuntimeError from reset() |
| 589 |
with pytest.raises(socket.error): |
| 590 |
asynloop(*x.args) |
| 591 |
x.hub.reset.assert_called_once() |
| 592 |
|
| 593 |
|
| 594 |
class test_synloop: |
| 595 |
|
| 596 |
def test_timeout_ignored(self): |
| 597 |
x = X(self.app) |
| 598 |
x.timeout_then_error(x.connection.drain_events) |
| 599 |
with pytest.raises(socket.error): |
| 600 |
synloop(*x.args) |
| 601 |
assert x.connection.drain_events.call_count == 2 |
| 602 |
|
| 603 |
def test_updates_qos_when_changed(self): |
| 604 |
x = X(self.app) |
| 605 |
x.qos.prev = 2 |
| 606 |
x.qos.value = 2 |
| 607 |
x.timeout_then_error(x.connection.drain_events) |
| 608 |
with pytest.raises(socket.error): |
| 609 |
synloop(*x.args) |
| 610 |
x.qos.update.assert_not_called() |
| 611 |
|
| 612 |
x.qos.value = 4 |
| 613 |
x.timeout_then_error(x.connection.drain_events) |
| 614 |
with pytest.raises(socket.error): |
| 615 |
synloop(*x.args) |
| 616 |
x.qos.update.assert_called_with() |
| 617 |
|
| 618 |
def test_ignores_socket_errors_when_closed(self): |
| 619 |
x = X(self.app) |
| 620 |
x.close_then_error(x.connection.drain_events) |
| 621 |
assert synloop(*x.args) is None |
| 622 |
|
| 623 |
def test_no_connection(self): |
| 624 |
x = X(self.app) |
| 625 |
x.connection = None |
| 626 |
x.hub.timer.call_repeatedly = Mock( |
| 627 |
name='x.hub.timer.call_repeatedly()' |
| 628 |
) |
| 629 |
x.blueprint.state = CLOSE |
| 630 |
synloop(*x.args) |
| 631 |
|
| 632 |
x.hub.timer.call_repeatedly.assert_not_called() |
| 633 |
|
| 634 |
def test_heartbeat_error(self): |
| 635 |
x = X(self.app, heartbeat=10) |
| 636 |
x.obj.pool.is_green = True |
| 637 |
|
| 638 |
def heartbeat_check(rate): |
| 639 |
raise RuntimeError('Heartbeat error') |
| 640 |
|
| 641 |
def call_repeatedly(rate, fn, args): |
| 642 |
fn(*args) |
| 643 |
|
| 644 |
x.connection.heartbeat_check = Mock( |
| 645 |
name='heartbeat_check', side_effect=heartbeat_check |
| 646 |
) |
| 647 |
x.obj.timer.call_repeatedly = call_repeatedly |
| 648 |
with pytest.raises(RuntimeError): |
| 649 |
synloop(*x.args) |
| 650 |
|
| 651 |
def test_no_heartbeat_support(self): |
| 652 |
x = X(self.app) |
| 653 |
x.connection.supports_heartbeats = False |
| 654 |
x.obj.pool.is_green = True |
| 655 |
x.obj.timer.call_repeatedly = Mock( |
| 656 |
name='x.obj.timer.call_repeatedly()' |
| 657 |
) |
| 658 |
|
| 659 |
def drain_events(timeout): |
| 660 |
x.blueprint.state = CLOSE |
| 661 |
x.connection.drain_events.side_effect = drain_events |
| 662 |
synloop(*x.args) |
| 663 |
|
| 664 |
x.obj.timer.call_repeatedly.assert_not_called() |
| 665 |
|
| 666 |
|
| 667 |
class test_quick_drain: |
| 668 |
|
| 669 |
def setup_method(self): |
| 670 |
self.connection = Mock(name='connection') |
| 671 |
|
| 672 |
def test_drain(self): |
| 673 |
_quick_drain(self.connection, timeout=33.3) |
| 674 |
self.connection.drain_events.assert_called_with(timeout=33.3) |
| 675 |
|
| 676 |
def test_drain_error(self): |
| 677 |
exc = KeyError() |
| 678 |
exc.errno = 313 |
| 679 |
self.connection.drain_events.side_effect = exc |
| 680 |
with pytest.raises(KeyError): |
| 681 |
_quick_drain(self.connection, timeout=33.3) |
| 682 |
|
| 683 |
def test_drain_error_EAGAIN(self): |
| 684 |
exc = KeyError() |
| 685 |
exc.errno = errno.EAGAIN |
| 686 |
self.connection.drain_events.side_effect = exc |
| 687 |
_quick_drain(self.connection, timeout=33.3) |