rapns 1.0.6 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/History.md ADDED
@@ -0,0 +1,5 @@
1
+
2
+ 1.0.7 / 2012-04-22
3
+ ==================
4
+
5
+ * Reconnect if the connnection has been idle for more than 30 minutes. TCP Keepalive alone is not enough to ensure the connection is still connected.
@@ -3,10 +3,17 @@ module Rapns
3
3
  class ConnectionError < StandardError; end
4
4
 
5
5
  class Connection
6
+ attr_accessor :last_write
7
+
8
+ def self.idle_period
9
+ 30.minutes
10
+ end
11
+
6
12
  def initialize(name, host, port)
7
13
  @name = name
8
14
  @host = host
9
15
  @port = port
16
+ written
10
17
  end
11
18
 
12
19
  def connect
@@ -31,6 +38,8 @@ module Rapns
31
38
  end
32
39
 
33
40
  def write(data)
41
+ reconnect_idle if idle_period_exceeded?
42
+
34
43
  retry_count = 0
35
44
 
36
45
  begin
@@ -59,9 +68,23 @@ module Rapns
59
68
 
60
69
  protected
61
70
 
71
+ def reconnect_idle
72
+ Rapns::Daemon.logger.info("[#{@name}] Idle period exceeded, reconnecting...")
73
+ reconnect
74
+ end
75
+
76
+ def idle_period_exceeded?
77
+ Time.now - last_write > self.class.idle_period
78
+ end
79
+
62
80
  def write_data(data)
63
81
  @ssl_socket.write(data)
64
82
  @ssl_socket.flush
83
+ written
84
+ end
85
+
86
+ def written
87
+ self.last_write = Time.now
65
88
  end
66
89
 
67
90
  def setup_ssl_context
data/lib/rapns/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Rapns
2
- VERSION = '1.0.6'
2
+ VERSION = '1.0.7'
3
3
  end
@@ -252,3 +252,42 @@ describe Rapns::Daemon::Connection, "when sending a notification" do
252
252
  @connection.write("blah")
253
253
  end
254
254
  end
255
+
256
+ describe Rapns::Daemon::Connection, 'idle period' do
257
+ before do
258
+ @connection = Rapns::Daemon::Connection.new('Connection 0', 'gateway.push.apple.com', 2195)
259
+ @ssl_socket = mock("SSLSocket", :write => nil, :flush => nil, :close => nil)
260
+ @tcp_socket = mock("TCPSocket", :close => nil)
261
+ @connection.stub(:setup_ssl_context)
262
+ @connection.stub(:connect_socket => [@tcp_socket, @ssl_socket])
263
+ @logger = mock("Logger", :info => nil)
264
+ Rapns::Daemon.stub(:logger).and_return(@logger)
265
+ @connection.connect
266
+ end
267
+
268
+ it 'reconnects if the connection has been idle for more than the defined period' do
269
+ Rapns::Daemon::Connection.stub(:idle_period => 0.1)
270
+ sleep 0.2
271
+ @connection.should_receive(:reconnect)
272
+ @connection.write('blah')
273
+ end
274
+
275
+ it 'resets the last write time' do
276
+ now = Time.now
277
+ Time.stub(:now => now)
278
+ @connection.write('blah')
279
+ @connection.last_write.should == now
280
+ end
281
+
282
+ it 'does not reconnect if the connection has not been idle for more than the defined period' do
283
+ @connection.should_not_receive(:reconnect)
284
+ @connection.write('blah')
285
+ end
286
+
287
+ it 'logs the the connection is idle' do
288
+ Rapns::Daemon::Connection.stub(:idle_period => 0.1)
289
+ sleep 0.2
290
+ Rapns::Daemon.logger.should_receive(:info).with('[Connection 0] Idle period exceeded, reconnecting...')
291
+ @connection.write('blah')
292
+ end
293
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,8 +1,12 @@
1
1
  ENV['RAILS_ENV'] = 'test'
2
2
 
3
- require 'simplecov'
4
- SimpleCov.start do
5
- add_filter '/spec/'
3
+ begin
4
+ require 'simplecov'
5
+ SimpleCov.start do
6
+ add_filter '/spec/'
7
+ end
8
+ rescue LoadError
9
+ puts "Coverage disabled."
6
10
  end
7
11
 
8
12
  require 'active_record'
@@ -15,6 +19,7 @@ if !adapters.include?($adapter)
15
19
  end
16
20
 
17
21
  puts "Using #{$adapter} adapter."
22
+
18
23
  ActiveRecord::Base.establish_connection('adapter' => $adapter, 'database' => 'rapns_test')
19
24
  require 'generators/templates/create_rapns_notifications'
20
25
  require 'generators/templates/create_rapns_feedback'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rapns
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-15 00:00:00.000000000 Z
12
+ date: 2012-04-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Easy to use library for Apple's Push Notification Service with Rails
15
15
  3
@@ -50,6 +50,7 @@ files:
50
50
  - lib/rapns/patches/rails/3.1.1/postgresql_adapter.rb
51
51
  - lib/rapns/version.rb
52
52
  - README.md
53
+ - History.md
53
54
  - spec/rapns/daemon/certificate_spec.rb
54
55
  - spec/rapns/daemon/configuration_spec.rb
55
56
  - spec/rapns/daemon/connection_spec.rb
@@ -87,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  version: '0'
88
89
  requirements: []
89
90
  rubyforge_project:
90
- rubygems_version: 1.8.10
91
+ rubygems_version: 1.8.17
91
92
  signing_key:
92
93
  specification_version: 3
93
94
  summary: Easy to use library for Apple's Push Notification Service with Rails 3