Skip to content
This repository has been archived by the owner on Jun 14, 2019. It is now read-only.

Commit

Permalink
Fix mute_signals behavior for signals with caching
Browse files Browse the repository at this point in the history
Connecting signals (with use_caching=True) inside mute_signals
was breaking unmute on exit. Paused receivers were not running.
This was caused by signal cache not being restored after unpatching.
Workaround is to clear signal cache on exit.

Fixes FactoryBoy#212
  • Loading branch information
coagulant committed Jul 2, 2015
1 parent 9246fa6 commit d176eb1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions factory/django.py
Expand Up @@ -277,6 +277,8 @@ def __exit__(self, exc_type, exc_value, traceback):
receivers)

signal.receivers = receivers
with signal.lock:
signal.sender_receivers_cache.clear()
self.paused = {}

def copy(self):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_django.py
Expand Up @@ -678,6 +678,19 @@ def test_context_manager(self):

self.assertSignalsReactivated()

def test_signal_cache(self):
with factory.django.mute_signals(signals.pre_save, signals.post_save):
signals.post_save.connect(self.handlers.mute_block_receiver)
WithSignalsFactory()

self.assertTrue(self.handlers.mute_block_receiver.call_count, 1)
self.assertEqual(self.handlers.pre_init.call_count, 1)
self.assertFalse(self.handlers.pre_save.called)
self.assertFalse(self.handlers.post_save.called)

self.assertSignalsReactivated()
self.assertTrue(self.handlers.mute_block_receiver.call_count, 1)

def test_class_decorator(self):
@factory.django.mute_signals(signals.pre_save, signals.post_save)
class WithSignalsDecoratedFactory(factory.django.DjangoModelFactory):
Expand Down

0 comments on commit d176eb1

Please sign in to comment.