Skip to content

Commit

Permalink
Merge pull request stripe#10 from stripe/cory-send-exceptions
Browse files Browse the repository at this point in the history
Ensure that errors sending to a UDP socket don't bubble up.
  • Loading branch information
cory-stripe authored Aug 14, 2017
2 parents 0d11044 + 20cadde commit 04e014c
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 8 deletions.
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ gemspec
gem 'google-protobuf', '3.3.0'
gem 'test-unit'
gem 'rake'

group :test do
gem 'mocha', '1.1.0'
end
8 changes: 6 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
PATH
remote: .
specs:
ssf (0.0.7)
ssf (0.0.8)
google-protobuf (= 3.3.0)

GEM
remote: https://rubygems.org/
specs:
google-protobuf (3.3.0)
metaclass (0.0.4)
mocha (1.1.0)
metaclass (~> 0.0.1)
power_assert (1.0.2)
rake (12.0.0)
test-unit (3.2.4)
Expand All @@ -18,9 +21,10 @@ PLATFORMS

DEPENDENCIES
google-protobuf (= 3.3.0)
mocha (= 1.1.0)
rake
ssf!
test-unit

BUNDLED WITH
1.15.2
1.15.3
11 changes: 10 additions & 1 deletion lib/ssf/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Client
attr_reader :port

attr_reader :service
attr_reader :socket

def initialize(host: DEFAULT_HOST, port: DEFAULT_PORT, service: '', max_buffer_size: 50)
@host = host
Expand All @@ -26,7 +27,15 @@ def connect_to_socket(host, port)
def send_to_socket(span)
message = Ssf::SSFSpan.encode(span)

@socket.send(message, 0)
begin
# Despite UDP being connectionless, some implementations — including
# ruby — will throw an exception if there's nothing listening. We will
# rescue it to avoid any problems for our client.
@socket.send(message, 0)
true
rescue StandardError
false
end
end

def start_span(operation: '', tags: {}, parent: nil)
Expand Down
1 change: 1 addition & 0 deletions lib/ssf/local_buffering_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def initialize(service: '')

def send_to_socket(span)
@buffer << span
true
end

def reset
Expand Down
1 change: 1 addition & 0 deletions lib/ssf/logging_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def connect_to_socket(host, port)

def send_to_socket(span)
puts("would have sent #{span}")
true
end
end
end
1 change: 0 additions & 1 deletion lib/ssf/ssf_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def finish(time: nil)
end

@client.send_to_socket(self)
self
end

def child_span(operation: '', tags: {})
Expand Down
2 changes: 1 addition & 1 deletion ssf-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spec = Gem::Specification.new do |s|
s.name = 'ssf'
s.version = '0.0.7'
s.version = '0.0.8'
s.required_ruby_version = '>= 1.9.3'
s.summary = 'Ruby client for the Standard Sensor Format'
s.description = 'Ruby client for the Standard Sensor Format'
Expand Down
42 changes: 39 additions & 3 deletions test/ssf/sample_test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
require 'mocha/test_unit'
require 'test/unit'
require 'ssf'
require 'securerandom'

module SSFTest
class FailingClient < SSF::Client

def connect_to_socket(host, port)
nil
end

def send_to_socket(span)
false
end
end

class SSFClientTest < Test::Unit::TestCase
def test_create_ssf
s = Ssf::SSFSpan.new({
Expand Down Expand Up @@ -75,13 +87,35 @@ def test_set_tag_with_nils

end

def test_failing_client
c = FailingClient.new(host: '127.0.01', port: '8128')
span = c.start_span(operation: 'run test')
result = span.finish

assert_false(result, "Failing client didn't fail")
end

def test_failing_udp_client
UDPSocket.any_instance.stubs(:send).raises(StandardError.new("explosion"))

c = SSF::Client.new(host: '127.0.01', port: '8128')
STDERR.puts(c.socket)
# c.socket = stub()
# c.socket.stubs(:send)
span = c.start_span(operation: 'run test')
result = span.finish

assert_false(result, "Failing client didn't fail")
end

def test_local_buffer_send
s = Ssf::SSFSpan.new({
id: 123456,
})

c = SSF::LocalBufferingClient.new()
c.send_to_socket(s)
result = c.send_to_socket(s)
assert_true(result, "Local client didn't return true")

assert_equal(1, c.buffer.length, 'Expected to find one span in client')
assert_equal(123456, c.buffer[0].id)
Expand All @@ -95,13 +129,15 @@ def test_client_send
})

c = SSF::LoggingClient.new(host: '127.0.01', port: '8128')
c.send_to_socket(s)
result = c.send_to_socket(s)
assert_true(result, "Logging client didn't return true")
end

def test_full_client_send
c = SSF::LoggingClient.new(host: '127.0.01', port: '8128', service: 'test-srv')
span = c.start_span(operation: 'run test')
span.finish
result = span.finish
assert_true(result, "Logging client didn't return true")

assert(span.end_timestamp > span.start_timestamp)
assert_equal('test_full_client_send(SSFTest::SSFClientTest)', name)
Expand Down

0 comments on commit 04e014c

Please sign in to comment.