etcd-discovery 0.0.12 → 0.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe29a8729a9678c7983c4cabb8f7b6cb9e33d460
4
- data.tar.gz: 9111832380a0338312a6cbef11bead94fcd414d9
3
+ metadata.gz: 9c01bf46ee0a9c4812bf9ac80fa816b6b5bcd541
4
+ data.tar.gz: 66c258afe4d3387737216e9c26fc7f1522d92d4e
5
5
  SHA512:
6
- metadata.gz: 2140fc22e32b13b396c14f12bfb45f4a58babfe4c77529976d69ea6f99e0e7ae2ddcd6d2a9448adef66033e0a2bf919a4146a16e4060c4713eca8ffe3d0e8d45
7
- data.tar.gz: 8625911eb2fdd877ab5d125673a91bb59adf7fb7587ce2d843417e2a135324f153b76188cd712b37b4bb1e05c1dbda942f6328deeb1bf7cc620cca574baf554f
6
+ metadata.gz: 4253017c7feb7d976bb37d0e0e81512decb5a10f4c56ce19b888fb11d41106a9e77d2fb7fbd7f9316f4962685078bbd2533c631dfb372ba96766638cadfd169a
7
+ data.tar.gz: c9d54aa78d9a5beeaa846a8a0fd1dab8ea9d14ea8fac04490d2468f1303e405ad39c46646c8e6e425fcfc43fdb042bae3ea36b81ccacefb6d2781fa75d5d974a
@@ -1,4 +1,15 @@
1
1
  module EtcdDiscovery
2
+ class InvalidStateError < StandardError
3
+ def initialize(current, expected)
4
+ @current = current
5
+ @expected = expected
6
+ end
7
+
8
+ def message
9
+ "Registrar is #{@current}, expected #{@expected}"
10
+ end
11
+ end
12
+
2
13
  class Registrar
3
14
  attr_reader :state
4
15
  attr_reader :thread
@@ -21,27 +32,41 @@ module EtcdDiscovery
21
32
 
22
33
  def register
23
34
  @state = :started
24
- config = EtcdDiscovery.config
25
- client = config.client
26
35
  value = @host.to_json
27
- key_name = "/services/#{@service}/#{@host.attributes['name']}"
28
36
 
29
37
  @thread = Thread.new {
38
+ @logger.warn "Register '#{@service}' started"
30
39
  while @state == :started
31
40
  begin
32
- client.set(key_name, value: value, ttl: config.register_ttl)
41
+ client.set(key, value: value, ttl: config.register_ttl)
33
42
  rescue => e
34
- logger.warn "Fail to set #{key_name}: #{e}, #{e.message}, #{e.class}"
43
+ @logger.warn "Fail to set #{key}: #{e}, #{e.message}, #{e.class}"
35
44
  end
36
45
  sleep config.register_renew
37
46
  end
38
- logger.warn "Register '#{@service}' stopped"
47
+ @logger.warn "Register '#{@service}' stopped"
39
48
  }
40
49
  self
41
50
  end
42
51
 
43
52
  def stop
53
+ raise InvalidStateError.new(@state, :started) if @state != :started
54
+ @logger.debug "Set state to :stopped"
44
55
  @state = :stopped
56
+ @logger.debug "Delete #{key}"
57
+ client.delete(key)
58
+ end
59
+
60
+ def client
61
+ config.client
62
+ end
63
+
64
+ def config
65
+ EtcdDiscovery.config
66
+ end
67
+
68
+ def key
69
+ "/services/#{@service}/#{@host.attributes['name']}"
45
70
  end
46
71
  end
47
72
  end
@@ -1,43 +1,62 @@
1
1
  require 'spec_helper.rb'
2
2
 
3
3
  RSpec.describe EtcdDiscovery::Registrar do
4
- describe '.new' do
5
- context 'with a Hash' do
6
- subject { EtcdDiscovery::Registrar.new "service", {"name" => "example.com", "ports" => {"http" => 80}} }
7
- it "should transform the has in Host" do
8
- expect(subject.host.attributes["name"]).to eq "example.com"
4
+ context "without a running client" do
5
+ describe '.new' do
6
+ context 'with a Hash' do
7
+ subject { EtcdDiscovery::Registrar.new "service", {"name" => "example.com", "ports" => {"http" => 80}} }
8
+ it "should transform the has in Host" do
9
+ expect(subject.host.attributes["name"]).to eq "example.com"
10
+ end
9
11
  end
10
- end
11
12
 
12
- context 'with a EtcdDiscovery::Host' do
13
- subject { EtcdDiscovery::Registrar.new "service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}}) }
14
- it "should defines the state to new" do
15
- expect(subject.state).to eq :new
13
+ context 'with a EtcdDiscovery::Host' do
14
+ subject { EtcdDiscovery::Registrar.new "service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}}) }
15
+ it "should defines the state to new" do
16
+ expect(subject.state).to eq :new
17
+ end
16
18
  end
17
19
  end
18
20
  end
19
21
 
20
- describe '#register' do
22
+ context "with an unregistered client" do
21
23
  subject { EtcdDiscovery::Registrar.new "service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}}) }
22
- it "should start a registering thread" do
23
- subject.register
24
- expect(subject.thread).not_to eq nil
25
- expect(subject.thread.alive?).to eq true
24
+
25
+ describe '#stop' do
26
+ it "should raise an exception if service not registered" do
27
+ expect{ subject.stop }.to raise_exception EtcdDiscovery::InvalidStateError
28
+ end
26
29
  end
30
+
31
+ its(:client) { is_expected.to eq EtcdDiscovery.config.client }
32
+ its(:key) { is_expected.to eq "/services/service/example.com" }
27
33
  end
28
34
 
29
- describe '#stop' do
30
- subject { EtcdDiscovery::Registrar.new "service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}}) }
31
- it "should change the state to stopped" do
32
- subject.stop
33
- expect(subject.state).to eq :stopped
35
+ context "with a registered client" do
36
+ subject { EtcdDiscovery::Registrar.new("service", EtcdDiscovery::Host.new({"name" => "example.com", "ports" => {"http" => 80}})) }
37
+
38
+ before(:each) do subject.register ; sleep 0.2 ; end
39
+ after(:each) do subject.stop if subject.state == :started ; end
40
+
41
+ describe '#register' do
42
+ its(:thread) { is_expected.not_to eq nil }
43
+ it "s thread should be alived" do
44
+ expect(subject.thread.alive?).to eq true
45
+ end
34
46
  end
35
47
 
36
- it "should stop its registering thread" do
37
- subject.register
38
- subject.stop
39
- sleep 1
40
- expect(subject.thread.alive?).to eq false
48
+ describe "#stop" do
49
+ it "should stop its registering thread" do
50
+ subject.stop
51
+ sleep 0.2
52
+ expect(subject.thread.alive?).to eq false
53
+ end
54
+
55
+ it "should remote the etcd key of the service" do
56
+ expect(EtcdDiscovery.get("service").length).to eq 1
57
+ subject.stop
58
+ expect { EtcdDiscovery.get("service") }.to raise_exception EtcdDiscovery::ServiceNotFound
59
+ end
41
60
  end
42
61
  end
43
62
  end
data/spec/spec_helper.rb CHANGED
@@ -4,5 +4,5 @@ require "rubygems"
4
4
  require "bundler/setup"
5
5
  Bundler.require(:default, :development, :test)
6
6
 
7
- EtcdDiscovery.config.register_renew = 0.5
7
+ EtcdDiscovery.config.register_renew = 0.1
8
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: etcd-discovery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Léo Unbekandt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-07 00:00:00.000000000 Z
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: etcd