Skip to content

Instantly share code, notes, and snippets.

@jamesdavidson
Created January 20, 2015 09:26
Show Gist options
  • Save jamesdavidson/6c88ba43def1c0453446 to your computer and use it in GitHub Desktop.
Save jamesdavidson/6c88ba43def1c0453446 to your computer and use it in GitHub Desktop.
Some test cases for a TCP chat server
require 'open3'
IP_ADDRESS = '192.168.1.103'
PORT = 1337
WELCOME_MSG_LENGTH = 'Welcome to chat server'.length + 1
TEST_MSG = 'hello'
RECVD_MSG = 'Message Sent'
def assert(thing,msg)
unless thing
raise "DANG! This didn't happen - #{msg}"
end
end
cmd = "nc #{IP_ADDRESS} #{PORT}"
sally_input, sally_output, _, sally_thread = Open3.popen3(cmd)
alice_input, alice_output, _, alice_thread = Open3.popen3(cmd)
david_input, david_output, _, david_thread = Open3.popen3(cmd)
# pull the welcome message out of each pipe
[sally_output,alice_output,david_output].each{ |f| f.read(WELCOME_MSG_LENGTH) }
unless sally_thread.status == "sleep"
raise "Connect failed. Check the server is listening #{IP_ADDRESS}:#{PORT}"
end
# send a message, check that is received only by the others
sally_input.write(TEST_MSG)
assert(sally_output.read(RECVD_MSG.length) == RECVD_MSG, 'sally did not got the message')
assert(david_output.read(TEST_MSG.length) == TEST_MSG, 'david got the message')
assert(alice_output.read(TEST_MSG.length) == TEST_MSG, 'alice got the message')
sally_output.read(1)
david_input.write(TEST_MSG)
assert(david_output.read(RECVD_MSG.length) == RECVD_MSG, 'david did not got the message')
assert(sally_output.read(TEST_MSG.length) == TEST_MSG, 'sally got the message')
assert(alice_output.read(TEST_MSG.length) == TEST_MSG, 'alice got the message')
david_output.read(1)
alice_input.write(TEST_MSG)
assert(alice_output.read(RECVD_MSG.length) == RECVD_MSG, 'alice did not got the message')
assert(david_output.read(TEST_MSG.length) == TEST_MSG, 'david got the message')
assert(sally_output.read(TEST_MSG.length) == TEST_MSG, 'sally got the message')
alice_output.read(1)
sally_thread.kill
alice_input.write(TEST_MSG)
assert(alice_output.read(RECVD_MSG.length) == RECVD_MSG, 'alice did not got the message')
assert(david_output.read(TEST_MSG.length) == TEST_MSG, 'david got the message')
alice_output.read(1)
david_thread.kill
alice_input.write(TEST_MSG)
assert(alice_output.read(RECVD_MSG.length) == RECVD_MSG, 'alice did not got the message')
alice_output.read(1)
mary_input, mary_output, _, mary_thread = Open3.popen3(cmd)
mary_output.read(WELCOME_MSG_LENGTH)
mary_input.write(TEST_MSG)
assert(mary_output.read(RECVD_MSG.length) == RECVD_MSG, 'mary did not got the message')
assert(alice_output.read(TEST_MSG.length) == TEST_MSG, 'alice got the message')
mary_output.read(1)
alice_input.write(TEST_MSG)
assert(alice_output.read(RECVD_MSG.length) == RECVD_MSG, 'alice did not got the message')
assert(mary_output.read(TEST_MSG.length) == TEST_MSG, 'mary got the message')
alice_output.read(1)
john_input, john_output, _, john_thread = Open3.popen3(cmd)
john_output.read(WELCOME_MSG_LENGTH)
alice_input.write(TEST_MSG)
assert(alice_output.read(RECVD_MSG.length) == RECVD_MSG, 'alice did not got the message')
assert(mary_output.read(TEST_MSG.length) == TEST_MSG, 'mary got the message')
assert(john_output.read(TEST_MSG.length) == TEST_MSG, 'john got the message')
alice_output.read(1)
alice_thread.kill
john_input.write(TEST_MSG)
assert(john_output.read(RECVD_MSG.length) == RECVD_MSG, 'john did not got the message')
assert(mary_output.read(TEST_MSG.length) == TEST_MSG, 'mary got the message')
john_output.read(1)
[john_thread,mary_thread].each(&:kill)
puts 'tests passed'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment