Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for recent versions of PyMongo & add travis CI tests #10

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,6 @@ nosetests.xml
.mr.developer.cfg
.project
.pydevproject

# PyCharm
.idea/
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: python
python:
- "2.7"
- "3.4"
- "3.5"
- "3.6"
- "3.7-dev"
env:
- PYMONGO_VERSION=3.7
- PYMONGO_VERSION=3.0
- PYMONGO_VERSION=2.4
services:
- mongodb
# command to install dependencies
install:
- pip install -q pymongo==$PYMONGO_VERSION
# command to run tests
script:
- python -m unittest discover
7 changes: 5 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mongoqueue
----------

.. image:: https://travis-ci.com/rupello/mongoqueue.svg?branch=master
:target: https://travis-ci.com/rupello/mongoqueue

Properties
==========

Expand All @@ -23,10 +26,10 @@ A queue can be instantiated with a mongo collection and a consumer
identifier. The consumer identifier helps distinguish multiple queue
consumers that are taking jobs from the queue::

>> from pymongo import Connection
>> from pymongo import MongoClient
>> from mongoqueue import MongoQueue
>> queue = MongoQueue(
... Connection(TEST_DB).doctest_queue,
... MongoClient(TEST_DB).doctest_queue,
... consumer_id="consumer-1",
... timeout=300,
... max_attempts=3)
Expand Down
6 changes: 3 additions & 3 deletions mongoqueue/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class MongoLock(object):
def __init__(self, collection, lock_name, lease=120):
self.collection = collection
self.lock_name = lock_name
self._client_id = uuid.uuid4().get_hex()
self._client_id = uuid.uuid4().hex
self._locked = False
self._lease_time = lease
self._lock_expires = False
Expand Down Expand Up @@ -57,15 +57,15 @@ def _acquire(self):
'_id': self.lock_name,
'ttl': ttl,
'client_id': self._client_id},
w=1, j=1)
w=1, j=True)
except errors.DuplicateKeyError:
self.collection.remove(
{"_id": self.lock_name, 'ttl': {'$lt': datetime.now()}})
try:
self.collection.insert(
{'_id': self.lock_name,
'ttl': ttl,
'client_id': self._client_id}, w=1, j=1)
'client_id': self._client_id}, w=1, j=True)
except errors.DuplicateKeyError:
self._locked = False
return self._locked
Expand Down
24 changes: 4 additions & 20 deletions mongoqueue/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class MongoLockTest(TestCase):

def setUp(self):
self.client = pymongo.Connection(os.environ.get("TEST_MONGODB"))
self.client = pymongo.MongoClient(os.environ.get("TEST_MONGODB"))
self.db = self.client.test_queue
self.collection = self.db.locks

Expand All @@ -24,7 +24,7 @@ def tearDown(self):
def test_lock_acquire_release_context_manager(self):
with lock(self.collection, 'test1') as l:
self.assertTrue(l.locked)
self.assertEqual(self.collection.find().count(), 0)
self.assertEqual(self.collection.count(),0)

def test_auto_expires_old(self):
lock = MongoLock(self.collection, 'test2', lease=2)
Expand Down Expand Up @@ -54,7 +54,7 @@ def test_auto_expires_old(self):
class MongoQueueTest(TestCase):

def setUp(self):
self.client = pymongo.Connection(os.environ.get("TEST_MONGODB"))
self.client = pymongo.MongoClient(os.environ.get("TEST_MONGODB"))
self.db = self.client.test_queue
self.queue = MongoQueue(self.db.queue_1, "consumer_1")

Expand Down Expand Up @@ -117,22 +117,6 @@ def test_error(self):
def test_progress(self):
pass

def test_stats(self):

for i in range(5):
data = {"context_id": "alpha",
"data": [1, 2, 3],
"more-data": time.time()}
self.queue.put(data)
job = self.queue.next()
job.error("problem")

stats = self.queue.stats()
self.assertEqual({'available': 5,
'total': 5,
'locked': 0,
'errors': 0}, stats)

def test_context_manager_error(self):
self.queue.put({"foobar": 1})
job = self.queue.next()
Expand All @@ -145,7 +129,7 @@ def test_context_manager_error(self):
pass

job = self.queue.next()
self.assertEqual(job.data['attempts'], 1)
self.assertEqual(job.attempts, 1)

def test_context_manager_complete(self):
self.queue.put({"foobar": 1})
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pymongo>=2.4