spec/async/udp_socket_spec.rb in async-0.12.0 vs spec/async/udp_socket_spec.rb in async-0.13.0

- old
+ new

@@ -17,38 +17,53 @@ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. RSpec.describe Async::Reactor do + include_context "closes all io" + # Shared port for localhost network tests. - let(:port) {6778} + let(:server_address) {Addrinfo.udp("127.0.0.1", 6778)} + let(:data) {"The quick brown fox jumped over the lazy dog."} describe 'basic udp server' do - # These may block: - let(:server) {UDPSocket.new.tap{|socket| socket.bind("localhost", port)}} - let(:client) {UDPSocket.new} - - let(:data) {"The quick brown fox jumped over the lazy dog."} - - after(:each) do - server.close + it "should echo data back to peer" do + subject.async do + Async::Socket.bind(server_address) do |server| + packet, address = server.recvfrom(512) + + server.send(packet, 0, address) + end + end + + subject.async do + Async::Socket.connect(server_address) do |client| + client.send(data) + response = client.recv(512) + + expect(response).to be == data + end + end + + subject.run end - it "should echo data back to peer" do - subject.async(server) do |server, task| - packet, (_, remote_port, remote_host) = server.recvfrom(512) - - subject.async do - server.send(packet, 0, remote_host, remote_port) + it "should use unconnected socket" do + subject.async do + Async::Socket.bind(server_address) do |server| + packet, address = server.recvfrom(512) + + server.send(packet, 0, address) end end - subject.async(client) do |client| - client.send(data, 0, "localhost", port) - - response, _ = client.recvfrom(512) - - expect(response).to be == data + subject.async do |task| + task.with(UDPSocket.new(server_address.afamily)) do |client| + client.send(data, 0, server_address) + response, address = client.recvfrom(512) + + expect(response).to be == data + end end subject.run end end