I've written a really simple date server:
require 'socket'
s = TCPServer.new 3939
while (conn = s.accept)
Thread.new(conn) do |c|
c.print "Enter your name: "
name = c.gets.chomp
c.puts "Hi #{name}, the date is..."
c.print `date`
c.close
end
end
A user connects, a thread is spawned, they enter their name, the date is returned. Simple.
I'm wondering about how I would go abouts testing something like this in rspec. Some ideas that I've had: 1.) Use VCR to record a server connection and Timecop to freeze and return a date. 2.) Connect to the actual server in a before block. I'm not entirely sure how to do this as when I run rspec, I think it actually runs the server...or something happens that the terminal just kind of freezes and waits for something... example test code:
before
@server = TCPSever.new 3939
end
it "does something.."
conn = @server.accept
# etc
end
after
@server.close
end
3.) Stub the connection. 4.) Don't even try to test that threads are created, and just put the name request and date response into a method and test this.
I looked at puma's tests to see how they test threads: https://github.com/puma/puma/blob/master/test/test_puma_server.rb#L27
I'd really appreciate some help with this. It's just a simple exercise where I'd like to see how best to implement some tests for this kind of server. Like, simulating a user connecting and entering their name. Thank you in advance.