Skip to content

Commit caf32d6

Browse files
committed
more tests, fix bug
1 parent 99bae54 commit caf32d6

File tree

3 files changed

+98
-5
lines changed

3 files changed

+98
-5
lines changed

lapis/db/postgres.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ configure = function(pool_name, config)
237237
else
238238
pgmoon:disconnect()
239239
end
240-
if ctx_name then
240+
if use_nginx and ctx_name then
241241
ngx.ctx[ctx_name] = nil
242242
else
243243
pgmoon_conn = nil

lapis/db/postgres.moon

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ configure = (pool_name, config) ->
225225
else
226226
pgmoon\disconnect!
227227
228-
if ctx_name
228+
if use_nginx and ctx_name
229229
ngx.ctx[ctx_name] = nil
230230
else
231231
pgmoon_conn = nil
@@ -270,7 +270,6 @@ configure = (pool_name, config) ->
270270
increment_perf "db_time", dt
271271
increment_perf "db_count", 1
272272
273-
-- TODO: consider a different naming convention here
274273
if logger = db.logger
275274
if logger.query
276275
if is_default_pool and ctx_name

spec/postgres_spec.moon

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,6 @@ describe "lapis.db.postgres", ->
10171017
}
10181018
}
10191019

1020-
10211020
connect_count = 0
10221021
disconnect_count = 0
10231022
connect_logs = {}
@@ -1043,14 +1042,82 @@ describe "lapis.db.postgres", ->
10431042
{@opts.application_name, q}
10441043

10451044
stub(Postgres.__base, "disconnect").invokes =>
1046-
error "no disconnect should happen"
1045+
@state = "disconnected"
1046+
disconnect_count += 1
10471047

10481048
-- no logging needed
10491049
stub(require("lapis.logging"), "query").invokes ->
10501050

10511051
stub(require("lapis.logging"), "db_connection").invokes (...) ->
10521052
table.insert connect_logs, {...}
10531053

1054+
describe "outside nginx", ->
1055+
it "connects and disconnects", ->
1056+
conn = assert require("lapis.db.postgres").connect!
1057+
assert.same {nil, "already connected"}, { require("lapis.db.postgres").connect! }
1058+
assert require("lapis.db.postgres").disconnect!
1059+
1060+
assert.same {
1061+
opts: {
1062+
application_name: "config_default"
1063+
pool_name: "pgmoon_default" -- this has no effect when not in nginx
1064+
}
1065+
state: "disconnected"
1066+
}, conn
1067+
1068+
it "connects from query", ->
1069+
assert.same {
1070+
"config_default"
1071+
"hello"
1072+
}, assert require("lapis.db.postgres").query "hello"
1073+
1074+
assert.same {
1075+
"config_default"
1076+
"world"
1077+
}, assert require("lapis.db.postgres").query "world"
1078+
1079+
assert.same 1, connect_count
1080+
assert.same 0, disconnect_count
1081+
1082+
require("lapis.db.postgres").disconnect!
1083+
1084+
assert.same {
1085+
"config_default"
1086+
"flarp"
1087+
}, assert require("lapis.db.postgres").query "flarp"
1088+
1089+
assert.same 2, connect_count
1090+
assert.same 1, disconnect_count
1091+
1092+
assert.same 2, #connect_logs
1093+
1094+
it "creates anonymous connection", ->
1095+
db = require("lapis.db.postgres").configure {
1096+
application_name: "anonymous"
1097+
}
1098+
1099+
conn = db.connect!
1100+
1101+
assert.same 1, connect_count
1102+
assert.same 0, disconnect_count
1103+
1104+
assert.same {
1105+
state: "connected"
1106+
opts: { application_name: "anonymous" }
1107+
}, conn
1108+
1109+
db.disconnect!
1110+
1111+
assert.same 1, connect_count
1112+
assert.same 1, disconnect_count
1113+
1114+
assert.same {
1115+
state: "disconnected"
1116+
opts: { application_name: "anonymous" }
1117+
}, conn
1118+
1119+
1120+
10541121
describe "in ngx", ->
10551122
local ngx
10561123

@@ -1277,3 +1344,30 @@ describe "lapis.db.postgres", ->
12771344
it "disconnect before connect is safe", ->
12781345
db = require("lapis.db.postgres").configure { application_name: "test" }
12791346
assert.nil (db.disconnect!)
1347+
1348+
-- connection with pool name
1349+
it "creates anonymous connection", ->
1350+
db = require("lapis.db.postgres").configure {
1351+
application_name: "anonymous"
1352+
}
1353+
1354+
conn = db.connect!
1355+
1356+
assert.same 1, connect_count
1357+
assert.same 0, disconnect_count
1358+
1359+
assert.same {
1360+
state: "connected"
1361+
opts: { application_name: "anonymous" }
1362+
}, conn
1363+
1364+
1365+
run_after_dispatch!
1366+
1367+
assert.same 1, connect_count
1368+
assert.same 1, disconnect_count
1369+
1370+
assert.same {
1371+
state: "keepalive"
1372+
opts: { application_name: "anonymous" }
1373+
}, conn

0 commit comments

Comments
 (0)