Skip to content

Instantly share code, notes, and snippets.

@til
Created March 3, 2013 22:56
Show Gist options
  • Save til/5078707 to your computer and use it in GitHub Desktop.
Save til/5078707 to your computer and use it in GitHub Desktop.
require 'socket'
require 'minitest/autorun'
describe '0x8c' do
it 'works' do
pid = Process.fork do
C=[];require 'gserver';Class.new(GServer){def serve(i)h=p;C<<i;i.each_line{|l|C.map{|c|c<<(h||=l.strip)+'>'+l}};end}.new($$,'*').start.join #"
end
sleep 0.1
puts "connecting to #{pid}"
def read_all(io)
output = ''
while true do
output << io.read_nonblock(1)
end
rescue Errno::EAGAIN
ensure
return output
end
TCPSocket.open('localhost', pid) do |alice|
TCPSocket.open('localhost', pid) do |bob|
alice.puts 'alice'
sleep 0.1
alice.puts 'hello'
sleep 0.1
bob.puts 'bob'
sleep 0.1
bob.puts 'hello'
sleep 0.1
alice.puts 'hey bob'
sleep 0.1
bob.puts 'hey alice'
sleep 0.1
alice_log = read_all(alice)
bob_log = read_all(bob)
bob_log.must_match(/bob>/)
end
end
Process.kill 'KILL', pid
Process.wait
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment