gpt4 book ai didi

简单套接字服务器的 Ruby 单元测试

转载 作者:数据小太阳 更新时间:2023-10-29 08:57:17 25 4
gpt4 key购买 nike

我有一个简单的 Socket 服务器,它监听端口 9000 并将反向字符串返回给客户端。

require 'socket'

# Create the server using the Socket class
server_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
# Set the socket addres
sockaddr = Socket.pack_sockaddr_in(9000, 'localhost')

# Bind the socket address to the server socket
server_socket.bind(sockaddr)

# set the state to listen
server_socket.listen(5)
loop do
client, client_sockaddr = server_socket.accept
client.puts 'Connected to the server! Press q to exit'

loop do
# Receive data using recvfrom method on the new client socket
data = client.recvfrom(1024)[0].chomp

if data == 'q'
client.close
break
else
# Reverse the string
respond = data.reverse

# Send the respond
client.puts "Your string in reverse is: #{respond}"
end
end
end

现在我想为这个服务器编写单元测试来证明响应的正确性。我不知道如何开始。查看了 Rspec 测试和内置的 Test::Unit 模块,但无法弄清楚。我知道我可以使用 RSpec Mocks 或类似的东西,但我什至不确定如何将我的测试文件放在哪里以及如何测试我的代码。

最佳答案

I am not even sure where to put my test file and how to test my code.

你可以把你的测试文件放在任何你想放的地方;您只需要在 rpsec 文件的顶部包含您的服务器(和客户端)的路径。对于以下示例,我将所有文件放在同一目录中。

$ tree sockets
sockets
├── my_client.rb
├── my_server.rb
├── my_specs.rb
└── prog.rb #<==creates a client and server to see if they work

我的客户端.rb:

require 'socket'

class MyClient
attr_reader :socket

def initialize
@socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
@socket.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)
@socket.connect Socket.pack_sockaddr_in(13_500, '127.0.0.1')
end

def send_data(data)
@socket.puts data
end

def recv_data
@socket.gets
end

end

我的服务器.rb:

require 'socket'

class MyServer
attr_reader :server_socket, :client

def initialize
@server_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
@server_socket.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR, true)

sockaddr = Socket.pack_sockaddr_in(13_500, '127.0.0.1')
@server_socket.bind(sockaddr)
@server_socket.listen(5)
end

def start
#Blocks and waits for client connections:
@client, client_sockaddr = @server_socket.accept
@client.puts "Connected to the server! Press q to exit"

loop do # loop() automatically catches a StopIteration exception and terminates
data = @client.recvfrom(1024)[0].chomp
handle_data(data)
end
end

def handle_data data
case data
when 'q', ''
raise StopIteration #terminates loop()
else
if @client #For some reason @client can be nil here
#which causes a nil.puts error two lines down.
response = data.reverse
@client.puts "Your string in reverse is: #{response}"
end
end

response
end


end

程序.rb:

require_relative 'my_server.rb'
require_relative 'my_client.rb'

server = MyServer.new

Thread.new do
server.start
end

sleep 1 #Otherwise, get connection refused error because client sends
#data to server before server creates the client socket

client = MyClient.new
greeting = client.recv_data
puts greeting

client.send_data("hello")
response = client.recv_data
puts response

client.socket.close
server.server_socket.close
server.client.close

我的规范.rb:

require_relative 'my_server'  #relative path to server
require_relative 'my_client' #relative path to client

describe MyServer do

before(:example) do
@server = MyServer.new

Thread.new do
@server.start
end

sleep 1 #Allow server to start, so client doesn't send data
#to the server before the server creates the socket.

@client = MyClient.new
@data = 'hello'
@client.send_data @data #Make sure server has started before doing this.
end

after(:example) do
@server.server_socket.close #Will send nil to client, causing gets() to unblock,
#allowing recv_data() to finish executing.
@server.client.close
@client.socket.close
end

describe '#handle_data' do
context 'given a string' do
it "returns reversed string" do
expect(@server.handle_data(@data)).to eql(@data.reverse)
end
end
end

describe '- client receives correct response' do
context 'given a string' do
it "returns reversed string" do
greeting = @client.recv_data
expect(@client.recv_data).to eql("Your string in reverse is: #{@data.reverse}\n")
end
end
end

end

运行规范:

~/ruby_programs/sockets$ rspec my_specs.rb
..

Finished in 2.01 seconds (files took 0.11932 seconds to load)
2 examples, 0 failures

关于简单套接字服务器的 Ruby 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48780973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com