Close listening socket outside of start

This commit is contained in:
Loic Nageleisen 2017-09-19 13:48:45 +02:00
parent eea9588742
commit 53988be5ee
2 changed files with 8 additions and 3 deletions

View file

@ -11,14 +11,15 @@ module NanoServe
@port = port @port = port
@block = block @block = block
@thr = nil @thr = nil
@srv = nil
end end
def start(y) def start(y)
server = TCPServer.new(@port) @srv = TCPServer.new(@port)
@thr = Thread.new do @thr = Thread.new do
Thread.abort_on_exception = true Thread.abort_on_exception = true
conn = server.accept conn = @srv.accept
port, host = conn.peeraddr[1, 2] port, host = conn.peeraddr[1, 2]
client = "#{host}:#{port}" client = "#{host}:#{port}"
logger.debug "#{client}: connected" logger.debug "#{client}: connected"
@ -38,12 +39,12 @@ module NanoServe
yield yield
@thr.join @thr.join
server.close
y y
end end
def stop def stop
@srv.close
@thr.kill @thr.kill
end end

View file

@ -24,6 +24,8 @@ class TestNanoServe < MiniTest::Test
s.close s.close
end end
r.stop
assert_equal(uuid, buf) assert_equal(uuid, buf)
end end
@ -50,6 +52,8 @@ class TestNanoServe < MiniTest::Test
Net::HTTP.get(uri + "test?uuid=#{uuid}") Net::HTTP.get(uri + "test?uuid=#{uuid}")
end end
r.stop
assert_equal(uuid, req.first.params['uuid']) assert_equal(uuid, req.first.params['uuid'])
end end
end end