thrift_client 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ v0.7.1 Added support for :before_method and :on_exception callback types.
2
+ Added support for registering multiple callbacks of a given type.
3
+
1
4
  v0.7.0 Updated thrift gem dependency to 0.7.0
2
5
 
3
6
  v0.6.3 Document the :connect_timeout option.
@@ -72,9 +72,19 @@ class AbstractThriftClient
72
72
  end
73
73
  end
74
74
 
75
- def add_callback(cb, &blk)
76
- if cb == :post_connect
77
- @callbacks[cb] = blk
75
+ # Adds a callback that will be invoked at a certain time. The valid callback types are:
76
+ # :post_connect - should accept a single AbstractThriftClient argument, which is the client object to
77
+ # which the callback was added. Called after a connection to the remote thrift server
78
+ # is established.
79
+ # :before_method - should accept a single method name argument. Called before a method is invoked on the
80
+ # thrift server.
81
+ # :on_exception - should accept 2 args: an Exception instance and a method name. Called right before the
82
+ # exception is raised.
83
+ def add_callback(callback_type, &block)
84
+ case callback_type
85
+ when :post_connect, :before_method, :on_exception
86
+ @callbacks[callback_type] ||= []
87
+ @callbacks[callback_type].push(block)
78
88
  # Allow chaining
79
89
  return self
80
90
  else
@@ -96,7 +106,7 @@ class AbstractThriftClient
96
106
  transport = @connection.transport
97
107
  transport.timeout = @options[:timeout] if transport_can_timeout?
98
108
  @client = @client_class.new(@options[:protocol].new(transport, *@options[:protocol_extra_params]))
99
- @callbacks[:post_connect].call(self) if @callbacks[:post_connect]
109
+ do_callbacks(:post_connect, self)
100
110
  end
101
111
 
102
112
  def disconnect!
@@ -108,6 +118,14 @@ class AbstractThriftClient
108
118
 
109
119
  private
110
120
 
121
+ # Calls all callbacks of the specified type with the given args
122
+ def do_callbacks(callback_type_sym, *args)
123
+ return unless @callbacks[callback_type_sym]
124
+ @callbacks[callback_type_sym].each do |callback|
125
+ callback.call(*args)
126
+ end
127
+ end
128
+
111
129
  def next_live_server
112
130
  @server_index ||= 0
113
131
  @server_list.length.times do |i|
@@ -128,6 +146,7 @@ class AbstractThriftClient
128
146
  @client.timeout = @options[:timeout_overrides][method_name.to_sym] || @options[:timeout]
129
147
  end
130
148
  @request_count += 1
149
+ do_callbacks(:before_method, method_name)
131
150
  @client.send(method_name, *args)
132
151
  rescue *@options[:exception_class_overrides] => e
133
152
  raise_or_default(e, method_name)
@@ -147,13 +166,14 @@ class AbstractThriftClient
147
166
 
148
167
  def raise_or_default(e, method_name)
149
168
  if @options[:raise]
150
- raise_wrapped_error(e)
169
+ raise_wrapped_error(e, method_name)
151
170
  else
152
171
  @options[:defaults][method_name.to_sym]
153
172
  end
154
173
  end
155
174
 
156
- def raise_wrapped_error(e)
175
+ def raise_wrapped_error(e, method_name)
176
+ do_callbacks(:on_exception, e, method_name)
157
177
  if @options[:wrapped_exception_classes].include?(e.class)
158
178
  raise @client_class.const_get(e.class.to_s.split('::').last), e.message, e.backtrace
159
179
  else
@@ -98,6 +98,35 @@ class ThriftClientTest < Test::Unit::TestCase
98
98
  assert_equal(1, calledcnt)
99
99
  end
100
100
 
101
+ def test_before_method_cb
102
+ before_method_counts = Hash.new { |hash, key| hash[key] = 0 }
103
+ client = ThriftClient.new(Greeter::Client, @servers, @options.merge(:retries => 2))
104
+ r = client.add_callback :before_method do |method_name|
105
+ before_method_counts[method_name.to_sym] += 1
106
+ end
107
+ assert_equal(client, r)
108
+ assert_nothing_raised do
109
+ client.greeting("someone")
110
+ client.yo("dude")
111
+ client.yo("dawg")
112
+ client.disconnect!
113
+ end
114
+ assert_equal({:greeting => 1, :yo => 2}, before_method_counts)
115
+ end
116
+
117
+ def test_on_exception_cb
118
+ on_exception_counts = Hash.new { |h1, method_name| h1[method_name] = Hash.new { |h2, clazz| h2[clazz] = 0 }}
119
+ client = ThriftClient.new(Greeter::Client, @servers[0,2], @options.merge(:retries => 2))
120
+ r = client.add_callback :on_exception do |error, method_name|
121
+ on_exception_counts[method_name.to_sym][error.class] += 1
122
+ end
123
+ assert_equal(client, r)
124
+ assert_raises(ThriftClient::NoServersAvailable) do
125
+ client.greeting("someone")
126
+ client.disconnect!
127
+ end
128
+ assert_equal({:greeting => {ThriftClient::NoServersAvailable => 1}}, on_exception_counts)
129
+ end
101
130
 
102
131
  def test_unknown_cb
103
132
  calledcnt = 0
@@ -108,6 +137,23 @@ class ThriftClientTest < Test::Unit::TestCase
108
137
  assert_equal(nil, r)
109
138
  end
110
139
 
140
+ def test_multiple_cb
141
+ calledcnt = 0
142
+ client = ThriftClient.new(Greeter::Client, @servers, @options.merge(:retries => 2))
143
+ 2.times do |i|
144
+ r = client.add_callback :post_connect do |cl|
145
+ calledcnt += 1
146
+ assert_equal(client, cl)
147
+ end
148
+ assert_equal(client, r)
149
+ end
150
+ assert_nothing_raised do
151
+ client.greeting("someone")
152
+ client.disconnect!
153
+ end
154
+ assert_equal(2, calledcnt)
155
+ end
156
+
111
157
  def test_no_servers_eventually_raise
112
158
  wascalled = false
113
159
  client = ThriftClient.new(Greeter::Client, @servers[0,2], @options.merge(:retries => 2))
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{thrift_client}
5
- s.version = "0.7.0"
5
+ s.version = "0.7.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0.8") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = [%q{Evan Weaver, Ryan King, Jeff Hodges}]
9
- s.date = %q{2011-08-22}
9
+ s.date = %q{2011-09-02}
10
10
  s.description = %q{A Thrift client wrapper that encapsulates some common failover behavior.}
11
11
  s.email = %q{}
12
12
  s.extra_rdoc_files = [%q{CHANGELOG}, %q{LICENSE}, %q{README.rdoc}, %q{lib/thrift_client.rb}, %q{lib/thrift_client/abstract_thrift_client.rb}, %q{lib/thrift_client/connection.rb}, %q{lib/thrift_client/connection/base.rb}, %q{lib/thrift_client/connection/factory.rb}, %q{lib/thrift_client/connection/http.rb}, %q{lib/thrift_client/connection/socket.rb}, %q{lib/thrift_client/event_machine.rb}, %q{lib/thrift_client/simple.rb}, %q{lib/thrift_client/thrift.rb}]
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thrift_client
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 0
10
- version: 0.7.0
9
+ - 1
10
+ version: 0.7.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Evan Weaver, Ryan King, Jeff Hodges
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-22 00:00:00 Z
18
+ date: 2011-09-02 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: thrift