Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

lib: streams use destroy() instead of close() and make tests pass

follow nodejs and use destroy() for closing streams

Next steps
- Stop methods from using fields that might not exist after destroy starts
- Standardize the destroying flag name for stream
  • Loading branch information...
commit 8b2b226eb8bb978c3f09d9e8f3b02f28e4b099b9 1 parent f0baa30
Brandon Philips authored
View
4 lib/luvit/core.lua
@@ -229,8 +229,8 @@ function iStream:pipe(target)
end)
function onclose()
- if target.close then
- target:close()
+ if target.destroy then
+ target:destroy()
end
end
View
12 lib/luvit/http.lua
@@ -90,8 +90,8 @@ function Request:initialize(socket)
self.socket = socket
end
-function Request:close(...)
- return self.socket:close(...)
+function Request:destroy(...)
+ return self.socket:destroy(...)
end
--------------------------------------------------------------------------------
@@ -306,7 +306,7 @@ function Response:done(callback)
if not self.should_keep_alive then
self.socket:shutdown(function ()
self:emit("end")
- self:close()
+ self:destroy()
if callback then
self:on("closed", callback)
end
@@ -318,8 +318,8 @@ function Response:done(callback)
end
end
-function Response:close(...)
- return self.socket:close(...)
+function Response:destroy(...)
+ return self.socket:destroy(...)
end
--------------------------------------------------------------------------------
@@ -472,7 +472,7 @@ function http.request(options, callback)
end
if callback then callback() end
end
- client.close = function (self, chunk, callback)
+ client.destroy = function (self, chunk, callback)
self.done = true
self:write(chunk, callback)
end
View
10 lib/luvit/net.lua
@@ -31,6 +31,9 @@ local net = {}
local Socket = iStream:extend()
function Socket:_connect(address, port, addressType)
+ if self.destroyed then
+ return
+ end
if port then
self.remotePort = port
end
@@ -77,6 +80,9 @@ function Socket:pipe(destination)
end
function Socket:write(data, callback)
+ if self.destroyed then
+ return
+ end
self.bytesWritten = self.bytesWritten + #data
if self._connecting == true then
@@ -321,7 +327,9 @@ function Server:close(callback)
if callback then
self:once('close', callback)
end
- self._handle:close()
+ if self._handle then
+ self._handle:close()
+ end
self:_emitClosedIfDrained()
end
View
25 lib/luvit/tls.lua
@@ -206,7 +206,7 @@ function CryptoStream:destroySoon()
if self.writable == true then
self:done()
else
- self:close()
+ self:destroy()
end
end
@@ -239,12 +239,12 @@ function CryptoStream:getPeerCertificate()
return nil
end
-function CryptoStream:close()
+function CryptoStream:destroy()
dbg('destroy')
if self.pair._doneFlag == true then
return
end
- self.pair:close()
+ self.pair:destroy()
end
function CryptoStream:_done()
@@ -257,7 +257,7 @@ function CryptoStream:_done()
if self.pair._secureEstablished == false then
self.pair:err()
else
- self.pair:close()
+ self.pair:destroy()
end
end
end
@@ -279,6 +279,7 @@ function CryptoStream:_push()
chunkBytes, tmpData = self:_pusher()
if self.pair.ssl and self.pair.ssl:getError() then
+ p('push error')
self.pair:err()
return
end
@@ -319,6 +320,7 @@ function CryptoStream:_pull()
local rv = self:_puller(tmp)
if self.pair.ssl and self.pair.ssl:getError() then
+ p('error function')
self.pair:err()
return
end
@@ -421,9 +423,9 @@ function CleartextStream:_pusher()
return self.pair.ssl:clearOut()
end
-function CleartextStream:close()
+function CleartextStream:destroy()
if self.socket and self._closing ~= true then
- self.socket:close()
+ self.socket:destroy()
end
self._closing = true
end
@@ -566,8 +568,8 @@ function SecurePair:maybeInitFinished()
end
end
-function SecurePair:close()
- dbg('SecurePair:close')
+function SecurePair:destroy()
+ dbg('SecurePair:destroy')
if self._doneFlag == true then
return
end
@@ -596,8 +598,8 @@ function SecurePair:err()
err = Error:new('socket hang up')
err.code = 'ECONNRESET'
end
- self:close()
self:emit('error', err)
+ self:destroy()
else
local err = self.ssl:getError()
self.ssl:clearError()
@@ -687,7 +689,7 @@ function Server:initialize(...)
if verifyError then
pair.cleartext.authorizationError = verifyError
if self.rejectUnauthorized == true then
- pair:close()
+ pair:destroy()
else
self:emit('secureConnection', cleartext, pair.encrypted)
end
@@ -705,6 +707,7 @@ function Server:initialize(...)
if listener then
self:on('secureConnection', listener)
+ self:on('secureConnection', function() p('secureConnection') end)
end
end
@@ -833,7 +836,7 @@ function connect(...)
cleartext.authorizationError = verifyError
if pair._rejectUnauthorized == true then
cleartext:emit('error', verifyError)
- pair:close()
+ pair:destroy()
else
cleartext:emit('secureConnect')
end
View
7 tests/test-tls-junk-closes-server.lua
@@ -25,18 +25,19 @@ local options = {
local server
server = tls.createServer(options, function(s)
- s:write('welcome!\n')
- s:pipe(s)
+ assert(false)
end)
server:listen(fixture.commonPort, function()
local c
c = net.createConnection({port = fixture.commonPort, host = '127.0.0.1'})
c:on('connect', function()
+ p('connect')
c:write('blah\nblah\nblah\n')
end)
c:on('end', function()
- c:close()
+ p('end')
+ c:destroy()
server:close()
end)
end)
View
4 tests/test-tls-peer-certificate.lua
@@ -27,7 +27,7 @@ local verified = false
local server
server = tls.createServer(options, function(cleartext)
cleartext:write('world')
- cleartext:close()
+ cleartext:destroy()
end)
server:listen(fixture.commonPort, function()
@@ -37,7 +37,7 @@ server:listen(fixture.commonPort, function()
assert(peercert.subject.subjectAltName == 'uniformResourceIdentifier:http://localhost:8000/alice.foaf#me')
verified = true
socket:write('Hello')
- socket:close()
+ socket:destroy()
server:close()
end)
end)
View
4 tests/test-tls-remote.lua
@@ -28,7 +28,7 @@ local server = tls.createServer(options, function(s)
assert(s:address().port == s.socket:address().port)
assert(s.remoteAddress == s.socket.remoteAddress)
assert(s.remotePort == s.socket.remotePort)
- s:close()
+ s:destroy()
end)
server:listen(fixture.commonPort, '127.0.0.1', function()
@@ -40,7 +40,7 @@ server:listen(fixture.commonPort, '127.0.0.1', function()
c:on('end', function()
assert(c:address().address == c.socket:address().address)
assert(c:address().port == c.socket:address().port)
- c:close()
+ c:destroy()
server:close()
end)
end)
View
4 tests/test-tls-request-timeout.lua
@@ -27,7 +27,7 @@ local server
server = tls.createServer(options, function(socket)
socket:on('timeout', function(err)
hadTimeout = true
- socket:close()
+ socket:destroy()
server:close()
end)
socket:setTimeout(100)
@@ -36,7 +36,7 @@ end)
server:listen(fixture.commonPort, function()
local socket = tls.connect({host = '127.0.0.1', port = fixture.commonPort})
socket:on('end', function()
- socket:close()
+ socket:destroy()
assert(hadTimeout == true)
end)
end)
View
2  tests/test-tls-server-verify.lua
@@ -222,7 +222,7 @@ function runTest(testIndex, done)
end
c:on('data', function(chunk)
if chunk:find('goodbye') then
- c:close()
+ c:destroy()
end
end)
end)
View
2  tests/test-tls-session-cache.lua
@@ -34,7 +34,7 @@ local requestCount = 0
local server
server = tls.createServer(options, function(cleartext)
requestCount = requestCount + 1
- cleartext.socket:close()
+ cleartext.socket:destroy()
end)
function clientConnect(callback)
View
2  tests/test-tls-sni-server-client.lua
@@ -74,7 +74,7 @@ function connectClient(options, callback)
local client
client = tls.connect(options, function()
table.insert(clientResults, client.authorized)
- client:close()
+ client:destroy()
callback()
end)
end
Please sign in to comment.
Something went wrong with that request. Please try again.