summaryrefslogtreecommitdiffstats
path: root/tests/018
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-08-07 12:11:54 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-08-07 12:11:54 -0700
commitb7d944377a6674e7fd8c6236f2b957a31c2eccde (patch)
tree352a588c225d44b0472875bbb28fd0585d17f04f /tests/018
parentfbcdc0c91be3700391a3f0294f87c26f2781944d (diff)
downloadtxr-b7d944377a6674e7fd8c6236f2b957a31c2eccde.tar.gz
txr-b7d944377a6674e7fd8c6236f2b957a31c2eccde.tar.bz2
txr-b7d944377a6674e7fd8c6236f2b957a31c2eccde.zip
streams: close-stream only caches non-nil result.
This is motivated by trying to implement a struct delegate stream which performs reference counting in close, in order to close the real stream when the count hits zero. The caching behavior of close-stream is a problem. * stream.c (strm_base_init): Initialize close_result to nil, rather than nao. (strm_base_mark): Don't check close_result for nao. (close_stream): Suppress the call to op->close if close_result has a non-nil value, rather than a value other than nao. * tests/018/close-delegate.tl, * tests/018/close-delegate.expected: New files. * txr.1: Document that only a non-nil return is cached by close-stream.
Diffstat (limited to 'tests/018')
-rw-r--r--tests/018/close-delegate.expected3
-rw-r--r--tests/018/close-delegate.tl20
2 files changed, 23 insertions, 0 deletions
diff --git a/tests/018/close-delegate.expected b/tests/018/close-delegate.expected
new file mode 100644
index 00000000..227f9f67
--- /dev/null
+++ b/tests/018/close-delegate.expected
@@ -0,0 +1,3 @@
+close called, count 2
+close called, count 1
+20
diff --git a/tests/018/close-delegate.tl b/tests/018/close-delegate.tl
new file mode 100644
index 00000000..4cf1d650
--- /dev/null
+++ b/tests/018/close-delegate.tl
@@ -0,0 +1,20 @@
+(load "../common")
+
+(defstruct refcount-close stream-wrap
+ stream
+ (count 1)
+
+ (:method close (me throw-on-error-p)
+ (put-line `close called, count @{me.count}`)
+ (when (plusp me.count)
+ (if (zerop (dec me.count))
+ (close-stream me.stream throw-on-error-p)))))
+
+(flow
+ (with-stream (s (make-struct-delegate-stream
+ (new refcount-close
+ count 2
+ stream (open-file *load-path*))))
+ (get-lines s))
+ len
+ prinl)