1
ネットワーキングアプリケーションをテストしようとしています。しかし、私は脳に結びついているかもしれません。 私が知っている限り、最小限のテストは並行して走ります。MinitestのTCPSocketにいくつかのテストを書く方法
RuntimeError: Address already in use - bind(2) for nil port 2000 TCPServer new failed
がそうでリッスンサーバー上のいくつかのテストを行うためのベストプラクティスです:この仮定に私は、セットアップ中にポートを(割り当てるとき、それはそれはいくつかのテストが実行されたときに失敗した)ことは明らかだと思いますポート?
class ServerTest < Minitest::Test
def setup
# -- set env variable
ENV["conf"] = "./tc_gpio.config"
Thread::abort_on_exception = true
@my_thr = Thread.start() do
@server = Server.new
@server.start
assert @server.hi() != nil
end
end
def teardown
Thread.kill(@my_thr) # sends exit() to thr
end
def test_connect_and_disconnect
sleep 1
hostname = 'localhost'
port = 2000
s = TCPSocket.open(hostname, port)
my_s = s.recvmsg()
s.sendmsg(:set.to_s, 0) # Failes since a serialized object is expected
my_s2 = s.recvmsg()
assert_equal( "READY" , my_s[0])
assert_equal("eeFIN" , my_s2[0])
end
def test_send_command
# fill command
com = Command.new
com.type = :set
com.device_name = 'pump0'
com.device_address = 'resource0'
com.value = 2
serial_com = YAML::dump(com)
sleep 1
hostname = 'localhost'
port = 2000
s = TCPSocket.open(hostname, port)
my_s = s.recvmsg()
s.sendmsg(serial_com, 0)
my_s2 = s.recvmsg()
assert_equal( "READY" , my_s[0])
assert_equal("FIN" , my_s2[0])
end
end