Remove Context#app to reduce GC load (experimental)

This commit is contained in:
Johannes Müller 2017-10-18 17:53:44 +02:00 committed by sdogruyol
parent 72bcac6dd5
commit 5917af3f14
5 changed files with 8 additions and 15 deletions

View file

@ -1,10 +1,9 @@
require "./spec_helper"
private def create_ws_request_and_return_io(handler, request, app = Kemal.application)
private def create_ws_request_and_return_io(handler, request)
io = IO::Memory.new
response = HTTP::Server::Response.new(io)
context = HTTP::Server::Context.new(request, response)
context.app = app
begin
handler.call context
rescue IO::Error
@ -36,7 +35,6 @@ describe "Kemal::WebSocketHandler" do
it "matches on given route" do
app = Kemal::Base.new
handler = app.websocket_handler
app.ws "/" { |socket, context| socket.send("Match") }
app.ws "/no_match" { |socket, context| socket.send "No Match" }
headers = HTTP::Headers{
@ -47,13 +45,12 @@ describe "Kemal::WebSocketHandler" do
}
request = HTTP::Request.new("GET", "/", headers)
io_with_context = create_ws_request_and_return_io(handler, request, app)
io_with_context = create_ws_request_and_return_io(app.websocket_handler, request)
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n\x81\u0005Match")
end
it "fetches named url parameters" do
app = Kemal::Base.new
handler = app.websocket_handler
app.ws "/:id" { |s, c| c.params.url["id"] }
headers = HTTP::Headers{
"Upgrade" => "websocket",
@ -62,7 +59,7 @@ describe "Kemal::WebSocketHandler" do
"Sec-WebSocket-Version" => "13",
}
request = HTTP::Request.new("GET", "/1234", headers)
io_with_context = create_ws_request_and_return_io(handler, request, app)
io_with_context = create_ws_request_and_return_io(app.websocket_handler, request)
io_with_context.to_s.should eq("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-Websocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n")
end