diff --git a/examples/config.mongodb b/examples/config.mongodb new file mode 100644 index 00000000..6413b57d --- /dev/null +++ b/examples/config.mongodb @@ -0,0 +1,11 @@ +root = "./" +thread = 8 +logger = nil +harbor = 0 +start = "main_mongodb" -- main script +bootstrap = "snlua bootstrap" -- The service for bootstrap +luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" +lualoader = "lualib/loader.lua" +snax = root.."examples/?.lua;"..root.."test/?.lua" +cpath = root.."cservice/?.so" +-- daemon = "./skynet.pid" diff --git a/examples/main_mongodb.lua b/examples/main_mongodb.lua new file mode 100644 index 00000000..0f505ce6 --- /dev/null +++ b/examples/main_mongodb.lua @@ -0,0 +1,12 @@ +local skynet = require "skynet" + + +skynet.start(function() + print("Main Server start") + local console = skynet.newservice( + "testmongodb", "127.0.0.1", 27017, "testdb", "test", "test" + ) + + print("Main Server exit") + skynet.exit() +end) diff --git a/lualib/skynet/db/mongo.lua b/lualib/skynet/db/mongo.lua index ec8063a6..e020ebcb 100644 --- a/lualib/skynet/db/mongo.lua +++ b/lualib/skynet/db/mongo.lua @@ -309,7 +309,7 @@ end function mongo_db:auth(user, pass) local authmod = rawget(self.connection, "authmod") or "scram_sha1" - local auth_func = auth_method[authmod] + local auth_func = auth_method["auth_" .. authmod] assert(auth_func , "Invalid authmod") return auth_func(self, user, pass) end diff --git a/test/testmongodb.lua b/test/testmongodb.lua index 11d819be..cda41ec4 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -3,6 +3,11 @@ local mongo = require "skynet.db.mongo" local bson = require "bson" local host, port, db_name, username, password = ... +if port then + port = math.tointeger(port) +end + +-- print(host, port, db_name, username, password) local function _create_client() return mongo.client( @@ -14,6 +19,25 @@ local function _create_client() ) end +function test_auth() + local c = mongo.client( + { + host = host, port = port, + } + ) + db = c[db_name] + db:auth(username, password) + + db.testdb:dropIndex("*") + db.testdb:drop() + + local ok, err, ret = db.testdb:safe_insert({test_key = 1}); + assert(ok and ret and ret.n == 1, err) + + local ok, err, ret = db.testdb:safe_insert({test_key = 1}); + assert(ok and ret and ret.n == 1, err) +end + function test_insert_without_index() local db = _create_client() db[db_name].testdb:dropIndex("*") @@ -105,6 +129,10 @@ function test_expire_index() end skynet.start(function() + if username then + print("Test auth") + test_auth() + end print("Test insert without index") test_insert_without_index() print("Test insert index")