Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test that data is not lost when on socket close. #1714

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions tests/close_socket_test.toit
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (C) 2023 Toitware ApS.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the tests/LICENSE file.

import expect show *
import net

// Test that written data is not lost when a socket is closed normally.

main:
network := net.open
port := start_server network
run_client network port

port2 := start_server network
run_client network port2 --keep_writing

run_client network port/int --keep_writing=false -> none:
socket := network.tcp_connect "127.0.0.1" port
socket.write "Hello, World!"
socket.no_delay = true
if keep_writing:
sleep --ms=1000
print
socket.read
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why break the line here?

print "Writing more"
socket.write "Hello, again!"
socket.close

start_server network -> int:
server_socket := network.tcp_listen 0
port := server_socket.local_address.port
task::
while socket := server_socket.accept:
print "Got connection"
message := #[]
while message.size < 13:
data := socket.read
if not data: break
message += data
print "Got message size $message.size"
expect_equals 13 message.size
socket.close
return port