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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6e952beaea5ffcb83ae577c9b95a19c05c595eba
4
- data.tar.gz: dd8c6d2cc4c03a078e84024f14ff50695f3138e7
3
+ metadata.gz: 8e5f959700c606323c1b0b0748db67ac608ad9da
4
+ data.tar.gz: 1621c20260613bd508ae1623689cd29932923b5b
5
5
  SHA512:
6
- metadata.gz: 3f74fd998d606f0ea0aebecb4220cb9f05d7f138e47d34bb3bc88af5b23c37410f1330ef8e5c63d8660b852e01c50baa030ebbec1c7ffe96ff75b4b367fcdbcd
7
- data.tar.gz: 601a492a7bc6bf57ada768f1b0c614d26c3616cfa94113ef758a840b962e062931d82c4635bdbfc5bc978e6c530735e2113df9febd8a0e173279c01f4cc50846
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 by default"
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"
@@ -1,12 +1,14 @@
1
- require 'discover/version'
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(host='127.0.0.1', port=1111)
9
- @client = RPCPlus::Client.new(host, port)
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
@@ -1,3 +1,3 @@
1
1
  module Discover
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -26,7 +26,7 @@ class TestRegistration < DiscoverIntegrationTest
26
26
  end
27
27
 
28
28
  def test_service_is_online_after_registration
29
- name = "foo"
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 = "foo"
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 = "foo"
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 = "foo"
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 = "foo"
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.2)
117
+ sleep(0.5)
118
118
  assert !standby.elected?
119
119
 
120
120
  registrations << @client.register(name, 3333, ip)
121
- sleep(0.2)
121
+ sleep(0.5)
122
122
  assert !standby.elected?
123
123
 
124
124
  registrations.each(&:unregister)
125
- sleep(0.2)
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 = "foo"
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.2)
37
+ sleep(0.5)
37
38
  assert_equal "#{ip}:1111", service.leader.address
38
39
 
39
40
  registrations.shift.unregister
40
- sleep(0.2)
41
+ sleep(0.5)
41
42
  assert_equal "#{ip}:2222", service.leader.address
42
43
 
43
44
  registrations.each(&:unregister)
44
- sleep(0.2)
45
+ sleep(0.5)
45
46
  assert_nil service.leader
46
47
  end
47
48
 
48
49
  def test_leader_changes
49
- name = "foo"
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.2)
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.2)
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.2)
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.2)
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 = "foo"
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.2)
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.2)
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.2)
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.1
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-26 00:00:00.000000000 Z
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