Skip to content
This repository has been archived by the owner on Aug 25, 2022. It is now read-only.

Commit

Permalink
Use Constant instead of String
Browse files Browse the repository at this point in the history
  • Loading branch information
blowmage committed Jun 8, 2018
1 parent e36c644 commit 1f9d9f8
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ def create_pubsub_message data, attributes
data = data.read
end
# Convert data to encoded byte array to match the protobuf defn
data_bytes = String(data).dup.force_encoding("ASCII-8BIT").freeze
data_bytes = \
String(data).dup.force_encoding(Encoding::ASCII_8BIT).freeze

# Convert attributes to strings to match the protobuf definition
attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def create_pubsub_message data, attributes
data = data.read
end
# Convert data to encoded byte array to match the protobuf defn
data_bytes = String(data).dup.force_encoding("ASCII-8BIT").freeze
data_bytes = \
String(data).dup.force_encoding(Encoding::ASCII_8BIT).freeze

# Convert attributes to strings to match the protobuf definition
attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
Expand Down
2 changes: 1 addition & 1 deletion google-cloud-pubsub/lib/google/cloud/pubsub/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def initialize data = nil, attributes = {}
attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]

@grpc = Google::Pubsub::V1::PubsubMessage.new(
data: String(data).encode("ASCII-8BIT"),
data: String(data).dup.force_encoding(Encoding::ASCII_8BIT),
attributes: attributes
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
let(:message1) { "new-message-here" }
let(:message2) { "second-new-message" }
let(:message3) { "third-new-message" }
let(:msg_encoded1) { message1.encode("ASCII-8BIT") }
let(:msg_encoded2) { message2.encode("ASCII-8BIT") }
let(:msg_encoded3) { message3.encode("ASCII-8BIT") }
let(:msg_encoded1) { message1.encode(Encoding::ASCII_8BIT) }
let(:msg_encoded2) { message2.encode(Encoding::ASCII_8BIT) }
let(:msg_encoded3) { message3.encode(Encoding::ASCII_8BIT) }

it "publishes a message" do
messages = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

it "publishes a message" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode("ASCII-8BIT"))
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode(Encoding::ASCII_8BIT))
]
publish_res = Google::Pubsub::V1::PublishResponse.decode_json({ message_ids: ["msg1"] }.to_json)
mock = Minitest::Mock.new
Expand Down Expand Up @@ -54,7 +54,7 @@

it "publishes a message with a callback" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode("ASCII-8BIT"))
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode(Encoding::ASCII_8BIT))
]
publish_res = Google::Pubsub::V1::PublishResponse.decode_json({ message_ids: ["msg1"] }.to_json)
mock = Minitest::Mock.new
Expand Down Expand Up @@ -97,7 +97,7 @@

it "publishes a message with multibyte characters" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "\xE3\x81\x82".force_encoding("ASCII-8BIT"))
Google::Pubsub::V1::PubsubMessage.new(data: "\xE3\x81\x82".force_encoding(Encoding::ASCII_8BIT))
]
publish_res = Google::Pubsub::V1::PublishResponse.decode_json({ message_ids: ["msg1"] }.to_json)
mock = Minitest::Mock.new
Expand All @@ -112,7 +112,7 @@
assert_kind_of Google::Cloud::Pubsub::PublishResult, result
assert result.succeeded?
assert_equal "msg1", result.msg_id
assert_equal "\xE3\x81\x82".force_encoding("ASCII-8BIT"), result.data
assert_equal "\xE3\x81\x82".force_encoding(Encoding::ASCII_8BIT), result.data
callback_called = true
end

Expand Down Expand Up @@ -141,7 +141,7 @@

it "publishes a message using an IO-ish object" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "\xE3\x81\x82".force_encoding("ASCII-8BIT"))
Google::Pubsub::V1::PubsubMessage.new(data: "\xE3\x81\x82".force_encoding(Encoding::ASCII_8BIT))
]
publish_res = Google::Pubsub::V1::PublishResponse.decode_json({ message_ids: ["msg1"] }.to_json)
mock = Minitest::Mock.new
Expand All @@ -161,7 +161,7 @@
assert_kind_of Google::Cloud::Pubsub::PublishResult, result
assert result.succeeded?
assert_equal "msg1", result.msg_id
assert_equal "\xE3\x81\x82".force_encoding("ASCII-8BIT"), result.data
assert_equal "\xE3\x81\x82".force_encoding(Encoding::ASCII_8BIT), result.data
callback_called = true
end
end
Expand Down Expand Up @@ -191,7 +191,7 @@

it "publishes a message with attributes" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode("ASCII-8BIT"), attributes: {"format" => "text"})
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode(Encoding::ASCII_8BIT), attributes: {"format" => "text"})
]
publish_res = Google::Pubsub::V1::PublishResponse.decode_json({ message_ids: ["msg1"] }.to_json)
mock = Minitest::Mock.new
Expand All @@ -206,7 +206,7 @@
assert_kind_of Google::Cloud::Pubsub::PublishResult, result
assert result.succeeded?
assert_equal "msg1", result.msg_id
assert_equal "async-message".force_encoding("ASCII-8BIT"), result.data
assert_equal "async-message".force_encoding(Encoding::ASCII_8BIT), result.data
assert_equal "text", result.attributes["format"]
callback_called = true
end
Expand Down Expand Up @@ -241,7 +241,7 @@

it "publishes a message" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode("ASCII-8BIT"))
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode(Encoding::ASCII_8BIT))
]
publish_res = Google::Pubsub::V1::PublishResponse.decode_json({ message_ids: ["msg1"] }.to_json)
mock = Minitest::Mock.new
Expand Down Expand Up @@ -274,7 +274,7 @@

it "publishes a message with attributes" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode("ASCII-8BIT"), attributes: { "format" => "text" })
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode(Encoding::ASCII_8BIT), attributes: { "format" => "text" })
]
publish_res = Google::Pubsub::V1::PublishResponse.decode_json({ message_ids: ["msg1"] }.to_json)
mock = Minitest::Mock.new
Expand All @@ -289,7 +289,7 @@
assert_kind_of Google::Cloud::Pubsub::PublishResult, result
assert result.succeeded?
assert_equal "msg1", result.msg_id
assert_equal "async-message".force_encoding("ASCII-8BIT"), result.data
assert_equal "async-message".force_encoding(Encoding::ASCII_8BIT), result.data
assert_equal "text", result.attributes["format"]
callback_called = true
end
Expand Down Expand Up @@ -330,7 +330,7 @@

it "publishes a message" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode("ASCII-8BIT"))
Google::Pubsub::V1::PubsubMessage.new(data: "async-message".encode(Encoding::ASCII_8BIT))
]

stub = Object.new
Expand All @@ -350,7 +350,7 @@ def stub.publish *args
assert_kind_of Google::Cloud::Pubsub::PublishResult, result
refute result.succeeded?
assert result.failed?
assert_equal "async-message".force_encoding("ASCII-8BIT"), result.data
assert_equal "async-message".force_encoding(Encoding::ASCII_8BIT), result.data
assert_kind_of Google::Cloud::NotFoundError, result.error
callback_called = true
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
let(:message1) { "new-message-here" }
let(:message2) { "second-new-message" }
let(:message3) { "third-new-message" }
let(:msg_encoded1) { message1.encode("ASCII-8BIT") }
let(:msg_encoded2) { message2.encode("ASCII-8BIT") }
let(:msg_encoded3) { message3.encode("ASCII-8BIT") }
let(:msg_encoded1) { message1.encode(Encoding::ASCII_8BIT) }
let(:msg_encoded2) { message2.encode(Encoding::ASCII_8BIT) }
let(:msg_encoded3) { message3.encode(Encoding::ASCII_8BIT) }

it "publishes a message" do
messages = [
Expand All @@ -44,7 +44,7 @@

it "publishes a message with multibyte characters" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "\xE3\x81\x82".force_encoding("ASCII-8BIT"))
Google::Pubsub::V1::PubsubMessage.new(data: "\xE3\x81\x82".force_encoding(Encoding::ASCII_8BIT))
]
publish_res = Google::Pubsub::V1::PublishResponse.decode_json({ message_ids: ["msg1"] }.to_json)
mock = Minitest::Mock.new
Expand All @@ -56,13 +56,13 @@
mock.verify

msg.must_be_kind_of Google::Cloud::Pubsub::Message
msg.data.must_equal "\xE3\x81\x82".force_encoding("ASCII-8BIT")
msg.data.must_equal "\xE3\x81\x82".force_encoding(Encoding::ASCII_8BIT)
msg.message_id.must_equal "msg1"
end

it "publishes a message using an IO-ish object" do
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: "\xE3\x81\x82".force_encoding("ASCII-8BIT"))
Google::Pubsub::V1::PubsubMessage.new(data: "\xE3\x81\x82".force_encoding(Encoding::ASCII_8BIT))
]
publish_res = Google::Pubsub::V1::PublishResponse.decode_json({ message_ids: ["msg1"] }.to_json)
mock = Minitest::Mock.new
Expand All @@ -80,7 +80,7 @@
mock.verify

msg.must_be_kind_of Google::Cloud::Pubsub::Message
msg.data.must_equal "\xE3\x81\x82".force_encoding("ASCII-8BIT")
msg.data.must_equal "\xE3\x81\x82".force_encoding(Encoding::ASCII_8BIT)
msg.message_id.must_equal "msg1"
end

Expand Down
8 changes: 4 additions & 4 deletions google-cloud-pubsub/test/google/cloud/pubsub/topic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def stub.create_subscription *args

it "can publish a message" do
message = "new-message-here"
encoded_msg = message.encode("ASCII-8BIT")
encoded_msg = message.encode(Encoding::ASCII_8BIT)
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: encoded_msg)
]
Expand All @@ -466,7 +466,7 @@ def stub.create_subscription *args

it "can publish a message with attributes" do
message = "new-message-here"
encoded_msg = message.encode("ASCII-8BIT")
encoded_msg = message.encode(Encoding::ASCII_8BIT)
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: encoded_msg, attributes: { "format" => "text" })
]
Expand All @@ -487,8 +487,8 @@ def stub.create_subscription *args
it "can publish multiple messages with a block" do
message1 = "first-new-message"
message2 = "second-new-message"
encoded_msg1 = message1.encode("ASCII-8BIT")
encoded_msg2 = message2.encode("ASCII-8BIT")
encoded_msg1 = message1.encode(Encoding::ASCII_8BIT)
encoded_msg2 = message2.encode(Encoding::ASCII_8BIT)
messages = [
Google::Pubsub::V1::PubsubMessage.new(data: encoded_msg1),
Google::Pubsub::V1::PubsubMessage.new(data: encoded_msg2, attributes: { "format" => "none" })
Expand Down

0 comments on commit 1f9d9f8

Please sign in to comment.