Skip to content

Commit

Permalink
Add support for Python 2 (#8)
Browse files Browse the repository at this point in the history
Add support for Python 2 (fixes #6)
  • Loading branch information
philipbl committed Nov 17, 2016
1 parent e319e5e commit 0b75d8c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
12 changes: 9 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
sudo: false
language: python
cache: pip
python:
- "3.4"
- "3.5"
matrix:
fast_finish: true
include:
- python: "2.7"
- python: "3.4"
- python: "3.5"
- python: "3.6-dev"
allow_failures:
- python: "3.6-dev"
install:
- "pip install -r requirements_test.txt"
script:
Expand Down
3 changes: 2 additions & 1 deletion persistent_queue/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ def flush(self):
# the danger zone.

_LOGGER.debug("Replacing old file with new file")
os.replace(temp_filename, os.path.join(self.path, self.filename))
os.remove(os.path.join(self.path, self.filename))
os.rename(temp_filename, os.path.join(self.path, self.filename))
self.file = self._open_file()

_LOGGER.debug("Finished flushing the queue")
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"License :: OSI Approved :: MIT License",
"Development Status :: 5 - Production/Stable",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.4",
"Programming Language :: Python :: 3.5",
"Topic :: Software Development :: Libraries :: Python Modules",
Expand Down
31 changes: 14 additions & 17 deletions tests/test_persistent_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,18 @@ def test_pop(self):
self.assertEqual(len(self.queue), 1)

def test_pop_blocking(self):
done = False
done = [False]
def func():
nonlocal done
time.sleep(1)
done = True
done[0] = True
self.queue.push(5)

t = threading.Thread(target=func)
t.start()

self.assertFalse(done)
self.assertFalse(done[0])
data = self.queue.pop(blocking=True)
self.assertTrue(done)
self.assertTrue(done[0])
self.assertEqual(data, 5)
self.assertEqual(len(self.queue), 0)

Expand Down Expand Up @@ -133,40 +132,38 @@ def test_peek(self):
self.assertEqual(self.queue.peek(0), [])

def test_peek_blocking(self):
done = False
done = [False]
def func():
nonlocal done
time.sleep(1)
done = True
done[0] = True
self.queue.push(5)

t = threading.Thread(target=func)
t.start()

self.assertFalse(done)
self.assertFalse(done[0])
data = self.queue.peek(blocking=True)
self.assertTrue(done)
self.assertTrue(done[0])
self.assertEqual(data, 5)
self.assertEqual(len(self.queue), 1)

def test_peek_blocking_list(self):
done_pushing = False
done_peeking = False
done_pushing = [False]
done_peeking = [False]

def func():
nonlocal done_pushing
for i in range(5):
time.sleep(.1)
self.queue.push(i)
self.assertFalse(done_peeking)
done_pushing = True
self.assertFalse(done_peeking[0])
done_pushing[0] = True

t = threading.Thread(target=func)
t.start()

data = self.queue.peek(5, blocking=True)
done_peeking = True
self.assertTrue(done_pushing)
done_peeking[0] = True
self.assertTrue(done_pushing[0])
self.assertEqual(data, [0, 1, 2, 3, 4])
self.assertEqual(len(self.queue), 5)

Expand Down

0 comments on commit 0b75d8c

Please sign in to comment.