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

时间:2018-02-14 06:29:36

标签: ruby unit-testing rspec mocking serversocket

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

for i in y
    c[i]=get(c,i,0)+1
    c= sort(collect(c),by=tuple -> last(tuple),rev=true)[1:3]
end


BoundsError: attempt to access 1-element Array{Pair{String,Int64},1} at index [1:3]

现在,我想为此服务器编写单元测试,以证明响应的正确性。我不知道如何开始。看看Rspec测试和内置的Test :: Unit模块,但无法弄明白。我知道我可以使用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 或类似的东西,但我甚至不知道如何放置我的测试文件以及如何测试我的代码。

1 个答案:

答案 0 :(得分:2)

  

我甚至不确定在哪里放置我的测试文件以及如何测试我的代码。

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

const

my_client.rb:

*iterator

my_server.rb:

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

prog.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

my_specs.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

运行规范:

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