Skip to content

Commit 8dcb5e6

Browse files
committed
Add tests for Buffered.open.
1 parent 2ee7cd4 commit 8dcb5e6

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

test/io/stream/buffered.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Copyright, 2024, by Samuel Williams.
55

66
require "io/stream/buffered"
7+
require "tempfile"
78

89
require "sus/fixtures/async/reactor_context"
910
require "sus/fixtures/openssl/verified_certificate_context"
@@ -22,6 +23,89 @@
2223
expect(IO::Stream::MAXIMUM_READ_SIZE).to be_within(1024*64..1024*64*512)
2324
end
2425
end
26+
27+
with ".open" do
28+
let(:tempfile) {Tempfile.new("io_stream_buffered_test")}
29+
let(:test_file_path) {tempfile.path}
30+
31+
after do
32+
tempfile.close
33+
tempfile.unlink
34+
end
35+
36+
it "can open a file and return a buffered stream" do
37+
stream = IO::Stream::Buffered.open(test_file_path, "w+")
38+
39+
expect(stream).to be_a(IO::Stream::Buffered)
40+
expect(stream.io).to be_a(File)
41+
42+
stream.write("Hello, World!")
43+
stream.flush
44+
stream.close
45+
46+
content = File.read(test_file_path)
47+
expect(content).to be == "Hello, World!"
48+
end
49+
50+
it "can open a file with a block and auto-close" do
51+
result = nil
52+
stream_reference = nil
53+
54+
# Create a separate tempfile for this test to avoid conflicts
55+
block_tempfile = Tempfile.new("io_stream_buffered_block_test")
56+
block_file_path = block_tempfile.path
57+
block_tempfile.close
58+
59+
begin
60+
result = IO::Stream::Buffered.open(block_file_path, "w+") do |stream|
61+
stream_reference = stream
62+
63+
expect(stream).to be_a(IO::Stream::Buffered)
64+
expect(stream.io).to be_a(File)
65+
66+
stream.write("Block test data")
67+
stream.flush
68+
69+
:block_result
70+
end
71+
72+
# The block's return value should be returned
73+
expect(result).to be == :block_result
74+
75+
# The stream should be automatically closed
76+
expect(stream_reference).to be(:closed?)
77+
78+
# The file should contain the written data
79+
content = File.read(block_file_path)
80+
expect(content).to be == "Block test data"
81+
ensure
82+
File.unlink(block_file_path) if File.exist?(block_file_path)
83+
end
84+
expect(content).to be == "Block test data"
85+
end
86+
87+
it "ensures stream is closed even when block raises an exception" do
88+
stream_reference = nil
89+
90+
expect do
91+
IO::Stream::Buffered.open(test_file_path, "w+") do |stream|
92+
stream_reference = stream
93+
94+
stream.write("Exception test")
95+
stream.flush
96+
97+
raise StandardError, "Test exception"
98+
end
99+
end.to raise_exception(StandardError, message: be == "Test exception")
100+
101+
# The stream should be closed despite the exception
102+
expect(stream_reference).to be(:closed?)
103+
104+
# The file should still contain the written data
105+
content = File.read(test_file_path)
106+
expect(content).to be == "Exception test"
107+
end
108+
end
25109
end
26110

27111
AUnidirectionalStream = Sus::Shared("a unidirectional stream") do

0 commit comments

Comments
 (0)