Skip to content

Commit 2404596

Browse files
committed
issue #313: Add tests
1 parent 2eca716 commit 2404596

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

couchdb/tests/client.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# you should have received as part of this distribution.
88

99
from datetime import datetime
10+
import functools
11+
import multiprocessing
1012
import os
1113
import os.path
1214
import shutil
@@ -19,6 +21,10 @@
1921
from couchdb.tests import testutil
2022

2123

24+
def _current_pid():
25+
return os.getpid()
26+
27+
2228
class ServerTestCase(testutil.TempDatabaseMixin, unittest.TestCase):
2329

2430
def test_init_with_resource(self):
@@ -481,7 +487,9 @@ def test_changes_releases_conn(self):
481487
# that the HTTP connection made it to the pool.
482488
list(self.db.changes(feed='continuous', timeout=0))
483489
scheme, netloc = util.urlsplit(client.DEFAULT_BASE_URL)[:2]
484-
self.assertTrue(self.db.resource.session.connection_pool.conns[(scheme, netloc)])
490+
current_pid = _current_pid()
491+
key = (current_pid, scheme, netloc)
492+
self.assertTrue(self.db.resource.session.connection_pool.conns[key])
485493

486494
def test_changes_releases_conn_when_lastseq(self):
487495
# Consume a changes feed, stopping at the 'last_seq' item, i.e. don't
@@ -490,8 +498,10 @@ def test_changes_releases_conn_when_lastseq(self):
490498
for obj in self.db.changes(feed='continuous', timeout=0):
491499
if 'last_seq' in obj:
492500
break
501+
current_pid = _current_pid()
493502
scheme, netloc = util.urlsplit(client.DEFAULT_BASE_URL)[:2]
494-
self.assertTrue(self.db.resource.session.connection_pool.conns[(scheme, netloc)])
503+
key = (current_pid, scheme, netloc)
504+
self.assertTrue(self.db.resource.session.connection_pool.conns[key])
495505

496506
def test_changes_conn_usable(self):
497507
# Consume a changes feed to get a used connection in the pool.
@@ -838,8 +848,33 @@ def test_startkey(self):
838848
def test_nullkeys(self):
839849
self.assertEqual(len(list(self.db.iterview('test/nulls', 10))), self.num_docs)
840850

851+
852+
def _get_by_id(db, result, id):
853+
result.append(db[id])
854+
855+
856+
class TestConcurrent(testutil.TempDatabaseMixin, unittest.TestCase):
857+
def test_concurrent_get(self):
858+
self.db.save({'_id': 'foo', 'value': 'hello'})
859+
self.db.save({'_id': 'bar', 'value': 'world'})
860+
processes = []
861+
result = multiprocessing.Manager().list()
862+
for id in ('foo', 'bar'):
863+
process = multiprocessing.Process(target=functools.partial(_get_by_id, self.db, result),
864+
args=(id,))
865+
processes.append(process)
866+
process.start()
867+
868+
for process in processes:
869+
process.join()
870+
871+
self.assertEqual(len(result), 2)
872+
self.assertEqual(set(['hello', 'world']), set([r['value'] for r in result]))
873+
874+
841875
def suite():
842876
suite = unittest.TestSuite()
877+
suite.addTest(unittest.makeSuite(TestConcurrent, 'test'))
843878
suite.addTest(unittest.makeSuite(ServerTestCase, 'test'))
844879
suite.addTest(unittest.makeSuite(DatabaseTestCase, 'test'))
845880
suite.addTest(unittest.makeSuite(ViewTestCase, 'test'))

0 commit comments

Comments
 (0)