discover 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +8 -4
- data/lib/discover.rb +11 -3
- data/lib/discover/version.rb +1 -1
- data/test/integration/test_registration.rb +8 -8
- data/test/integration/test_service_leader.rb +10 -9
- data/test/integration/test_service_updates.rb +4 -4
- data/test/unit/test_client.rb +37 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8e5f959700c606323c1b0b0748db67ac608ad9da
|
4
|
+
data.tar.gz: 1621c20260613bd508ae1623689cd29932923b5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b8b8cd45b2e7bbee0c168891194b3bf631d5b43f26caa3159f6a0808fd622bc422982202ff7c4473631fa7cc955355b8b85e9777047c065d52d1e7e41723f36
|
7
|
+
data.tar.gz: fced2f474c7430f855e2666e12d7fb81173c18e2b3d9dc03ba7981a75ca9270b205181e3710fbacbab4ee6349acb7ce6ed2c5aa9f76e51679a1004c35d4d0bd1
|
data/Rakefile
CHANGED
@@ -1,14 +1,18 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
2
3
|
|
3
|
-
desc "Run all tests
|
4
|
-
task :default => "test:integration"
|
4
|
+
desc "Run all tests"
|
5
|
+
task :default => ["test:unit", "test:integration"]
|
5
6
|
|
6
7
|
namespace :test do
|
8
|
+
Rake::TestTask.new(:unit) do |t|
|
9
|
+
t.libs << "test"
|
10
|
+
t.pattern = "test/unit/test_*.rb"
|
11
|
+
end
|
12
|
+
|
7
13
|
# If HAS_DEPENDENCIES is set then we just run the tests, otherwise we spin up a
|
8
14
|
# Docker container with all the dependencies installed and run the tests inside that
|
9
15
|
if ENV["HAS_DEPENDENCIES"]
|
10
|
-
require "rake/testtask"
|
11
|
-
|
12
16
|
Rake::TestTask.new(:integration) do |t|
|
13
17
|
t.libs << "test"
|
14
18
|
t.pattern = "test/integration/test_*.rb"
|
data/lib/discover.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
require
|
1
|
+
require "uri"
|
2
2
|
require "rpcplus"
|
3
3
|
|
4
4
|
module Discover
|
5
5
|
class Client
|
6
6
|
include Celluloid
|
7
7
|
|
8
|
-
def initialize(
|
9
|
-
|
8
|
+
def initialize(address = nil)
|
9
|
+
uri = parse(address || ENV["DISCOVERD"] || "127.0.0.1:1111")
|
10
|
+
|
11
|
+
@client = RPCPlus::Client.new(uri.host, uri.port)
|
10
12
|
@registrations = []
|
11
13
|
end
|
12
14
|
|
@@ -35,6 +37,12 @@ module Discover
|
|
35
37
|
end
|
36
38
|
|
37
39
|
private
|
40
|
+
def parse(address)
|
41
|
+
URI.parse(address)
|
42
|
+
rescue URI::InvalidURIError
|
43
|
+
URI.parse("tcp://#{address}")
|
44
|
+
end
|
45
|
+
|
38
46
|
def _register(name, port=nil, ip=nil, attributes={}, standby=false)
|
39
47
|
reg = Registration.new(self, name, "#{ip}:#{port}", attributes, standby)
|
40
48
|
@registrations << reg
|
data/lib/discover/version.rb
CHANGED
@@ -26,7 +26,7 @@ class TestRegistration < DiscoverIntegrationTest
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def test_service_is_online_after_registration
|
29
|
-
name = "
|
29
|
+
name = "service-online"
|
30
30
|
port = 1111
|
31
31
|
ip = "127.0.0.1"
|
32
32
|
attributes = { "foo" => "bar" }
|
@@ -47,7 +47,7 @@ class TestRegistration < DiscoverIntegrationTest
|
|
47
47
|
end
|
48
48
|
|
49
49
|
def test_service_is_offline_after_unregister
|
50
|
-
name = "
|
50
|
+
name = "service-offline"
|
51
51
|
port = 1111
|
52
52
|
ip = "127.0.0.1"
|
53
53
|
|
@@ -63,7 +63,7 @@ class TestRegistration < DiscoverIntegrationTest
|
|
63
63
|
end
|
64
64
|
|
65
65
|
def test_changing_service_attributes
|
66
|
-
name = "
|
66
|
+
name = "change-attributes"
|
67
67
|
port = 1111
|
68
68
|
ip = "127.0.0.1"
|
69
69
|
attributes = { "foo" => "bar" }
|
@@ -87,7 +87,7 @@ class TestRegistration < DiscoverIntegrationTest
|
|
87
87
|
end
|
88
88
|
|
89
89
|
def test_service_with_filters
|
90
|
-
name = "
|
90
|
+
name = "service-filters"
|
91
91
|
ip = "127.0.0.1"
|
92
92
|
|
93
93
|
matching_attributes = { "foo" => "bar", "baz" => "qux" }
|
@@ -107,22 +107,22 @@ class TestRegistration < DiscoverIntegrationTest
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def test_register_and_standby
|
110
|
-
name = "
|
110
|
+
name = "register-and-standby"
|
111
111
|
ip = "127.0.0.1"
|
112
112
|
|
113
113
|
registrations = []
|
114
114
|
registrations << @client.register(name, 1111, ip)
|
115
115
|
|
116
116
|
standby = TestRegisterStandby.new @client, name, 2222, ip
|
117
|
-
sleep(0.
|
117
|
+
sleep(0.5)
|
118
118
|
assert !standby.elected?
|
119
119
|
|
120
120
|
registrations << @client.register(name, 3333, ip)
|
121
|
-
sleep(0.
|
121
|
+
sleep(0.5)
|
122
122
|
assert !standby.elected?
|
123
123
|
|
124
124
|
registrations.each(&:unregister)
|
125
|
-
sleep(0.
|
125
|
+
sleep(0.5)
|
126
126
|
assert standby.elected?
|
127
127
|
end
|
128
128
|
end
|
@@ -21,7 +21,7 @@ class TestServiceLeader < DiscoverIntegrationTest
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_leader_is_oldest_online_service
|
24
|
-
name = "
|
24
|
+
name = "leader-is-oldest"
|
25
25
|
ip = "127.0.0.1"
|
26
26
|
|
27
27
|
service = @client.service(name)
|
@@ -29,24 +29,25 @@ class TestServiceLeader < DiscoverIntegrationTest
|
|
29
29
|
|
30
30
|
registrations = []
|
31
31
|
registrations << @client.register(name, 1111, ip)
|
32
|
+
sleep(0.5)
|
32
33
|
assert_equal "#{ip}:1111", service.leader.address
|
33
34
|
|
34
35
|
registrations << @client.register(name, 2222, ip)
|
35
36
|
registrations << @client.register(name, 3333, ip)
|
36
|
-
sleep(0.
|
37
|
+
sleep(0.5)
|
37
38
|
assert_equal "#{ip}:1111", service.leader.address
|
38
39
|
|
39
40
|
registrations.shift.unregister
|
40
|
-
sleep(0.
|
41
|
+
sleep(0.5)
|
41
42
|
assert_equal "#{ip}:2222", service.leader.address
|
42
43
|
|
43
44
|
registrations.each(&:unregister)
|
44
|
-
sleep(0.
|
45
|
+
sleep(0.5)
|
45
46
|
assert_nil service.leader
|
46
47
|
end
|
47
48
|
|
48
49
|
def test_leader_changes
|
49
|
-
name = "
|
50
|
+
name = "leader-changes"
|
50
51
|
ip = "127.0.0.1"
|
51
52
|
|
52
53
|
service = @client.service(name)
|
@@ -55,23 +56,23 @@ class TestServiceLeader < DiscoverIntegrationTest
|
|
55
56
|
|
56
57
|
registrations = []
|
57
58
|
registrations << @client.register(name, 1111, ip)
|
58
|
-
sleep(0.
|
59
|
+
sleep(0.5)
|
59
60
|
assert_equal 1, watcher.leader_updates.size
|
60
61
|
assert_equal "#{ip}:1111", watcher.leader_updates.last.address
|
61
62
|
|
62
63
|
registrations << @client.register(name, 2222, ip)
|
63
64
|
registrations << @client.register(name, 3333, ip)
|
64
|
-
sleep(0.
|
65
|
+
sleep(0.5)
|
65
66
|
assert_equal 1, watcher.leader_updates.size
|
66
67
|
assert_equal "#{ip}:1111", watcher.leader_updates.last.address
|
67
68
|
|
68
69
|
registrations.shift.unregister
|
69
|
-
sleep(0.
|
70
|
+
sleep(0.5)
|
70
71
|
assert_equal 2, watcher.leader_updates.size
|
71
72
|
assert_equal "#{ip}:2222", watcher.leader_updates.last.address
|
72
73
|
|
73
74
|
registrations.each(&:unregister)
|
74
|
-
sleep(0.
|
75
|
+
sleep(0.5)
|
75
76
|
assert_equal 3, watcher.leader_updates.size
|
76
77
|
assert_equal "#{ip}:3333", watcher.leader_updates.last.address
|
77
78
|
end
|
@@ -20,24 +20,24 @@ class TestServiceUpdates < DiscoverIntegrationTest
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def test_registration_triggers_updates
|
23
|
-
name = "
|
23
|
+
name = "registration-updates"
|
24
24
|
ip = "127.0.0.1"
|
25
25
|
|
26
26
|
service = @client.service(name)
|
27
27
|
watcher = TestServiceWatcher.new(service)
|
28
28
|
|
29
29
|
@client.register name, 1111, ip
|
30
|
-
sleep(0.
|
30
|
+
sleep(0.5)
|
31
31
|
assert_equal 1, watcher.updates.size
|
32
32
|
assert_equal "#{ip}:1111", watcher.updates.last.address
|
33
33
|
|
34
34
|
@client.register name, 2222, ip
|
35
|
-
sleep(0.
|
35
|
+
sleep(0.5)
|
36
36
|
assert_equal 2, watcher.updates.size
|
37
37
|
assert_equal "#{ip}:2222", watcher.updates.last.address
|
38
38
|
|
39
39
|
@client.register name, 1111, ip, { "foo" => "bar" }
|
40
|
-
sleep(0.
|
40
|
+
sleep(0.5)
|
41
41
|
assert_equal 3, watcher.updates.size
|
42
42
|
assert_equal "#{ip}:1111", watcher.updates.last.address
|
43
43
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class TestClient < Minitest::Test
|
4
|
+
def setup
|
5
|
+
@rpc_client_class = RPCPlus.send(:remove_const, :Client)
|
6
|
+
RPCPlus.send(:const_set, :Client, Minitest::Mock.new)
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
RPCPlus.send(:remove_const, :Client)
|
11
|
+
RPCPlus.send(:const_set, :Client, @rpc_client_class)
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_explicit_address
|
15
|
+
address = "1.2.3.4:5678"
|
16
|
+
RPCPlus::Client.expect :new, nil, ["1.2.3.4", 5678]
|
17
|
+
Discover::Client.new(address)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_explicit_address_as_uri
|
21
|
+
address = "tcp://1.2.3.4:5678"
|
22
|
+
RPCPlus::Client.expect :new, nil, ["1.2.3.4", 5678]
|
23
|
+
Discover::Client.new(address)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_address_as_environment_variable
|
27
|
+
ENV["DISCOVERD"] = "tcp://5.6.7.8:4321"
|
28
|
+
RPCPlus::Client.expect :new, nil, ["5.6.7.8", 4321]
|
29
|
+
Discover::Client.new
|
30
|
+
ENV.delete "DISCOVERD"
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_default_address
|
34
|
+
RPCPlus::Client.expect :new, nil, ["127.0.0.1", 1111]
|
35
|
+
Discover::Client.new
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: discover
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonathan Rudenberg
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-02-
|
12
|
+
date: 2014-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: celluloid
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- test/integration/test_service_updates.rb
|
109
109
|
- test/support/discover_integration_test.rb
|
110
110
|
- test/test_helper.rb
|
111
|
+
- test/unit/test_client.rb
|
111
112
|
homepage: ''
|
112
113
|
licenses:
|
113
114
|
- BSD
|
@@ -138,3 +139,4 @@ test_files:
|
|
138
139
|
- test/integration/test_service_updates.rb
|
139
140
|
- test/support/discover_integration_test.rb
|
140
141
|
- test/test_helper.rb
|
142
|
+
- test/unit/test_client.rb
|