1
我嘗試測試一個網絡應用程序。但是我可能會在腦中產生一個結。 據我所知,minitest中的測試並行運行。在此假設,我認爲很明顯的是,當我分配在設置一個端口()失敗時,幾個測試運行:如何在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