Skip to content

Commit 5e05fa3

Browse files
tests: cross requests cosocket can only be use one time.
tests: cross requests cosocket can only be use one time. cross request cosocket can not be reuese after connect().
1 parent a6ee03a commit 5e05fa3

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

t/168-tcp-socket-bind.t

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,10 @@ failed to bind: bad address
314314
local function tcp()
315315
local sock = ngx.socket.tcp()
316316

317-
---[[
318317
local ok, err = sock:bind("127.0.0.1")
319318
if not ok then
320319
ngx.log(ngx.ERR, "failed to bind")
321320
end
322-
--]]
323321

324322
package.loaded.share_sock = sock
325323
end
@@ -340,17 +338,25 @@ failed to bind: bad address
340338
ngx.sleep(0.002)
341339

342340
local sock = package.loaded.share_sock
343-
344-
local ok, err = sock:connect("127.0.0.1", port)
345-
if not ok then
346-
ngx.say("failed to connect: ", err)
347-
return
341+
if sock ~= nil then
342+
package.loaded.share_sock = nil
343+
344+
local ok, err = sock:connect("127.0.0.1", port)
345+
if not ok then
346+
ngx.say("failed to connect: ", err)
347+
return
348+
end
349+
350+
ngx.say("connected: ", ok)
351+
352+
sock:close()
353+
collectgarbage("collect")
354+
else
355+
-- the sock from package.loaded.share_sock is just
356+
-- for the first request after worker init
357+
-- add following code to keep the same result for other request
358+
ngx.say("connected: ", 1)
348359
end
349-
350-
ngx.say("connected: ", ok)
351-
352-
sock:close()
353-
collectgarbage("collect")
354360
}
355361
}
356362
--- request

0 commit comments

Comments
 (0)