1

我想用鼻子来测试我正在使用twisted 和txmongo 编写的应用程序。我什至无法获得像以下工作这样的简单用例: from nose.twistedtools import reactor, deferred, threaded_reactor import logging from twisted.internet import defer import txmongo

log = logging.getLogger("common.test.test_db")

conn = txmongo.lazyMongoConnectionPool('localhost', 27017, 4)

@deferred()
def test_mongo():
    tdb = conn.test

    @defer.inlineCallbacks
    def cb(oid):
        assert oid
        obj = yield tdb.test.find({"_id":oid})
        log.error("In callback")
        assert obj 

    d = tdb.test.save({"s":1, "b":2})
    d.addCallback(cb)

    return d

但是,这总是返回以下内容:

E
======================================================================
ERROR: common.test.test_db.test_mongo
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Volumes/Users/jce/.pyenv/celery/lib/python2.6/site-packages/nose/case.py", line 186, in runTest
    self.test(*self.arg)
  File "/Volumes/Users/jce/.pyenv/celery/lib/python2.6/site-packages/nose/twistedtools.py", line 138, in errback
    failure.raiseException()
  File "/Volumes/Users/jce/.pyenv/celery/lib/python2.6/site-packages/twisted/python/failure.py", line 326, in raiseException
    raise self.type, self.value, self.tb
RuntimeWarning: not connected

----------------------------------------------------------------------
Ran 1 test in 0.006s

FAILED (errors=1)

我尝试手动添加 threaded_reactor() 调用,但没有帮助。

编辑

我删除了“懒惰”的连接,并修改了代码,现在它可以工作了......我仍然很好奇为什么“懒惰”没有工作。工作代码如下:

dbconn = txmongo.MongoConnectionPool('localhost', 27017, 4)

@deferred()
def test_mongo():
    @defer.inlineCallbacks
    def cb(conn):
        tdb = conn.test
        oid = yield tdb.test.save({"s":1, "b":2})
        assert oid
        log.error(str(oid))
        obj = yield tdb.test.find({"_id":oid})
        assert obj 
        log.error(str(obj))
    dbconn.addCallback(cb)
    return dbconn
4

1 回答 1

1

MongoConnectionPool will return a deferred, which is fired when the connection is established passing the connection handler as argument to the callback. You should conn = yield MongoConnectionPool().

lazyMongoConnectionPool will return the connection handler directly, without waiting for the connection to be established.

Lazy is usually used by web servers and other services that doesn't require immediate connection when your service starts. If you want to do so, don't use the lazy method.

于 2010-11-05T04:28:40.990 回答