|
4 | 4 | # Copyright, 2024, by Samuel Williams. |
5 | 5 |
|
6 | 6 | require "io/stream/buffered" |
| 7 | +require "tempfile" |
7 | 8 |
|
8 | 9 | require "sus/fixtures/async/reactor_context" |
9 | 10 | require "sus/fixtures/openssl/verified_certificate_context" |
|
22 | 23 | expect(IO::Stream::MAXIMUM_READ_SIZE).to be_within(1024*64..1024*64*512) |
23 | 24 | end |
24 | 25 | 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 |
25 | 109 | end |
26 | 110 |
|
27 | 111 | AUnidirectionalStream = Sus::Shared("a unidirectional stream") do |
|
0 commit comments