Skip to content

Instantly share code, notes, and snippets.

@rphillips
Forked from pquerna/test.lua
Created June 21, 2012 04:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rphillips/b8f5375c5a2559635eef to your computer and use it in GitHub Desktop.
Save rphillips/b8f5375c5a2559635eef to your computer and use it in GitHub Desktop.
require('helper')
local fixture = require('./fixture-tls')
local tls = require('tls')
local timer = require('timer')
local Buffer = require('buffer').Buffer
local options = {
cert = fixture.certPem,
key = fixture.keyPem
}
local serverConnected = 0
local clientConnected = 0
local clientSent = 0
local clientBuffer = ""
local serverSent = 0
local serverBuffer = ""
local server
function fib(n)
return n<2 and n or fib(n-1)+fib(n-2)
end
function fill(buf, char)
local i = 1
while i < buf.length do
buf[i] = char
i = i + 1
end
end
function writeFibBuffer(isClient, conn, fibCount, callback)
timer.setTimeout(fibCount, function()
if (fibCount == 0) then
callback()
return
end
local buf = Buffer:new(fib(fibCount))
fill(buf, 0xA)
conn:write(buf)
if isClient == true then
clientSent = clientSent + buf.length
else
serverSent = serverSent + buf.length
end
writeFibBuffer(isClient, conn, fibCount - 1, callback)
end)
end
server = tls.createServer(options, function(conn)
serverConnected = serverConnected + 1
conn:on('data', function(chunk)
p('got server data callback')
serverBuffer = serverBuffer .. chunk
end)
writeFibBuffer(false, conn, 20, function()
p('closing server')
server:close()
end)
end)
server:listen(fixture.commonPort, function()
local client
client = tls.connect({port = fixture.commonPort, host = '127.0.0.1'}, {}, function()
clientConnected = clientConnected + 1
client:on('data', function(chunk)
p('got client data callback')
clientBuffer = clientBuffer .. chunk
end)
writeFibBuffer(true, client, 22, function()
p('closing client')
client:destroy()
end)
end)
end)
process:on('exit', function()
assert(serverConnected == 1)
assert(clientConnected == 1)
p(#clientBuffer)
p(serverSent)
assert(#clientBuffer == serverSent)
assert(#serverBuffer == clientSent)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment