zk-eventmachine 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,6 +12,7 @@ module ZK
12
12
  def initialize(host, opts={})
13
13
  @host = host
14
14
  @close_deferred = Deferred::Default.new
15
+ @connection_lost_deferred = Deferred::Default.new
15
16
  @event_handler = EventHandlerEM.new(self)
16
17
  end
17
18
 
@@ -29,6 +30,18 @@ module ZK
29
30
  @cnx.on_attached(&blk)
30
31
  end
31
32
 
33
+ # If we get a ZK::Exceptions::ConnectionLoss exeption back from any call,
34
+ # we will call back any handlers registered here with the exception
35
+ # instance as the argument
36
+ #
37
+ # once this deferred has been fired, it will be replaced with a new
38
+ # deferred, so callbacks must be re-registered
39
+ #
40
+ def on_connection_lost(&blk)
41
+ @connection_lost_deferred.callback(&blk) if blk
42
+ @connection_lost_deferred
43
+ end
44
+
32
45
  def reopen(*a)
33
46
  raise NotImplementedError, "reoopen is not implemented for the eventmachine version of the client"
34
47
  end
@@ -61,18 +74,21 @@ module ZK
61
74
  #
62
75
  def get(path, opts={}, &block)
63
76
  Callback.new_get_cb(block) do |cb|
77
+ cb.errback(&method(:connection_lost_hook))
64
78
  super(path, opts.merge(:callback => cb))
65
79
  end
66
80
  end
67
81
 
68
82
  def create(path, data='', opts={}, &block)
69
83
  Callback.new_create_cb(block) do |cb|
84
+ cb.errback(&method(:connection_lost_hook))
70
85
  super(path, data, opts.merge(:callback => cb))
71
86
  end
72
87
  end
73
88
 
74
89
  def set(path, data, opts={}, &block)
75
90
  Callback.new_set_cb(block) do |cb|
91
+ cb.errback(&method(:connection_lost_hook))
76
92
  super(path, data, opts.merge(:callback => cb))
77
93
  end
78
94
  end
@@ -83,6 +99,7 @@ module ZK
83
99
  meth = :"new_#{cb_style}_cb"
84
100
 
85
101
  Callback.__send__(meth, block) do |cb|
102
+ cb.errback(&method(:connection_lost_hook))
86
103
  super(path, opts.merge(:callback => cb))
87
104
  end
88
105
  end
@@ -93,27 +110,39 @@ module ZK
93
110
 
94
111
  def delete(path, opts={}, &block)
95
112
  Callback.new_delete_cb(block) do |cb|
113
+ cb.errback(&method(:connection_lost_hook))
96
114
  super(path, opts.merge(:callback => cb))
97
115
  end
98
116
  end
99
117
 
100
118
  def children(path, opts={}, &block)
101
119
  Callback.new_children_cb(block) do |cb|
120
+ cb.errback(&method(:connection_lost_hook))
102
121
  super(path, opts.merge(:callback => cb))
103
122
  end
104
123
  end
105
124
 
106
125
  def get_acl(path, opts={}, &block)
107
126
  Callback.new_get_acl_cb(block) do |cb|
127
+ cb.errback(&method(:connection_lost_hook))
108
128
  super(path, opts.merge(:callback => cb))
109
129
  end
110
130
  end
111
131
 
112
132
  def set_acl(path, acls, opts={}, &block)
113
133
  Callback.new_set_acl_cb(block) do |cb|
134
+ cb.errback(&method(:connection_lost_hook))
114
135
  super(path, acls, opts.merge(:callback => cb))
115
136
  end
116
137
  end
138
+
139
+ protected
140
+ def connection_lost_hook(exc)
141
+ if exc and exc.kind_of?(ZK::Exceptions::ConnectionLoss)
142
+ dfr, @connection_lost_deferred = @connection_lost_deferred, Deferred::Default.new
143
+ dfr.succeed(exc)
144
+ end
145
+ end
117
146
  end
118
147
  end
119
148
  end
@@ -1,5 +1,5 @@
1
1
  module ZK
2
2
  module ZKEventMachine
3
- VERSION = "0.1.7"
3
+ VERSION = "0.1.8"
4
4
  end
5
5
  end
@@ -45,6 +45,44 @@ module ZK::ZKEventMachine
45
45
  end
46
46
  end
47
47
 
48
+ describe 'on_connection_loss' do
49
+ before do
50
+ @path = [@base_path, 'foo'].join('/')
51
+ @data = 'this is data'
52
+ @zk.create(@path, @data)
53
+ end
54
+
55
+ it %[should be called back if the connection is lost] do
56
+ em do
57
+ @zkem.on_connection_lost do |exc|
58
+ logger.debug { "WIN!" }
59
+ exc.should be_kind_of(ZK::Exceptions::ConnectionLoss)
60
+ @zkem.close! { done }
61
+ end
62
+
63
+ @zkem.connect do
64
+ flexmock(@zkem.cnx) do |m|
65
+ m.should_receive(:get).with(Hash).and_return do |hash|
66
+ logger.debug { "client received :get wtih #{hash.inspect}" }
67
+ @user_cb = hash[:callback]
68
+
69
+ EM.next_tick do
70
+ logger.debug { "calling back user cb with connection loss" }
71
+ @user_cb.call(:rc => ZK::Exceptions::CONNECTIONLOSS)
72
+ end
73
+
74
+ { :rc => Zookeeper::ZOK }
75
+ end
76
+ end
77
+
78
+ @zkem.get(@path) do |exc,data|
79
+ exc.should be_kind_of(ZK::Exceptions::ConnectionLoss)
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+
48
86
  describe 'get' do
49
87
  describe 'success' do
50
88
  before do
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
16
16
 
17
17
  # zk depends on slyphon-zookeeper, but we need at least this version
18
18
  s.add_dependency('slyphon-zookeeper', '~> 0.2.4')
19
- s.add_dependency('eventmachine', '>= 0.12.10')
19
+ s.add_dependency('eventmachine', '~> 1.0.0.beta.3')
20
20
 
21
21
  s.add_development_dependency('rspec', '~> 2.5.0')
22
22
  s.add_development_dependency('yard', '~> 0.7.0')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zk-eventmachine
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 7
10
- version: 0.1.7
9
+ - 8
10
+ version: 0.1.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jonathan D. Simms
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-05 00:00:00 +00:00
19
- default_executable:
18
+ date: 2011-10-26 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: zk
@@ -56,14 +55,16 @@ dependencies:
56
55
  requirement: &id003 !ruby/object:Gem::Requirement
57
56
  none: false
58
57
  requirements:
59
- - - ">="
58
+ - - ~>
60
59
  - !ruby/object:Gem::Version
61
- hash: 59
60
+ hash: 62196357
62
61
  segments:
62
+ - 1
63
63
  - 0
64
- - 12
65
- - 10
66
- version: 0.12.10
64
+ - 0
65
+ - beta
66
+ - 3
67
+ version: 1.0.0.beta.3
67
68
  type: :runtime
68
69
  version_requirements: *id003
69
70
  - !ruby/object:Gem::Dependency
@@ -183,7 +184,6 @@ files:
183
184
  - spec/z_k/z_k_event_machine/event_handler_e_m_spec.rb
184
185
  - spec/z_k/z_k_event_machine/unixisms_spec.rb
185
186
  - zk-eventmachine.gemspec
186
- has_rdoc: true
187
187
  homepage: https://github.com/slyphon/zk-eventmachine
188
188
  licenses: []
189
189
 
@@ -213,7 +213,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
213
213
  requirements: []
214
214
 
215
215
  rubyforge_project:
216
- rubygems_version: 1.6.2
216
+ rubygems_version: 1.8.6
217
217
  signing_key:
218
218
  specification_version: 3
219
219
  summary: ZK client for EventMachine-based (async) applications