'Signals should be fired right before bulk_operations() exits.'

@ddt.data(ModuleStoreEnum.Type.mongo, ModuleStoreEnum.Type.split)
def test_bulk_operations_signal_firing(self, default):

    with MongoContentstoreBuilder().build() as contentstore:
        signal_handler = Mock(name='signal_handler')
        self.store = MixedModuleStore(contentstore=contentstore, create_modulestore_instance=create_modulestore_instance, mappings={}, signal_handler=signal_handler, **self.OPTIONS)
        self.addCleanup(self.store.close_all_connections)
        with self.store.default_store(default):
            signal_handler.send.assert_not_called()
            course = self.store.create_course('org_x', 'course_y', 'run_z', self.user_id)
            signal_handler.send.assert_called_with('course_published', course_key=course.id)
            signal_handler.reset_mock()
            course_key = course.id
            def _clear_bulk_ops_record(course_key):
                '\n                                                                                Check    if    the    signal    has    been    fired.\n                                                                                The    course_published    signal    fires    before    the    _clear_bulk_ops_record.\n                                                                                '
                signal_handler.send.assert_called_with('course_published', course_key=course.id)
            with patch.object(self.store.thread_cache.default_store, '_clear_bulk_ops_record', wraps=_clear_bulk_ops_record) as mock_clear_bulk_ops_record:
                with self.store.bulk_operations(course_key):
                    categories = DIRECT_ONLY_CATEGORIES
                    for block_type in categories:
                        self.store.create_item(self.user_id, course_key, block_type)
                        signal_handler.send.assert_not_called()
                self.assertEqual(mock_clear_bulk_ops_record.call_count, 1)
            signal_handler.send.assert_called_with('course_published', course_key=course.id)
