active_event 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +19 -19
  3. data/README.md +9 -9
  4. data/app/models/active_event/event.rb +15 -15
  5. data/app/models/active_event/event_repository.rb +11 -11
  6. data/db/migrate/00_create_domain_events.rb +9 -9
  7. data/lib/active_event/autoload.rb +11 -9
  8. data/lib/active_event/command.rb +35 -39
  9. data/lib/active_event/domain.rb +4 -2
  10. data/lib/active_event/event_server.rb +72 -76
  11. data/lib/active_event/event_source_server.rb +149 -127
  12. data/lib/active_event/event_type.rb +29 -28
  13. data/lib/active_event/replay_server.rb +94 -98
  14. data/lib/active_event/sse.rb +26 -26
  15. data/lib/active_event/support/attr_initializer.rb +76 -74
  16. data/lib/active_event/support/attr_setter.rb +31 -29
  17. data/lib/active_event/support/autoload.rb +46 -44
  18. data/lib/active_event/support/autoloader.rb +41 -38
  19. data/lib/active_event/support/multi_logger.rb +31 -28
  20. data/lib/active_event/validations.rb +68 -68
  21. data/lib/active_event/validations_registry.rb +18 -18
  22. data/lib/active_event/version.rb +1 -1
  23. data/spec/factories/event_factory.rb +9 -9
  24. data/spec/lib/command_spec.rb +14 -14
  25. data/spec/lib/domain_spec.rb +21 -21
  26. data/spec/lib/event_server_spec.rb +29 -29
  27. data/spec/lib/event_type_spec.rb +38 -38
  28. data/spec/lib/replay_server_spec.rb +71 -68
  29. data/spec/lib/support/attr_initializer_spec.rb +55 -55
  30. data/spec/lib/support/attr_setter_spec.rb +61 -61
  31. data/spec/models/event_spec.rb +20 -20
  32. data/spec/spec_helper.rb +1 -1
  33. data/spec/support/active_record.rb +40 -38
  34. metadata +2 -4
  35. data/lib/active_event/support/hash_buffer.rb +0 -24
  36. data/lib/active_event/support/ring_buffer.rb +0 -20
@@ -1,7 +1,7 @@
1
1
  module ActiveEvent
2
2
  # Returns the version of the currently loaded ActiveEvent as a Gem::Version
3
3
  def self.version
4
- Gem::Version.new '0.5.2'
4
+ Gem::Version.new '0.5.3'
5
5
  end
6
6
 
7
7
  module VERSION #:nodoc:
@@ -1,9 +1,9 @@
1
- require 'factory_girl'
2
-
3
- FactoryGirl.define do
4
- factory :event, class: ActiveEvent::Event do
5
- event 'TestEvent'
6
- data name: 'Hello', r: 10, g: 20, b: 30
7
- created_at DateTime.new(2012,12,24,11,11,11)
8
- end
9
- end
1
+ require 'factory_girl'
2
+
3
+ FactoryGirl.define do
4
+ factory :event, class: ActiveEvent::Event do
5
+ event 'TestEvent'
6
+ data name: 'Hello', r: 10, g: 20, b: 30
7
+ created_at DateTime.new(2012, 12, 24, 11, 11, 11)
8
+ end
9
+ end
@@ -1,14 +1,14 @@
1
- require_relative '../spec_helper'
2
-
3
- describe ActiveEvent::Command do
4
- class TestCommand
5
- include ActiveEvent::Command
6
- attributes :r, :g, :b
7
- end
8
-
9
- describe 'create instance' do
10
- it 'can create an instance' do
11
- expect { TestCommand.new r: 10 }.to be
12
- end
13
- end
14
- end
1
+ require_relative '../spec_helper'
2
+
3
+ describe ActiveEvent::Command do
4
+ class TestCommand
5
+ include ActiveEvent::Command
6
+ attributes :r, :g, :b
7
+ end
8
+
9
+ describe 'create instance' do
10
+ it 'can create an instance' do
11
+ expect(TestCommand.new r: 10).to be
12
+ end
13
+ end
14
+ end
@@ -1,21 +1,21 @@
1
- require_relative '../spec_helper'
2
-
3
- describe ActiveEvent::Domain do
4
- class TestDomain
5
- include ActiveEvent::Domain
6
- end
7
-
8
- describe 'connect and run command' do
9
- before :each do
10
- @command = Object.new
11
- @drb_object = double 'drb_object'
12
- end
13
-
14
- it 'sends command over instance' do
15
- expect(DRbObject).to receive(:new_with_uri).with(TestDomain.server_uri).and_return(@drb_object)
16
- expect(@command).to receive(:valid?).and_return(true)
17
- expect(@drb_object).to receive(:run_command).with(@command)
18
- TestDomain.run_command(@command)
19
- end
20
- end
21
- end
1
+ require_relative '../spec_helper'
2
+
3
+ describe ActiveEvent::Domain do
4
+ class TestDomain
5
+ include ActiveEvent::Domain
6
+ end
7
+
8
+ describe 'connect and run command' do
9
+ before :each do
10
+ @command = Object.new
11
+ @drb_object = double 'drb_object'
12
+ end
13
+
14
+ it 'sends command over instance' do
15
+ expect(DRbObject).to receive(:new_with_uri).with(TestDomain.server_uri).and_return(@drb_object)
16
+ expect(@command).to receive(:valid?).and_return(true)
17
+ expect(@drb_object).to receive(:run_command).with(@command)
18
+ TestDomain.run_command(@command)
19
+ end
20
+ end
21
+ end
@@ -1,29 +1,29 @@
1
- require_relative '../spec_helper'
2
- describe ActiveEvent::EventServer do
3
- class TestEvent
4
- end
5
- before :all do
6
- @server = ActiveEvent::EventServer.instance
7
- @event = TestEvent.new
8
- end
9
- describe 'resend_events_after' do
10
- it 'starts the replay server if its not running' do
11
- expect(ActiveEvent::ReplayServer).to receive(:start)
12
- @server.resend_events_after(1).join
13
- end
14
-
15
- it 'updates the replay server if its running' do
16
- @server.instance_variable_set(:@replay_server_thread, Thread.new {sleep 1})
17
- expect(ActiveEvent::ReplayServer).to receive(:update)
18
- @server.resend_events_after 1
19
- end
20
- end
21
-
22
- it 'publishes an event' do
23
- allow(@server).to receive(:event_exchange).and_return(Object)
24
- expect(@server.event_exchange).to receive(:publish).with("Test2", {:type => "TestEvent", :headers => "Test"})
25
- expect(@event).to receive(:store_infos).and_return('Test')
26
- expect(@event).to receive(:to_json).and_return('Test2')
27
- @server.class.publish(@event)
28
- end
29
- end
1
+ require_relative '../spec_helper'
2
+ describe ActiveEvent::EventServer do
3
+ class TestEvent
4
+ end
5
+ before :all do
6
+ @server = ActiveEvent::EventServer.instance
7
+ @event = TestEvent.new
8
+ end
9
+ describe 'resend_events_after' do
10
+ it 'starts the replay server if its not running' do
11
+ expect(ActiveEvent::ReplayServer).to receive(:start)
12
+ @server.resend_events_after(1).join
13
+ end
14
+
15
+ it 'updates the replay server if its running' do
16
+ @server.instance_variable_set(:@replay_server_thread, Thread.new { sleep 1 })
17
+ expect(ActiveEvent::ReplayServer).to receive(:update)
18
+ @server.resend_events_after 1
19
+ end
20
+ end
21
+
22
+ it 'publishes an event' do
23
+ allow(@server).to receive(:event_exchange).and_return(Object)
24
+ expect(@server.event_exchange).to receive(:publish).with('Test2', type: 'TestEvent', headers: 'Test')
25
+ expect(@event).to receive(:store_infos).and_return('Test')
26
+ expect(@event).to receive(:to_json).and_return('Test2')
27
+ @server.class.publish(@event)
28
+ end
29
+ end
@@ -1,38 +1,38 @@
1
- require_relative '../spec_helper'
2
-
3
- describe ActiveEvent::EventType do
4
- class RgbColorEvent
5
- include ActiveEvent::EventType
6
- attributes :r, :g, :b
7
- end
8
-
9
- describe 'create instance' do
10
- it 'can create an instance' do
11
- expect { ActiveEvent::EventType.create_instance :RgbColorEvent, r: 10 }.to be
12
- end
13
-
14
- it 'can create events of any kind' do
15
- expect { ActiveEvent::EventType.create_instance :RandomEvent, r: 10 }.to be
16
- end
17
- end
18
-
19
- describe 'regular instance' do
20
- before :all do
21
- @color = RgbColorEvent.new r: 10, g: 20, b: 30
22
- end
23
-
24
- it 'can report type' do
25
- @color.event_type.should == 'RgbColorEvent'
26
- end
27
- end
28
-
29
- describe 'unknown instance' do
30
- before :all do
31
- @random = ActiveEvent::EventType.create_instance :RandomEvent, r: 10
32
- end
33
-
34
- it 'can report type' do
35
- @random.event_type.should == 'RandomEvent'
36
- end
37
- end
38
- end
1
+ require_relative '../spec_helper'
2
+
3
+ describe ActiveEvent::EventType do
4
+ class RgbColorEvent
5
+ include ActiveEvent::EventType
6
+ attributes :r, :g, :b
7
+ end
8
+
9
+ describe 'create instance' do
10
+ it 'can create an instance' do
11
+ expect(ActiveEvent::EventType.create_instance :RgbColorEvent, r: 10).to be
12
+ end
13
+
14
+ it 'can create events of any kind' do
15
+ expect(ActiveEvent::EventType.create_instance :RandomEvent, r: 10).to be
16
+ end
17
+ end
18
+
19
+ describe 'regular instance' do
20
+ before :all do
21
+ @color = RgbColorEvent.new r: 10, g: 20, b: 30
22
+ end
23
+
24
+ it 'can report type' do
25
+ expect(@color.event_type).to eq('RgbColorEvent')
26
+ end
27
+ end
28
+
29
+ describe 'unknown instance' do
30
+ before :all do
31
+ @random = ActiveEvent::EventType.create_instance :RandomEvent, r: 10
32
+ end
33
+
34
+ it 'can report type' do
35
+ expect(@random.event_type).to eq('RandomEvent')
36
+ end
37
+ end
38
+ end
@@ -1,68 +1,71 @@
1
- require_relative '../spec_helper'
2
- describe ActiveEvent::ReplayServer do
3
- class TestEvent
4
- end
5
- before :all do
6
- @server = ActiveEvent::ReplayServer.instance
7
- @event = TestEvent.new
8
- end
9
-
10
- it 'republishes an event' do
11
- date = DateTime.now.utc
12
- allow(@server).to receive(:resend_exchange).and_return(Object)
13
- expect(@server.resend_exchange).to receive(:publish).with("{\"bla\":\"Test2\"}", {:type => "TestEvent", :headers => {id: 0, created_at: date.to_s, replayed: true}})
14
- expect(@event).to receive(:event).and_return(@event.class.name)
15
- expect(@event).to receive(:data).and_return(bla: 'Test2')
16
- expect(@event).to receive(:id).and_return(0)
17
- expect(@event).to receive(:created_at).and_return(date.to_s)
18
- @server.republish(@event)
19
- end
20
-
21
- it 'starts and stop republishing events' do
22
- expect(ActiveEvent::EventRepository).to receive(:after_id).and_return([1, 2, 3, 4], [])
23
- expect(@server).to receive(:republish_events).once
24
- @server.start_republishing
25
- end
26
-
27
- it 'republish all found events' do
28
- @server.instance_variable_set(:@events, [1, 2, 3, 4])
29
- expect(Thread).to receive(:pass).exactly(4).times
30
- expect(@server).to receive(:next_event).exactly(4).times { @server.instance_variable_get(:@events).shift }
31
- expect(@server).to receive(:republish).exactly(4).times
32
- @server.republish_events
33
- end
34
-
35
- describe 'new_id?' do
36
- before :each do
37
- @server.instance_variable_set(:@last_id, 4)
38
- end
39
-
40
- it 'overrides id if smaller' do
41
- @server.queue << 1
42
- expect(@server.new_id?).to be_true
43
- expect(@server.instance_variable_get(:@last_id)).to eq 1
44
- end
45
-
46
- it 'does nothing if id is greater' do
47
- @server.queue << 5
48
- expect(@server.new_id?).to be_false
49
- expect(@server.instance_variable_get(:@last_id)).not_to eq 5
50
- end
51
-
52
- it 'does nothing if queue is empty' do
53
- @server.queue.clear
54
- expect(@server.new_id?).to be_false
55
- end
56
- end
57
-
58
- it 'can update start id while running' do
59
- expect(ActiveEvent::EventRepository).to receive(:after_id).with(1).and_return([2, 3, 4], [])
60
- expect(ActiveEvent::EventRepository).to receive(:after_id).with(2).and_return([3, 4])
61
- expect(@server).to receive(:next_event).exactly(3).times { @server.instance_variable_get(:@events).shift }
62
- expect(@server).to receive(:republish).exactly(3).times
63
- expect(Thread).to receive(:pass).exactly(3).times
64
- @server.instance_variable_set(:@last_id, 2)
65
- @server.queue << 1
66
- @server.start_republishing
67
- end
68
- end
1
+ require_relative '../spec_helper'
2
+ describe ActiveEvent::ReplayServer do
3
+ class TestEvent
4
+ end
5
+ before :all do
6
+ @server = ActiveEvent::ReplayServer.instance
7
+ @event = TestEvent.new
8
+ end
9
+
10
+ it 'republishes an event' do
11
+ date = DateTime.now.utc
12
+ allow(@server).to receive(:resend_exchange).and_return(Object)
13
+ expect(@server.resend_exchange).to receive(:publish).with('{"bla":"Test2"}',
14
+ type: 'TestEvent',
15
+ headers: {id: 0, created_at: date.to_s, replayed: true},
16
+ )
17
+ expect(@event).to receive(:event).and_return(@event.class.name)
18
+ expect(@event).to receive(:data).and_return(bla: 'Test2')
19
+ expect(@event).to receive(:id).and_return(0)
20
+ expect(@event).to receive(:created_at).and_return(date.to_s)
21
+ @server.republish(@event)
22
+ end
23
+
24
+ it 'starts and stop republishing events' do
25
+ expect(ActiveEvent::EventRepository).to receive(:after_id).and_return([1, 2, 3, 4], [])
26
+ expect(@server).to receive(:republish_events).once
27
+ @server.start_republishing
28
+ end
29
+
30
+ it 'republish all found events' do
31
+ @server.instance_variable_set(:@events, [1, 2, 3, 4])
32
+ expect(Thread).to receive(:pass).exactly(4).times
33
+ expect(@server).to receive(:next_event).exactly(4).times { @server.instance_variable_get(:@events).shift }
34
+ expect(@server).to receive(:republish).exactly(4).times
35
+ @server.republish_events
36
+ end
37
+
38
+ describe 'new_id?' do
39
+ before :each do
40
+ @server.instance_variable_set(:@last_id, 4)
41
+ end
42
+
43
+ it 'overrides id if smaller' do
44
+ @server.queue << 1
45
+ expect(@server.new_id?).to be_truthy
46
+ expect(@server.instance_variable_get(:@last_id)).to eq 1
47
+ end
48
+
49
+ it 'does nothing if id is greater' do
50
+ @server.queue << 5
51
+ expect(@server.new_id?).to be_falsy
52
+ expect(@server.instance_variable_get(:@last_id)).not_to eq 5
53
+ end
54
+
55
+ it 'does nothing if queue is empty' do
56
+ @server.queue.clear
57
+ expect(@server.new_id?).to be_falsy
58
+ end
59
+ end
60
+
61
+ it 'can update start id while running' do
62
+ expect(ActiveEvent::EventRepository).to receive(:after_id).with(1).and_return([2, 3, 4], [])
63
+ expect(ActiveEvent::EventRepository).to receive(:after_id).with(2).and_return([3, 4])
64
+ expect(@server).to receive(:next_event).exactly(3).times { @server.instance_variable_get(:@events).shift }
65
+ expect(@server).to receive(:republish).exactly(3).times
66
+ expect(Thread).to receive(:pass).exactly(3).times
67
+ @server.instance_variable_set(:@last_id, 2)
68
+ @server.queue << 1
69
+ @server.start_republishing
70
+ end
71
+ end
@@ -1,55 +1,55 @@
1
- require_relative '../../spec_helper'
2
-
3
- describe ActiveEvent::Support::AttrInitializer do
4
- class RgbColor
5
- include ActiveEvent::Support::AttrInitializer
6
- attributes :r, :g, :b
7
- end
8
-
9
- describe 'constructor' do
10
- it 'works for no args' do
11
- expect { RgbColor.new }.to be
12
- end
13
-
14
- it 'works for defined attributes' do
15
- expect { RgbColor.new r: 10, g: 20 }.to be
16
- end
17
-
18
- it 'fails for undeclared attributes' do
19
- expect { RgbColor.new z: 10 }.to raise_error(ActiveEvent::Support::UnknownAttributeError)
20
- end
21
-
22
- it 'fails for unauthorized hash' do
23
- class CheckedHash < Hash
24
- def permitted?
25
- false
26
- end
27
- end
28
- expect { RgbColor.new(CheckedHash.new.merge! r: 1) }.to raise_error(ActiveEvent::Support::ForbiddenAttributesError)
29
- end
30
- end
31
-
32
- describe 'instance' do
33
- before :all do
34
- @color = RgbColor.new r: 10, g: 20
35
- end
36
-
37
- it 'can retrieve attributes' do
38
- @color.r.should == 10
39
- @color.g.should == 20
40
- @color.b.should be_nil
41
- end
42
-
43
- it 'can filter attributes' do
44
- @color.attributes_except(:r).should eq(g: 20)
45
- end
46
-
47
- it 'can convert to hash' do
48
- @color.to_hash.should eq(r: 10, g: 20)
49
- end
50
-
51
- it 'is read only' do
52
- expect { @color.r = 20 }.to raise_error
53
- end
54
- end
55
- end
1
+ require_relative '../../spec_helper'
2
+
3
+ describe ActiveEvent::Support::AttrInitializer do
4
+ class RgbColor
5
+ include ActiveEvent::Support::AttrInitializer
6
+ attributes :r, :g, :b
7
+ end
8
+
9
+ describe 'constructor' do
10
+ it 'works for no args' do
11
+ expect(RgbColor.new).to be
12
+ end
13
+
14
+ it 'works for defined attributes' do
15
+ expect(RgbColor.new r: 10, g: 20).to be
16
+ end
17
+
18
+ it 'fails for undeclared attributes' do
19
+ expect { RgbColor.new z: 10 }.to raise_error(ActiveEvent::Support::UnknownAttributeError)
20
+ end
21
+
22
+ it 'fails for unauthorized hash' do
23
+ class CheckedHash < Hash
24
+ def permitted?
25
+ false
26
+ end
27
+ end
28
+ expect { RgbColor.new(CheckedHash.new.merge! r: 1) }.to raise_error(ActiveEvent::Support::ForbiddenAttributesError)
29
+ end
30
+ end
31
+
32
+ describe 'instance' do
33
+ before :all do
34
+ @color = RgbColor.new r: 10, g: 20
35
+ end
36
+
37
+ it 'can retrieve attributes' do
38
+ expect(@color.r).to eq(10)
39
+ expect(@color.g).to eq(20)
40
+ expect(@color.b).to be_nil
41
+ end
42
+
43
+ it 'can filter attributes' do
44
+ expect(@color.attributes_except(:r)).to eq(g: 20)
45
+ end
46
+
47
+ it 'can convert to hash' do
48
+ expect(@color.to_hash).to eq(r: 10, g: 20)
49
+ end
50
+
51
+ it 'is read only' do
52
+ expect { @color.r = 20 }.to raise_error
53
+ end
54
+ end
55
+ end