|
| 1 | +local skynet = require "skynet" |
| 2 | +local mongo = require "mongo" |
| 3 | +local bson = require "bson" |
| 4 | + |
| 5 | +local host, db_name = ... |
| 6 | + |
| 7 | +function test_insert_without_index() |
| 8 | + local db = mongo.client({host = host}) |
| 9 | + |
| 10 | + db[db_name].testdb:dropIndex("*") |
| 11 | + db[db_name].testdb:drop() |
| 12 | + |
| 13 | + local ret = db[db_name].testdb:safe_insert({test_key = 1}); |
| 14 | + assert(ret and ret.n == 1) |
| 15 | + |
| 16 | + local ret = db[db_name].testdb:safe_insert({test_key = 1}); |
| 17 | + assert(ret and ret.n == 1) |
| 18 | +end |
| 19 | + |
| 20 | +function test_insert_with_index() |
| 21 | + local db = mongo.client({host = host}) |
| 22 | + |
| 23 | + db[db_name].testdb:dropIndex("*") |
| 24 | + db[db_name].testdb:drop() |
| 25 | + |
| 26 | + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) |
| 27 | + |
| 28 | + local ret = db[db_name].testdb:safe_insert({test_key = 1}) |
| 29 | + assert(ret and ret.n == 1) |
| 30 | + |
| 31 | + local ret = db[db_name].testdb:safe_insert({test_key = 1}) |
| 32 | + assert(ret and ret.n == 0) |
| 33 | +end |
| 34 | + |
| 35 | +function test_find_and_remove() |
| 36 | + local db = mongo.client({host = host}) |
| 37 | + |
| 38 | + db[db_name].testdb:dropIndex("*") |
| 39 | + db[db_name].testdb:drop() |
| 40 | + |
| 41 | + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) |
| 42 | + |
| 43 | + local ret = db[db_name].testdb:safe_insert({test_key = 1}) |
| 44 | + assert(ret and ret.n == 1) |
| 45 | + |
| 46 | + local ret = db[db_name].testdb:findOne({test_key = 1}) |
| 47 | + assert(ret and ret.test_key == 1) |
| 48 | + |
| 49 | + db[db_name].testdb:delete({test_key = 1}) |
| 50 | + |
| 51 | + local ret = db[db_name].testdb:findOne({test_key = 1}) |
| 52 | + assert(ret == nil) |
| 53 | +end |
| 54 | + |
| 55 | + |
| 56 | +function test_expire_index() |
| 57 | + local db = mongo.client({host = host}) |
| 58 | + |
| 59 | + db[db_name].testdb:dropIndex("*") |
| 60 | + db[db_name].testdb:drop() |
| 61 | + |
| 62 | + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, }) |
| 63 | + db[db_name].testdb:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, }) |
| 64 | + |
| 65 | + local ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())}) |
| 66 | + assert(ret and ret.n == 1) |
| 67 | + |
| 68 | + local ret = db[db_name].testdb:findOne({test_key = 1}) |
| 69 | + assert(ret and ret.test_key == 1) |
| 70 | + |
| 71 | + for i = 1, 1000 do |
| 72 | + skynet.sleep(11); |
| 73 | + |
| 74 | + local ret = db[db_name].testdb:findOne({test_key = 1}) |
| 75 | + if ret == nil then |
| 76 | + return |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + assert(false, "test expire index failed"); |
| 81 | +end |
| 82 | + |
| 83 | +skynet.start(function() |
| 84 | + test_insert_without_index() |
| 85 | + test_insert_with_index() |
| 86 | + test_find_and_remove() |
| 87 | + test_expire_index() |
| 88 | + |
| 89 | + print("mongodb test finish."); |
| 90 | +end) |
0 commit comments