forked from bitly/asyncmongo
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* handle authentication transparently on each connection
- Loading branch information
Showing
8 changed files
with
229 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env python | ||
|
||
# mkdir /tmp/asyncmongo_sample_app2 | ||
# mongod --port 27017 --oplogSize 10 --dbpath /tmp/asyncmongo_sample_app2 | ||
|
||
# $mongo | ||
# >>>use test; | ||
# db.addUser("testuser", "testpass"); | ||
|
||
# ab -n 1000 -c 16 http://127.0.0.1:8888/ | ||
|
||
import sys | ||
import logging | ||
import os | ||
app_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) | ||
if app_dir not in sys.path: | ||
logging.debug('adding %r to sys.path' % app_dir) | ||
sys.path.insert(0, app_dir) | ||
|
||
import asyncmongo | ||
# make sure we get the local asyncmongo | ||
assert asyncmongo.__file__.startswith(app_dir) | ||
|
||
import tornado.ioloop | ||
import tornado.web | ||
import tornado.options | ||
|
||
class MainHandler(tornado.web.RequestHandler): | ||
@tornado.web.asynchronous | ||
def get(self): | ||
db.users.find_one({"user_id" : 1}, callback=self._on_response) | ||
|
||
def _on_response(self, response, error): | ||
assert not error | ||
self.write(str(response)) | ||
self.finish() | ||
|
||
|
||
if __name__ == "__main__": | ||
tornado.options.parse_command_line() | ||
application = tornado.web.Application([ | ||
(r"/?", MainHandler) | ||
]) | ||
application.listen(8888) | ||
db = asyncmongo.Client(pool_id="test", | ||
host='127.0.0.1', | ||
port=27017, | ||
mincached=5, | ||
maxcached=15, | ||
maxconnections=30, | ||
dbname='test', | ||
dbuser='testuser', | ||
dbpass='testpass') | ||
tornado.ioloop.IOLoop.instance().start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import tornado.ioloop | ||
import time | ||
import logging | ||
import subprocess | ||
|
||
import test_shunt | ||
import asyncmongo | ||
|
||
TEST_TIMESTAMP = int(time.time()) | ||
|
||
class AuthenticationTest(test_shunt.MongoTest): | ||
def setUp(self): | ||
super(AuthenticationTest, self).setUp() | ||
logging.info('creating user') | ||
pipe = subprocess.Popen('''echo -e 'use test;\n db.addUser("testuser", "testpass");\n exit;' | mongo --port 27017 --host 127.0.0.1''', shell=True) | ||
pipe.wait() | ||
|
||
def test_authentication(self): | ||
try: | ||
test_shunt.setup() | ||
db = asyncmongo.Client(pool_id='testauth', host='127.0.0.1', port=27017, dbname='test', dbuser='testuser', dbpass='testpass', maxconnections=2) | ||
|
||
def update_callback(response, error): | ||
logging.info(response) | ||
assert len(response) == 1 | ||
test_shunt.register_called('update') | ||
tornado.ioloop.IOLoop.instance().stop() | ||
|
||
db.test_stats.update({"_id" : TEST_TIMESTAMP}, {'$inc' : {'test_count' : 1}}, upsert=True, callback=update_callback) | ||
|
||
tornado.ioloop.IOLoop.instance().start() | ||
test_shunt.assert_called('update') | ||
|
||
def query_callback(response, error): | ||
logging.info(response) | ||
logging.info(error) | ||
assert error is None | ||
assert isinstance(response, dict) | ||
assert response['_id'] == TEST_TIMESTAMP | ||
assert response['test_count'] == 1 | ||
test_shunt.register_called('retrieved') | ||
tornado.ioloop.IOLoop.instance().stop() | ||
|
||
db.test_stats.find_one({"_id" : TEST_TIMESTAMP}, callback=query_callback) | ||
tornado.ioloop.IOLoop.instance().start() | ||
test_shunt.assert_called('retrieved') | ||
except: | ||
tornado.ioloop.IOLoop.instance().stop() | ||
raise | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters