Skip to content

Commit

Permalink
swallowing RuntimeError on calls to closed loops (#102)
Browse files Browse the repository at this point in the history
* swallowing RuntimeError on call_soon_threadsafe of closed loops

* test to cover new code
  • Loading branch information
apatrushev authored and asvetlov committed Jul 27, 2018
1 parent 03d8bec commit 05f9b9d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion janus/__init__.py
Expand Up @@ -46,8 +46,11 @@ def __init__(self, maxsize=0, *, loop=None):
self._pending = set()

def checked_call_soon_threadsafe(callback, *args):
if not loop.is_closed():
try:
loop.call_soon_threadsafe(callback, *args)
except RuntimeError:
# swallowing agreed in #2
pass
self._call_soon_threadsafe = checked_call_soon_threadsafe

def checked_call_soon(callback, *args):
Expand Down
9 changes: 9 additions & 0 deletions tests/test_sync.py
Expand Up @@ -5,6 +5,7 @@
import queue
import time
import unittest
from unittest.mock import patch

import threading

Expand Down Expand Up @@ -380,6 +381,14 @@ def test_failing_queue(self):
self.failing_queue_test(q)
self.failing_queue_test(q)

def test_closed_loop_non_failing(self):
q = janus.Queue(QUEUE_SIZE, loop=self.loop).sync_q
# we are pacthing loop to follow setUp/tearDown agreement
with patch.object(self.loop, 'call_soon_threadsafe') as func:
func.side_effect = RuntimeError()
q.put_nowait(1)
self.assertEqual(func.call_count, 1)


if __name__ == "__main__":
unittest.main()

0 comments on commit 05f9b9d

Please sign in to comment.