Skip to content

Commit

Permalink
Some initial tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Mar 29, 2024
1 parent 9ab4032 commit 51f6cb1
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
84 changes: 84 additions & 0 deletions fixtures/async/http/a_graceful_stop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2023, by Samuel Williams.
# Copyright, 2020, by Igor Sidorov.

require 'async'
require 'async/http/client'
require 'async/http/server'
require 'async/http/endpoint'
require 'async/http/body/hijack'
require 'tempfile'

module Async
module HTTP
AGracefulStop = Sus::Shared("a graceful stop") do
include Sus::Fixtures::Async::HTTP::ServerContext

let(:events) {Async::Queue.new}

with 'a streaming server (defered stop body)' do
let(:app) do
::Protocol::HTTP::Middleware.for do |request|
body = ::Async::HTTP::Body::Writable.new

Async do |task|
task.defer_stop do
10.times do
body.write("Hello, World!\n")
task.sleep(0.1)
end
end
rescue => error
events.enqueue(error)
ensure
body.close(error)
end

::Protocol::HTTP::Response[200, {}, body]
end
end

it "should stop gracefully" do
response = client.get("/")
expect(response).to be(:success?)

@server_task.stop

expect(response.read).to be == "Hello, World!\n" * 10
end
end

with 'a streaming server' do
let(:app) do
::Protocol::HTTP::Middleware.for do |request|
body = ::Async::HTTP::Body::Writable.new

Async do |task|
10.times do
body.write("Hello, World!\n")
task.sleep(0.1)
end
rescue => error
events.enqueue(error)
ensure
body.close(error)
end

::Protocol::HTTP::Response[200, {}, body]
end
end

it "should stop gracefully" do
response = client.get("/")
expect(response).to be(:success?)

@server_task.stop

inform(response.read)
end
end
end
end
end
3 changes: 2 additions & 1 deletion lib/async/http/protocol/http1/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def each(task: Task.current)
return
end

task.defer_stop do
# task.defer_stop do
begin
# If a response was generated, send it:
if response
trailer = response.headers.trailer!
Expand Down
2 changes: 2 additions & 0 deletions test/async/http/protocol/http10.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

require 'async/http/protocol/http10'
require 'async/http/a_protocol'
require 'async/http/a_graceful_stop'

describe Async::HTTP::Protocol::HTTP10 do
it_behaves_like Async::HTTP::AProtocol
it_behaves_like Async::HTTP::AGracefulStop
end

0 comments on commit 51f6cb1

Please sign in to comment.