backchat 0.0.1 → 0.0.2
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/lib/backchat.rb +0 -1
- data/lib/backchat/connection_pool.rb +21 -13
- data/lib/backchat/version.rb +1 -1
- data/test/lib/backchat/test_connection_pool.rb +6 -4
- metadata +2 -2
data/lib/backchat.rb
CHANGED
@@ -6,34 +6,39 @@ module Backchat
|
|
6
6
|
host: 'localhost',
|
7
7
|
username: 'admin',
|
8
8
|
password: 'password',
|
9
|
-
pool_size: 5
|
9
|
+
pool_size: 5,
|
10
|
+
persistent: true }
|
10
11
|
|
11
12
|
@settings = DEFAULT_SETTINGS
|
12
13
|
|
13
14
|
def initialize(count = self.class.settings[:pool_size])
|
14
|
-
@queue = fill(
|
15
|
+
@queue = fill(SizedQueue.new(count))
|
15
16
|
end
|
16
17
|
|
17
|
-
def
|
18
|
-
queue.
|
18
|
+
def count
|
19
|
+
queue.length
|
19
20
|
end
|
20
21
|
|
21
22
|
def take
|
22
|
-
queue.
|
23
|
+
queue.pop
|
23
24
|
end
|
24
25
|
|
25
26
|
def add(connection)
|
26
|
-
queue.
|
27
|
+
queue.push(connection)
|
27
28
|
end
|
28
29
|
|
29
30
|
def with_connection
|
30
31
|
connection = take
|
32
|
+
connection.connect unless connection.is_connected?
|
33
|
+
connection.login(self.class.settings[:username],
|
34
|
+
self.class.settings[:password],
|
35
|
+
"resource_#{connection.connection_id}") unless connection.is_authenticated?
|
31
36
|
yield connection
|
32
37
|
ensure
|
38
|
+
connection.disconnect unless self.class.settings[:persistent]
|
33
39
|
add(connection)
|
34
40
|
end
|
35
41
|
|
36
|
-
|
37
42
|
class << self
|
38
43
|
attr_reader :settings
|
39
44
|
|
@@ -58,7 +63,7 @@ module Backchat
|
|
58
63
|
end
|
59
64
|
|
60
65
|
private
|
61
|
-
[:host, :username, :password, :pool_size].each do |key|
|
66
|
+
[:host, :username, :password, :pool_size, :persistent].each do |key|
|
62
67
|
define_method("#{key}") do |val|
|
63
68
|
@settings ||= {}
|
64
69
|
@settings[key] = val
|
@@ -70,13 +75,16 @@ module Backchat
|
|
70
75
|
attr_reader :queue
|
71
76
|
|
72
77
|
def fill(queue)
|
73
|
-
while queue.
|
78
|
+
while queue.length < queue.max
|
74
79
|
conn = XMPPConnection.new(self.class.settings[:host])
|
75
|
-
|
76
|
-
|
77
|
-
|
80
|
+
if self.class.settings[:persistent]
|
81
|
+
conn.connect
|
82
|
+
conn.login(self.class.settings[:username],
|
83
|
+
self.class.settings[:password],
|
84
|
+
"resource_#{conn.connection_id}")
|
85
|
+
end
|
78
86
|
|
79
|
-
queue.
|
87
|
+
queue.push(conn)
|
80
88
|
end
|
81
89
|
|
82
90
|
queue
|
data/lib/backchat/version.rb
CHANGED
@@ -19,7 +19,8 @@ class BackchatConnectionPoolTest < MiniTest::Unit::TestCase
|
|
19
19
|
assert_equal Backchat::ConnectionPool.settings, { host: 'example.com',
|
20
20
|
username: 'don_draper',
|
21
21
|
password: 'secret',
|
22
|
-
pool_size: 1
|
22
|
+
pool_size: 1,
|
23
|
+
persistent: true }
|
23
24
|
end
|
24
25
|
|
25
26
|
def test_configure_accepts_a_hash
|
@@ -29,7 +30,8 @@ class BackchatConnectionPoolTest < MiniTest::Unit::TestCase
|
|
29
30
|
assert_equal Backchat::ConnectionPool.settings, { host: 'example.com',
|
30
31
|
username: 'don_draper',
|
31
32
|
password: 'secret',
|
32
|
-
pool_size: 1
|
33
|
+
pool_size: 1,
|
34
|
+
persistent: true }
|
33
35
|
end
|
34
36
|
|
35
37
|
def test_configure_allows_partial_settings_in_a_block
|
@@ -50,8 +52,8 @@ class BackchatConnectionPoolTest < MiniTest::Unit::TestCase
|
|
50
52
|
assert_includes @pool.instance_variables, :@queue
|
51
53
|
end
|
52
54
|
|
53
|
-
def
|
54
|
-
assert_instance_of
|
55
|
+
def test_queue_is_a_queue
|
56
|
+
assert_instance_of SizedQueue, @pool.instance_variable_get(:@queue)
|
55
57
|
end
|
56
58
|
|
57
59
|
def test_initialize_fills_the_pool_with_connections
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: backchat
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
4
5
|
prerelease:
|
5
|
-
version: 0.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Zach Pendleton
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-04-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|