Skip to content

Commit

Permalink
Fix broken server tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jul 16, 2023
1 parent beba627 commit d7a147d
Showing 1 changed file with 66 additions and 64 deletions.
130 changes: 66 additions & 64 deletions tests/server/test_server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,51 +26,56 @@ end
-- Server runs the event loop
function tests.test_server_runs()
local server = Server()
local luv = require "luv"
stub(luv, "run").returns(false)

local has_active_handles = server:run()

assert.is_false(has_active_handles)
luv.run:revert()
end

-- Server creates a TCP server
function tests.test_makes_tcp_server()
local server = Server()
local luv = require "luv"
stub(luv, "new_tcp").returns({}, nil)

local status = server:_make_tcp_server()

assert.equal(0, status)
luv.new_tcp:revert()
end

-- Server sets up the TCP server
function tests.test_tcp_server_set_up()
local server = Server()
-- TODO: figure out what's going wrong with stubs
-- stub(server, "_make_tcp_server").returns(0)
-- server._server = build_mock_server()
-- local config = {host = "127.0.0.1", port = 5555}
--
-- local status = server:set_up(config)
--
-- assert.equal(0, status)
-- loop.close()
stub(server, "_make_tcp_server").returns(0)
stub(server, "_set_sigint").returns(0)
server._server = build_mock_server()
local config = {host = "127.0.0.1", port = 5555}

local status = server:set_up(config)

assert.equal(0, status)
loop.close()
end

-- Server listens via the callback
function tests.test_server_listens()
local luv = require "luv"
local err = nil
-- TODO: figure out what's going wrong with stubs
-- local server = Server()
-- server._server = build_mock_server()
-- local mock_client = {read_start = function() end}
-- stub(mock_client, "read_start")
-- stub(luv, "new_tcp").returns(mock_client)
--
-- local callback = server:_make_listen_callback()
-- callback(err)
--
-- assert.stub(mock_client.read_start).called()
-- luv.new_tcp:revert()
local server = Server()
server._server = build_mock_server()
local mock_client = {read_start = function() end}
stub(mock_client, "read_start")
stub(luv, "new_tcp").returns(mock_client)

local callback = server:_make_listen_callback()
callback(err)

assert.stub(mock_client.read_start).called()
luv.new_tcp:revert()
end

-- Server exits on sigint
Expand All @@ -86,69 +91,66 @@ end
-- Server fails on a signal creation error
function tests.test_signal_creation_error()
local luv = require "luv"
-- TODO: figure out what's going wrong with stubs
-- stub(luv, "new_signal").returns(nil, 1)
-- local server = Server()
--
-- local status
-- loop.run_until_done(function() status = server:_set_sigint() end)
--
-- assert.equal(1, status)
stub(luv, "new_signal").returns(nil, 1)
local server = Server()

local status
loop.run_until_done(function() status = server:_set_sigint() end)

assert.equal(1, status)
luv.new_signal:revert()
end

-- Server fails on TCP creation error
function tests.test_tcp_creation_error()
local luv = require "luv"
-- TODO: figure out what's going wrong with stubs
-- stub(luv, "new_tcp").returns(nil, 1)
-- local server = Server()
--
-- local status
-- loop.run_until_done(function() status = server:_make_tcp_server() end)
--
-- assert.equal(1, status)
stub(luv, "new_tcp").returns(nil, 1)
local server = Server()

local status
loop.run_until_done(function() status = server:_make_tcp_server() end)

assert.equal(1, status)
luv.new_tcp:revert()
end

-- Server fails on bind error
function tests.test_bind_error()
local server = Server()
-- TODO: figure out what's going wrong with stubs
-- server._server = build_mock_server()
-- stub(server._server, "bind").returns(42, nil)
--
-- local status
-- loop.run_until_done(function() status = server:_bind("127.0.0.1", 5000) end)
--
-- assert.equal(42, status)
server._server = build_mock_server()
stub(server._server, "bind").returns(42, nil)

local status
loop.run_until_done(function() status = server:_bind("127.0.0.1", 5000) end)

assert.equal(42, status)
end

-- Server fails on listen error
function tests.test_listen_error()
local server = Server()
-- TODO: figure out what's going wrong with stubs
-- server._server = build_mock_server()
-- stub(server._server, "listen").returns(42, nil)
--
-- local status
-- loop.run_until_done(function() status = server:_listen() end)
--
-- assert.equal(42, status)
server._server = build_mock_server()
stub(server._server, "listen").returns(42, nil)

local status
loop.run_until_done(function() status = server:_listen() end)

assert.equal(42, status)
end

-- main runs
function tests.test_main_runs()
local server = Server()
-- TODO: figure out what's going wrong with stubs
-- stub(server, "_make_tcp_server").returns(0)
-- stub(server, "run").returns(false)
-- server._server = build_mock_server()
-- local config = {host = "127.0.0.1", port = 5555}
-- local app = {}
--
-- local status = main.run(config, server, app)
--
-- assert.equal(1, status)
-- loop.close()
stub(server, "set_up").returns(0)
stub(server, "run").returns(false)
server._server = build_mock_server()
local config = {host = "127.0.0.1", port = 5555}
local app = {}

local status = main.run(config, server, app)

assert.equal(0, status)
loop.close()
end

-- main fails on server setup failure
Expand Down

0 comments on commit d7a147d

Please sign in to comment.