Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions lib/rb/lib/thrift/transport/memory_buffer_transport.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ module Thrift
class MemoryBufferTransport < BaseTransport
GARBAGE_BUFFER_SIZE = 4*(2**10) # 4kB
Comment thread
kpumuk marked this conversation as resolved.

# If you pass a string to this, you should #dup that string
# unless you want it to be modified by #read and #write
#--
# this behavior is no longer required. If you wish to change it
# go ahead, just make sure the specs pass
# The transport copies the input buffer and keeps its own mutable storage.
def initialize(buffer = nil)
@buf = buffer ? Bytes.force_binary_encoding(buffer) : Bytes.empty_byte_buffer
@buf = buffer ? Bytes.force_binary_encoding(buffer.dup) : Bytes.empty_byte_buffer
Comment thread
kpumuk marked this conversation as resolved.
@index = 0
end

Expand Down
26 changes: 20 additions & 6 deletions lib/rb/spec/base_transport_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,26 @@
expect(@buffer.to_s).to eq("memory")
end

it "should accept a buffer on input and use it directly" do
s = +"this is a test"
@buffer = Thrift::MemoryBufferTransport.new(s)
expect(@buffer.read(4)).to eq("this")
s.slice!(-4..-1)
expect(@buffer.read(@buffer.available)).to eq(" is a ")
it "should privately own a buffer passed on input" do
source = +"this is a test"
@buffer = Thrift::MemoryBufferTransport.new(source)

expect(source.encoding).to eq(Encoding::UTF_8)
source.replace("caller changed")
@buffer.write("!")

expect(source).to eq("caller changed")
expect(@buffer.read(@buffer.available)).to eq("this is a test!".b)
end

it "should allow writes and resets after receiving a frozen buffer" do
@buffer = Thrift::MemoryBufferTransport.new("abc".b.freeze)

@buffer.write("d")
expect(@buffer.read(4)).to eq("abcd")

@buffer.reset_buffer("xy")
expect(@buffer.read(2)).to eq("xy")
end

it "should always remain open" do
Expand Down
Loading