-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add IO::Stapled * fixup! Add IO::Stapled * fixup! Add IO::Stapled
- 1.15.1
- 1.15.0
- 1.14.1
- 1.14.0
- 1.13.3
- 1.13.2
- 1.13.1
- 1.13.0
- 1.12.2
- 1.12.1
- 1.12.0
- 1.11.2
- 1.11.1
- 1.11.0
- 1.10.1
- 1.10.0
- 1.9.2
- 1.9.1
- 1.9.0
- 1.8.2
- 1.8.1
- 1.8.0
- 1.7.3
- 1.7.2
- 1.7.1
- 1.7.0
- 1.6.2
- 1.6.1
- 1.6.0
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- 0.36.1
- 0.36.0
- 0.35.1
- 0.35.0
- 0.34.0
- 0.33.0
- 0.32.1
- 0.32.0
- 0.31.1
- 0.31.0
- 0.30.1
- 0.30.0
- 0.29.0
- 0.28.0
- 0.27.2
- 0.27.1
- 0.27.0
- 0.26.1
- 0.26.0
- 0.25.1
- 0.25.0
1 parent
db0dc67
commit 981c343
Showing
2 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
require "spec" | ||
|
||
describe IO::Stapled do | ||
it "combines two IOs" do | ||
writer = IO::Memory.new | ||
io = IO::Stapled.new IO::Memory.new("paul"), writer | ||
io.gets.should eq "paul" | ||
io << "peter" | ||
writer.to_s.should eq "peter" | ||
end | ||
|
||
it "loops back" do | ||
io = IO::Stapled.new(*IO.pipe) | ||
io.puts "linus" | ||
io.gets.should eq "linus" | ||
end | ||
|
||
describe "#close" do | ||
it "does not close underlying IOs" do | ||
reader, writer = IO::Memory.new, IO::Memory.new | ||
io = IO::Stapled.new reader, writer | ||
io.sync_close?.should be_false | ||
io.close | ||
io.closed?.should be_true | ||
reader.closed?.should be_false | ||
writer.closed?.should be_false | ||
end | ||
|
||
it "closes underlying IOs when sync_close is true" do | ||
reader, writer = IO::Memory.new, IO::Memory.new | ||
io = IO::Stapled.new reader, writer, sync_close: true | ||
io.sync_close?.should be_true | ||
io.close | ||
io.closed?.should be_true | ||
reader.closed?.should be_true | ||
writer.closed?.should be_true | ||
end | ||
|
||
it "stops access to underlying IOs" do | ||
reader, writer = IO::Memory.new("cle"), IO::Memory.new | ||
io = IO::Stapled.new reader, writer | ||
io.close | ||
io.closed?.should be_true | ||
reader.closed?.should be_false | ||
writer.closed?.should be_false | ||
|
||
expect_raises(IO::Error, "Closed stream") do | ||
io.gets | ||
end | ||
expect_raises(IO::Error, "Closed stream") do | ||
io.peek | ||
end | ||
expect_raises(IO::Error, "Closed stream") do | ||
io << "closed" | ||
end | ||
end | ||
end | ||
|
||
it "#sync_close?" do | ||
reader, writer = IO::Memory.new, IO::Memory.new | ||
io = IO::Stapled.new reader, writer | ||
io.sync_close = false | ||
io.sync_close?.should be_false | ||
io.sync_close = true | ||
io.sync_close?.should be_true | ||
io.close | ||
reader.closed?.should be_true | ||
writer.closed?.should be_true | ||
end | ||
|
||
it "#peek delegates to reader" do | ||
reader = IO::Memory.new "cletus" | ||
io = IO::Stapled.new reader, IO::Memory.new | ||
io.peek.should eq "cletus".to_slice | ||
io.gets | ||
io.peek.should eq Bytes.empty | ||
end | ||
|
||
describe ".pipe" do | ||
it "creates a bidirectional pipe" do | ||
a, b = IO::Stapled.pipe | ||
begin | ||
a.sync_close?.should be_true | ||
b.sync_close?.should be_true | ||
a.puts "john" | ||
b.gets.should eq "john" | ||
b.puts "paul" | ||
a.gets.should eq "paul" | ||
ensure | ||
a.close | ||
b.close | ||
end | ||
end | ||
|
||
it "with block creates a bidirectional pipe" do | ||
ext_a, ext_b = nil, nil | ||
IO::Stapled.pipe do |a, b| | ||
ext_a, ext_b = a, b | ||
a.sync_close?.should be_true | ||
b.sync_close?.should be_true | ||
a.puts "john" | ||
b.gets.should eq "john" | ||
b.puts "paul" | ||
a.gets.should eq "paul" | ||
a.sync_close = false | ||
b.sync_close = false | ||
end | ||
ext_a.not_nil!.closed?.should be_true | ||
ext_b.not_nil!.closed?.should be_true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
# This class staples together two unidirectional `IO`s to form a single, | ||
# bidirectional `IO`. | ||
# | ||
# Example (loopback): | ||
# ``` | ||
# io = IO::Stapled.new(*IO.pipe) | ||
# io.puts "linus" | ||
# io.gets # => "linus" | ||
# ``` | ||
# | ||
# Most methods simply delegate to the underlying `IO`s. | ||
class IO::Stapled < IO | ||
# If `#sync_close?` is `true`, closing this `IO` will close the underlying `IO`s. | ||
property? sync_close : Bool | ||
|
||
# Returns `true` if this `IO` is closed. | ||
# | ||
# Underlying ÌO`s might have a different status. | ||
getter? closed : Bool = false | ||
|
||
# Creates a new `IO::Stapled` which reads from *reader* and writes to *writer*- | ||
def initialize(@reader : IO, @writer : IO, @sync_close : Bool = false) | ||
end | ||
|
||
# Reads a single byte from `reader`. | ||
def read_byte : UInt8? | ||
check_open | ||
|
||
@reader.read_byte | ||
end | ||
|
||
# Reads a slice from `reader`. | ||
def read(slice : Bytes) | ||
check_open | ||
|
||
@reader.read(slice) | ||
end | ||
|
||
# Gets a string from `reader`. | ||
def gets(delimiter : Char, limit : Int, chomp = false) : String? | ||
check_open | ||
|
||
@reader.gets(delimiter, limit, chomp) | ||
end | ||
|
||
# Peeks into *reader*. | ||
def peek : Bytes? | ||
check_open | ||
|
||
@reader.peek | ||
end | ||
|
||
# Skips `reader`. | ||
def skip(bytes_count : Int) : Nil | ||
check_open | ||
|
||
@reader.skip(bytes_count) | ||
end | ||
|
||
# Writes a byte to `writer`. | ||
def write_byte(byte : UInt8) : Nil | ||
check_open | ||
|
||
@writer.write_byte(byte) | ||
end | ||
|
||
# Writes a slice to `writer`. | ||
def write(slice : Bytes) : Nil | ||
check_open | ||
|
||
@writer.write(slice) | ||
end | ||
|
||
# `Flushes `writer`. | ||
def flush : self | ||
check_open | ||
|
||
@writer.flush | ||
|
||
self | ||
end | ||
|
||
# Closes this ÌO`. | ||
# | ||
# If `sync_close?` is `true`it will also close the underlying ÌO`s. | ||
def close : Nil | ||
return if @closed | ||
@closed = true | ||
|
||
if @sync_close | ||
@reader.close | ||
@writer.close | ||
end | ||
end | ||
|
||
# Creates a pair of bidirectional pipe endpoints connected with each other | ||
# and passes them to the given block. | ||
# | ||
# Both endpoints and the underlying ÌO`s are closed after the block | ||
# (even if `sync_close?` is `false`). | ||
def self.pipe(read_blocking : Bool = false, write_blocking : Bool = false) | ||
IO.pipe(read_blocking, write_blocking) do |a_read, a_write| | ||
IO.pipe(read_blocking, write_blocking) do |b_read, b_write| | ||
a, b = new(a_read, b_write, true), new(b_read, a_write, true) | ||
begin | ||
yield a, b | ||
ensure | ||
a.close | ||
b.close | ||
end | ||
end | ||
end | ||
end | ||
|
||
# Creates a pair of bidirectional pipe endpoints connected with each other | ||
# and returns them in a `Tuple`. | ||
def self.pipe(read_blocking : Bool = false, write_blocking : Bool = false) : {self, self} | ||
a_read, a_write = IO.pipe(read_blocking, write_blocking) | ||
b_read, b_write = IO.pipe(read_blocking, write_blocking) | ||
return new(a_read, b_write, true), new(b_read, a_write, true) | ||
end | ||
end |