aboutsummaryrefslogtreecommitdiff
path: root/tests/socket.test
diff options
context:
space:
mode:
authorSteve Bennett <steveb@workware.net.au>2023-05-28 11:22:12 +1000
committerSteve Bennett <steveb@workware.net.au>2023-07-04 09:23:43 +1000
commit41f431f30cc6118ef982c6374914810cd07a8106 (patch)
tree036384d2c7e90a0236642ebf65686601c92656d5 /tests/socket.test
parentad720049ec1ae3536d64fbb4c80a79e65ba5af39 (diff)
downloadjimtcl-41f431f30cc6118ef982c6374914810cd07a8106.zip
jimtcl-41f431f30cc6118ef982c6374914810cd07a8106.tar.gz
jimtcl-41f431f30cc6118ef982c6374914810cd07a8106.tar.bz2
aio: change to use unix io, not stdio
This changes especially makes buffered I/O work with non-blocking channels. - separate read and write buffering - support for timeout on blocking read - read/write on same channel in event loop with buffering - read buffer is the same across read, gets, copyto - autoflush non-blocking writes via event loop - copyto can now copy to any filehandle-like command - add some copyto tests Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'tests/socket.test')
-rw-r--r--tests/socket.test77
1 files changed, 66 insertions, 11 deletions
diff --git a/tests/socket.test b/tests/socket.test
index 67fdb9c..1eb98b4 100644
--- a/tests/socket.test
+++ b/tests/socket.test
@@ -129,6 +129,8 @@ test socket-1.6 {pipe} -body {
test socket-1.7 {socketpair} -body {
lassign [socket pair] s1 s2
+ $s1 buffering line
+ $s2 buffering line
stdout flush
if {[os.fork] == 0} {
$s1 close
@@ -338,20 +340,18 @@ set s [socket stream.server 0]
if {[os.fork] == 0} {
# child
set c [socket stream [socket-connect-addr $s]]
- # Note: We have to disable buffering here, otherwise
- # when we read data in $c readable {} we many leave buffered
- # data and readable won't retrigger.
- $c buffering none
$s close
+ $c ndelay 1
$c readable {
- # when we read we need to also read any pending data,
- # otherwise readable won't retrigger
- set buf [$c read 1]
- if {[string length $buf] == 0} {
+ # read everything available (non-blocking read)
+ set buf [$c read]
+ if {[string length $buf]} {
+ $c puts -nonewline $buf
+ $c flush
+ }
+ if {[$c eof]} {
incr readdone
$c close
- } else {
- $c puts -nonewline $buf
}
}
vwait readdone
@@ -365,6 +365,8 @@ defer {
}
$s close
+$cs buffering line
+
# At this point, $cs is the server connection to the client in the child process
test eventloop-1.1 {puts/gets} {
@@ -372,14 +374,67 @@ test eventloop-1.1 {puts/gets} {
$cs gets
} hello
-test eventloop-1.2 {puts/gets} {
+test eventloop-1.2 {puts/read} {
$cs puts -nonewline again
+ $cs flush
lmap p [range 5] {
set c [$cs read 1]
set c
}
} {a g a i n}
+test eventloop-1.3 {gets with no timeout and multiple newlines} {
+ $cs puts a\nb\nc\nd\ne
+ lmap p [range 5] {
+ $cs gets buf
+ set buf
+ }
+} {a b c d e}
+
+test eventloop-1.4 {gets with timeout and multiple newlines} {
+ $cs timeout 100
+ $cs puts a\nb\nc\nd\ne
+ lmap p [range 6] {
+ set rc [$cs gets buf]
+ set buf
+ }
+} {a b c d e {}}
+
+test eventloop-1.5 {gets with timeout and incomplete line} {
+ $cs timeout 100
+ $cs puts -nonewline first
+ list [$cs gets buf] $buf
+} {-1 {}}
+
+test eventloop-1.6 {gets with timeout and complete line} {
+ $cs timeout 100
+ $cs puts second
+ list [$cs gets buf] $buf
+} {11 firstsecond}
+
+test eventloop-1.7 {gets when read with extra data} {
+ $cs timeout 100
+ $cs puts -nonewline abcde
+ $cs flush
+ # This won't get get a line
+ $cs gets line
+ # now read should read the data
+ set data [$cs read -nonewline]
+ list $line $data
+} {{} abcde}
+
+test eventloop-1.7 {read with timeout and no data} {
+ $cs timeout 100
+ $cs read
+} {}
+
+test eventloop-1.6 {read with timeout and data} {
+ $cs timeout 100
+ $cs puts -nonewline data
+ $cs flush
+ $cs read
+} {data}
+
test sockopt-1.1 {sockopt} -body {
lsort [dict keys [$cs sockopt]]
} -match glob -result {*tcp_nodelay*}