Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Fix for celery#2743
Browse files Browse the repository at this point in the history
Fixes celery issue for pymongo 3+ with gevent
  • Loading branch information
sukrit007 authored and Tristan Carel committed Dec 18, 2015
1 parent 02601d8 commit af677e3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
11 changes: 7 additions & 4 deletions celery/backends/mongodb.py
Expand Up @@ -104,8 +104,13 @@ def _prepare_client_options(self):
if pymongo.version_tuple >= (3, ):
return {'maxPoolSize': self.max_pool_size}
else: # pragma: no cover
return {'max_pool_size': max_pool_size,
'auto_start_request': False}
options = {
'max_pool_size': self.max_pool_size,
'auto_start_request': False
}
if detect_environment() != 'default':
options['use_greenlets'] = True
return options

def _get_connection(self):
"""Connect to the MongoDB server."""
Expand All @@ -124,8 +129,6 @@ def _get_connection(self):
url = 'mongodb://{0}:{1}'.format(url, self.port)
if url == 'mongodb://':
url = url + 'localhost'
if detect_environment() != 'default':
self.options['use_greenlets'] = True
self._connection = MongoClient(host=url, **self.options)

return self._connection
Expand Down
41 changes: 40 additions & 1 deletion celery/tests/backends/test_mongodb.py
Expand Up @@ -254,7 +254,7 @@ def test_restore_group(self, mock_get_database):
mock_database.__getitem__.assert_called_once_with(MONGODB_COLLECTION)
mock_collection.find_one.assert_called_once_with(
{'_id': sentinel.taskset_id})
self.assertEqual(
self.assertItemsEqual(
['date_done', 'result', 'task_id'],
list(ret_val.keys()),
)
Expand Down Expand Up @@ -324,3 +324,42 @@ def test_get_database_authfailure(self):
with self.assertRaises(ImproperlyConfigured):
x._get_database()
db.authenticate.assert_called_with('jerry', 'cere4l')

@patch('celery.backends.mongodb.detect_environment')
def test_prepare_client_options_for_ver_2(self, m_detect_env):
m_detect_env.return_value = 'default'
with patch('pymongo.version_tuple', new=(2, 6, 3)):
options = self.backend._prepare_client_options()
self.assertDictEqual(options, {
'max_pool_size': self.backend.max_pool_size,
'auto_start_request': False
})

@patch('celery.backends.mongodb.detect_environment')
def test_prepare_client_options_for_ver_2_with_gevent(self, m_detect_env):
m_detect_env.return_value = 'gevent'
with patch('pymongo.version_tuple', new=(2, 6, 3)):
options = self.backend._prepare_client_options()
self.assertDictEqual(options, {
'max_pool_size': self.backend.max_pool_size,
'auto_start_request': False,
'use_greenlets': True
})

@patch('celery.backends.mongodb.detect_environment')
def test_prepare_client_options_for_ver_3(self, m_detect_env):
m_detect_env.return_value = 'default'
with patch('pymongo.version_tuple', new=(3, 0, 3)):
options = self.backend._prepare_client_options()
self.assertDictEqual(options, {
'maxPoolSize': self.backend.max_pool_size
})

@patch('celery.backends.mongodb.detect_environment')
def test_prepare_client_options_for_ver_3_with_gevent(self, m_detect_env):
m_detect_env.return_value = 'gevent'
with patch('pymongo.version_tuple', new=(3, 0, 3)):
options = self.backend._prepare_client_options()
self.assertDictEqual(options, {
'maxPoolSize': self.backend.max_pool_size
})

0 comments on commit af677e3

Please sign in to comment.