mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 03:53:09 +00:00
fix mongodb testcase
This commit is contained in:
@@ -2,55 +2,64 @@ local skynet = require "skynet"
|
||||
local mongo = require "skynet.db.mongo"
|
||||
local bson = require "bson"
|
||||
|
||||
local host, db_name = ...
|
||||
local host, port, db_name, username, password = ...
|
||||
|
||||
local function _create_client()
|
||||
return mongo.client(
|
||||
{
|
||||
host = host, port = port,
|
||||
username = username, password = password,
|
||||
authdb = db_name,
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
function test_insert_without_index()
|
||||
local db = mongo.client({host = host})
|
||||
|
||||
local db = _create_client()
|
||||
db[db_name].testdb:dropIndex("*")
|
||||
db[db_name].testdb:drop()
|
||||
|
||||
local ret = db[db_name].testdb:safe_insert({test_key = 1});
|
||||
assert(ret and ret.n == 1)
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1});
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ret = db[db_name].testdb:safe_insert({test_key = 1});
|
||||
assert(ret and ret.n == 1)
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1});
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
end
|
||||
|
||||
function test_insert_with_index()
|
||||
local db = mongo.client({host = host})
|
||||
local db = _create_client()
|
||||
|
||||
db[db_name].testdb:dropIndex("*")
|
||||
db[db_name].testdb:drop()
|
||||
|
||||
db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"})
|
||||
|
||||
local ret = db[db_name].testdb:safe_insert({test_key = 1})
|
||||
assert(ret and ret.n == 1)
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1})
|
||||
assert(ok and ret and ret.n == 1)
|
||||
|
||||
local ret = db[db_name].testdb:safe_insert({test_key = 1})
|
||||
assert(ret and ret.n == 0)
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1})
|
||||
assert(ok == false and string.find(err, "duplicate key error"))
|
||||
end
|
||||
|
||||
function test_find_and_remove()
|
||||
local db = mongo.client({host = host})
|
||||
local db = _create_client()
|
||||
|
||||
db[db_name].testdb:dropIndex("*")
|
||||
db[db_name].testdb:drop()
|
||||
|
||||
db[db_name].testdb:ensureIndex({test_key = 1}, {test_key2 = -1}, {unique = true, name = "test_index"})
|
||||
|
||||
local ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 1})
|
||||
assert(ret and ret.n == 1)
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 1})
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 2})
|
||||
assert(ret and ret.n == 1)
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_key2 = 2})
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ret = db[db_name].testdb:safe_insert({test_key = 2, test_key2 = 3})
|
||||
assert(ret and ret.n == 1)
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 2, test_key2 = 3})
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ret = db[db_name].testdb:findOne({test_key2 = 1})
|
||||
assert(ret and ret.test_key2 == 1)
|
||||
assert(ret and ret.test_key2 == 1, err)
|
||||
|
||||
local ret = db[db_name].testdb:find({test_key2 = {['$gt'] = 0}}):sort({test_key = 1}, {test_key2 = -1}):skip(1):limit(1)
|
||||
assert(ret:count() == 3)
|
||||
@@ -69,7 +78,7 @@ end
|
||||
|
||||
|
||||
function test_expire_index()
|
||||
local db = mongo.client({host = host})
|
||||
local db = _create_client()
|
||||
|
||||
db[db_name].testdb:dropIndex("*")
|
||||
db[db_name].testdb:drop()
|
||||
@@ -77,21 +86,21 @@ function test_expire_index()
|
||||
db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, })
|
||||
db[db_name].testdb:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, })
|
||||
|
||||
local ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())})
|
||||
assert(ret and ret.n == 1)
|
||||
local ok, err, ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())})
|
||||
assert(ok and ret and ret.n == 1, err)
|
||||
|
||||
local ret = db[db_name].testdb:findOne({test_key = 1})
|
||||
assert(ret and ret.test_key == 1)
|
||||
|
||||
for i = 1, 1000 do
|
||||
skynet.sleep(1);
|
||||
|
||||
for i = 1, 60 do
|
||||
skynet.sleep(100);
|
||||
print("check expire", i)
|
||||
local ret = db[db_name].testdb:findOne({test_key = 1})
|
||||
if ret == nil then
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
print("test expire index failed")
|
||||
assert(false, "test expire index failed");
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user