【发布时间】:2012-07-03 22:24:50
【问题描述】:
我正在尝试设置一个脚本来为我的使用 MongoDB 的 node.js 程序执行测试。我的想法是我想要一个可以运行的脚本:
- 启动 MongoDB 进程,作为守护进程分叉
- 用一些测试数据预填充数据库
- 永远启动我的节点服务器,因此它作为守护进程运行
- 运行我的测试
- 从数据库中删除测试数据
我有一个粗略的脚本来执行所有这些步骤。我的问题是 MongoDB 需要花费可变的时间来设置,这导致我的脚本中出现 sleep 调用。因此它只是偶尔起作用。
# the directory of this script DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" # launch mongodb. $DIR/../../db/mongod --fork --logpath ./logs/mongodb.log --logappend --dbpath ./testdb/ --quiet # takes a bit of time for the database to get set up, # and since we make it a daemon process we cant run the tests immediately sleep 1 # avoid EADDRINUSE errors because existing node servers are up. killall node &> /dev/null # start up our node server using a test database. forever start $DIR/../main.js --dbname=testdb --logpath=test/logs/testlog.log # takes a bit of time for node to get set up, # and since we make it a daemon process we cant run the tests immediately sleep 1 # run any database setup code (inject data for testing) $DIR/../../db/mongo 127.0.0.1:27017/testdb $DIR/setup.js --quiet # actually run the tests node $DIR/tests.js # kill the servers (this could be a little less heavy handed...) killall node &> /dev/null killall forever &> /dev/null # finally tear down the database (drop anything we've added to the test db) $DIR/../../db/mongo 127.0.0.1:27017/testdb $DIR/teardown.js --quiet # and then shut mogodb down kill -2 `ps ax | grep mongod | grep -v grep | awk '{print $1}'`
完成我想做的事情的最佳方式是什么?我是在这里陷入困境,还是我在 MongoDB 文档中遗漏了什么?
【问题讨论】:
-
我对 node 或 mongo 一无所知,但你会用其他语言编写代码,这样它就不会依赖于数据库。
-
你为什么不多睡一会儿呢?可变时间是什么意思?
-
有什么理由不能让 mongodb 一直运行?您已经在使用测试数据库,所以我不认为您会有冲突的进程。
标签: bash node.js shell mongodb