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.
- checksums.yaml +4 -4
- data/MIT-LICENSE +19 -19
- data/README.md +9 -9
- data/app/models/active_event/event.rb +15 -15
- data/app/models/active_event/event_repository.rb +11 -11
- data/db/migrate/00_create_domain_events.rb +9 -9
- data/lib/active_event/autoload.rb +11 -9
- data/lib/active_event/command.rb +35 -39
- data/lib/active_event/domain.rb +4 -2
- data/lib/active_event/event_server.rb +72 -76
- data/lib/active_event/event_source_server.rb +149 -127
- data/lib/active_event/event_type.rb +29 -28
- data/lib/active_event/replay_server.rb +94 -98
- data/lib/active_event/sse.rb +26 -26
- data/lib/active_event/support/attr_initializer.rb +76 -74
- data/lib/active_event/support/attr_setter.rb +31 -29
- data/lib/active_event/support/autoload.rb +46 -44
- data/lib/active_event/support/autoloader.rb +41 -38
- data/lib/active_event/support/multi_logger.rb +31 -28
- data/lib/active_event/validations.rb +68 -68
- data/lib/active_event/validations_registry.rb +18 -18
- data/lib/active_event/version.rb +1 -1
- data/spec/factories/event_factory.rb +9 -9
- data/spec/lib/command_spec.rb +14 -14
- data/spec/lib/domain_spec.rb +21 -21
- data/spec/lib/event_server_spec.rb +29 -29
- data/spec/lib/event_type_spec.rb +38 -38
- data/spec/lib/replay_server_spec.rb +71 -68
- data/spec/lib/support/attr_initializer_spec.rb +55 -55
- data/spec/lib/support/attr_setter_spec.rb +61 -61
- data/spec/models/event_spec.rb +20 -20
- data/spec/spec_helper.rb +1 -1
- data/spec/support/active_record.rb +40 -38
- metadata +2 -4
- data/lib/active_event/support/hash_buffer.rb +0 -24
- data/lib/active_event/support/ring_buffer.rb +0 -20
data/lib/active_event/version.rb
CHANGED
@@ -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
|
data/spec/lib/command_spec.rb
CHANGED
@@ -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
|
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
|
data/spec/lib/domain_spec.rb
CHANGED
@@ -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(
|
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
|
data/spec/lib/event_type_spec.rb
CHANGED
@@ -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
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'can create events of any kind' do
|
15
|
-
expect
|
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.
|
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.
|
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(
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
expect(@event).to receive(:
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
expect(
|
63
|
-
expect(
|
64
|
-
@server.
|
65
|
-
@server.
|
66
|
-
|
67
|
-
|
68
|
-
|
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
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'works for defined attributes' do
|
15
|
-
expect
|
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.
|
39
|
-
@color.g.
|
40
|
-
@color.b.
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'can filter attributes' do
|
44
|
-
@color.attributes_except(:r).
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'can convert to hash' do
|
48
|
-
@color.to_hash.
|
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
|