Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

Commit

Permalink
Improve echo spec.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Oct 29, 2023
1 parent 8f91a7b commit 46513be
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions spec/async/io/echo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,36 @@
# Copyright, 2020, by Benoit Daloze.

require 'async/io'
require 'async/io/address'

RSpec.describe "echo client/server" do
include_context Async::RSpec::Reactor

let(:server_address) {Async::IO::Address.tcp('0.0.0.0', 9002)}

def echo_server(server_address)
Async do |task|
# This is a synchronous block within the current task:
Async::IO::Socket.accept(server_address) do |client|
server = server_address.bind
server.listen(10)

Async do
while true
peer, address = server.accept
# This is an asynchronous block within the current reactor:
data = client.read(512)
data = peer.read(512)

# This produces out-of-order responses.
task.sleep(rand * 0.01)
sleep(rand * 0.01)

client.write(data)
peer.write(data)
end
ensure
server.close
end
end

def echo_client(server_address, data, responses)
Async do |task|
Async::IO::Socket.connect(server_address) do |peer|
server_address.connect do |peer|
result = peer.write(data)
peer.close_write

Expand All @@ -49,7 +55,7 @@ def echo_client(server_address, data, responses)
echo_client(server_address, "Hello World #{i}", responses)
end

# task.reactor.print_hierarchy
# reactor.print_hierarchy

tasks.each(&:wait)
server.stop
Expand Down

0 comments on commit 46513be

Please sign in to comment.