synapse-core 0.4.0 → 0.5.1
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.
- data/lib/synapse/command/async_command_bus.rb +41 -0
- data/lib/synapse/command/command_callback.rb +49 -2
- data/lib/synapse/command/command_handler.rb +2 -0
- data/lib/synapse/command/filters/validation.rb +2 -2
- data/lib/synapse/command/gateway/interval_retry_scheduler.rb +75 -0
- data/lib/synapse/command/gateway/retry_scheduler.rb +26 -0
- data/lib/synapse/command/gateway/retrying_callback.rb +46 -0
- data/lib/synapse/command/gateway.rb +56 -11
- data/lib/synapse/command/interceptor_chain.rb +2 -2
- data/lib/synapse/command/interceptors/serialization.rb +2 -2
- data/lib/synapse/command/message.rb +16 -0
- data/lib/synapse/command/simple_command_bus.rb +6 -2
- data/lib/synapse/command/wiring.rb +3 -3
- data/lib/synapse/command.rb +7 -1
- data/lib/synapse/common/concurrency/identifier_lock.rb +2 -17
- data/lib/synapse/common/concurrency/public_lock.rb +8 -9
- data/lib/synapse/common/errors.rb +1 -1
- data/lib/synapse/common/message.rb +2 -6
- data/lib/synapse/common.rb +9 -0
- data/lib/synapse/configuration/component/command_bus/async_command_bus.rb +41 -0
- data/lib/synapse/configuration/component/command_bus/gateway.rb +38 -0
- data/lib/synapse/configuration/component/command_bus/simple_command_bus.rb +81 -0
- data/lib/synapse/configuration/component/command_bus.rb +35 -0
- data/lib/synapse/configuration/component/event_bus/simple_event_bus.rb +41 -0
- data/lib/synapse/configuration/component/event_bus.rb +15 -0
- data/lib/synapse/configuration/component/event_sourcing/repository.rb +62 -0
- data/lib/synapse/configuration/component/event_sourcing.rb +15 -0
- data/lib/synapse/configuration/component/repository/locking_repository.rb +85 -0
- data/lib/synapse/configuration/component/repository/simple_repository.rb +31 -0
- data/lib/synapse/configuration/component/repository.rb +15 -0
- data/lib/synapse/configuration/component/serialization/converter_factory.rb +50 -0
- data/lib/synapse/configuration/component/serialization/serializer.rb +90 -0
- data/lib/synapse/configuration/component/serialization.rb +25 -0
- data/lib/synapse/configuration/component/uow/unit_factory.rb +51 -0
- data/lib/synapse/configuration/component/uow.rb +22 -0
- data/lib/synapse/configuration/component/upcasting/upcaster_chain.rb +61 -0
- data/lib/synapse/configuration/component/upcasting.rb +15 -0
- data/lib/synapse/configuration/container.rb +80 -0
- data/lib/synapse/configuration/container_builder.rb +108 -0
- data/lib/synapse/configuration/definition.rb +31 -0
- data/lib/synapse/configuration/definition_builder.rb +138 -0
- data/lib/synapse/configuration/dependent.rb +31 -0
- data/lib/synapse/configuration/ext.rb +35 -0
- data/lib/synapse/configuration.rb +32 -0
- data/lib/synapse/domain/aggregate_root.rb +25 -26
- data/lib/synapse/domain/event_container.rb +5 -5
- data/lib/synapse/domain/message.rb +16 -0
- data/lib/synapse/event_bus/event_listener.rb +2 -0
- data/lib/synapse/event_bus/event_publisher.rb +19 -0
- data/lib/synapse/event_bus/simple_event_bus.rb +21 -30
- data/lib/synapse/event_bus/wiring.rb +5 -4
- data/lib/synapse/event_bus.rb +1 -1
- data/lib/synapse/event_sourcing/aggregate_root.rb +6 -4
- data/lib/synapse/process_manager/container_resource_injector.rb +18 -0
- data/lib/synapse/process_manager/pessimistic_lock_manager.rb +1 -1
- data/lib/synapse/process_manager/process.rb +4 -0
- data/lib/synapse/process_manager/process_factory.rb +3 -3
- data/lib/synapse/process_manager/repository/in_memory.rb +1 -1
- data/lib/synapse/process_manager/wiring/process.rb +6 -6
- data/lib/synapse/process_manager/wiring/process_manager.rb +8 -8
- data/lib/synapse/process_manager.rb +2 -0
- data/lib/synapse/repository/locking.rb +5 -3
- data/lib/synapse/repository/optimistic_lock_manager.rb +2 -7
- data/lib/synapse/repository/pessimistic_lock_manager.rb +3 -3
- data/lib/synapse/repository/repository.rb +2 -2
- data/lib/synapse/repository/simple_repository.rb +69 -0
- data/lib/synapse/repository.rb +1 -0
- data/lib/synapse/serialization/converter/chain.rb +2 -2
- data/lib/synapse/serialization/converter_factory.rb +2 -2
- data/lib/synapse/serialization/lazy_object.rb +2 -2
- data/lib/synapse/serialization/message/serialization_aware_message.rb +3 -3
- data/lib/synapse/serialization/message/serialized_message.rb +6 -10
- data/lib/synapse/serialization/message/serialized_message_builder.rb +4 -4
- data/lib/synapse/serialization/message/serialized_object_cache.rb +2 -2
- data/lib/synapse/serialization/message/serializer.rb +2 -2
- data/lib/synapse/serialization/revision_resolver.rb +3 -3
- data/lib/synapse/serialization/serialized_object.rb +2 -2
- data/lib/synapse/serialization/serialized_type.rb +2 -2
- data/lib/synapse/serialization/serializer/attribute.rb +2 -2
- data/lib/synapse/serialization/serializer/marshal.rb +2 -2
- data/lib/synapse/serialization/serializer/oj.rb +2 -2
- data/lib/synapse/serialization/serializer/ox.rb +2 -2
- data/lib/synapse/serialization/serializer.rb +2 -2
- data/lib/synapse/uow/factory.rb +2 -2
- data/lib/synapse/uow/listener_collection.rb +2 -2
- data/lib/synapse/uow/nesting.rb +9 -2
- data/lib/synapse/uow/provider.rb +2 -2
- data/lib/synapse/uow/uow.rb +8 -2
- data/lib/synapse/upcasting/{chain.rb → upcaster_chain.rb} +0 -0
- data/lib/synapse/upcasting.rb +1 -1
- data/lib/synapse/version.rb +1 -1
- data/lib/synapse/wiring/message_wiring.rb +31 -0
- data/lib/synapse.rb +2 -14
- data/test/auditing/data_provider_test.rb +2 -2
- data/test/auditing/dispatch_interceptor_test.rb +1 -1
- data/test/auditing/unit_listener_test.rb +3 -3
- data/test/command/async_command_bus_test.rb +49 -0
- data/test/command/duplication_test.rb +2 -2
- data/test/command/gateway/interval_retry_scheduler_test.rb +42 -0
- data/test/command/gateway/retrying_callback_test.rb +57 -0
- data/test/command/gateway_test.rb +41 -7
- data/test/command/interceptor_chain_test.rb +1 -1
- data/test/command/message_test.rb +17 -0
- data/test/command/serialization_test.rb +2 -2
- data/test/command/simple_command_bus_test.rb +7 -7
- data/test/command/validation_test.rb +3 -3
- data/test/command/wiring_test.rb +3 -3
- data/test/common/concurrency/identifier_lock_test.rb +2 -13
- data/test/common/concurrency/public_lock_test.rb +6 -6
- data/test/{duplication_test.rb → common/duplication_test.rb} +3 -3
- data/test/configuration/component/command_bus/async_command_bus_test.rb +36 -0
- data/test/configuration/component/command_bus/simple_command_bus_test.rb +57 -0
- data/test/configuration/component/event_bus/simple_event_bus_test.rb +58 -0
- data/test/configuration/component/serialization/converter_factory_test.rb +48 -0
- data/test/configuration/component/serialization/serializer_test.rb +78 -0
- data/test/configuration/component/uow/unit_factory_test.rb +46 -0
- data/test/configuration/container_builder_test.rb +47 -0
- data/test/configuration/container_test.rb +88 -0
- data/test/configuration/definition_builder_test.rb +126 -0
- data/test/configuration/definition_test.rb +41 -0
- data/test/configuration/dependent_test.rb +30 -0
- data/test/configuration/ext_test.rb +19 -0
- data/test/configuration/fixtures/dependent.rb +10 -0
- data/test/domain/aggregate_root_test.rb +5 -5
- data/test/domain/message_test.rb +15 -3
- data/test/domain/stream_test.rb +2 -2
- data/test/event_bus/publisher_test.rb +29 -0
- data/test/event_bus/wiring_test.rb +1 -1
- data/test/event_sourcing/aggregate_factory_test.rb +12 -6
- data/test/event_sourcing/aggregate_root_test.rb +4 -4
- data/test/event_sourcing/entity_test.rb +10 -9
- data/test/event_sourcing/repository_test.rb +6 -6
- data/test/event_sourcing/storage_listener_test.rb +8 -4
- data/test/event_store/in_memory_test.rb +3 -3
- data/test/process_manager/container_resource_injector_test.rb +19 -0
- data/test/process_manager/correlation_set_test.rb +2 -2
- data/test/process_manager/correlation_test.rb +2 -2
- data/test/process_manager/in_memory_test.rb +3 -3
- data/test/process_manager/process_factory_test.rb +2 -2
- data/test/process_manager/process_test.rb +3 -3
- data/test/process_manager/simple_process_manager_test.rb +5 -5
- data/test/process_manager/wiring/process_manager_test.rb +4 -4
- data/test/process_manager/wiring/process_test.rb +2 -2
- data/test/repository/locking_test.rb +4 -4
- data/test/repository/optimistic_test.rb +2 -2
- data/test/repository/pessimistic_test.rb +1 -1
- data/test/repository/simple_repository_test.rb +79 -0
- data/test/support/countdown_latch.rb +18 -0
- data/test/test_helper.rb +6 -3
- data/test/upcasting/data_test.rb +31 -0
- metadata +84 -25
- data/lib/synapse/event_bus/event_listener_proxy.rb +0 -12
- data/lib/synapse/partitioning/memory_queue_reader.rb +0 -31
- data/lib/synapse/partitioning/memory_queue_writer.rb +0 -19
- data/lib/synapse/partitioning/message_receipt.rb +0 -25
- data/lib/synapse/partitioning/packing/json_packer.rb +0 -93
- data/lib/synapse/partitioning/packing/json_unpacker.rb +0 -83
- data/lib/synapse/partitioning/packing.rb +0 -27
- data/lib/synapse/partitioning/queue_reader.rb +0 -32
- data/lib/synapse/partitioning/queue_writer.rb +0 -17
- data/lib/synapse/partitioning.rb +0 -18
- data/test/partitioning/memory_test.rb +0 -34
- data/test/partitioning/packing/json_test.rb +0 -62
data/test/command/wiring_test.rb
CHANGED
@@ -4,7 +4,7 @@ module Synapse
|
|
4
4
|
module Command
|
5
5
|
|
6
6
|
class WiringCommandHandlerTest < Test::Unit::TestCase
|
7
|
-
|
7
|
+
should 'pass the command to the correct wire' do
|
8
8
|
handler = ExampleWiringCommandHandler.new
|
9
9
|
unit = Object.new
|
10
10
|
|
@@ -31,7 +31,7 @@ module Synapse
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
-
|
34
|
+
should 'subscribe handler to the command bus for each wired command type' do
|
35
35
|
handler = ExampleWiringCommandHandler.new
|
36
36
|
bus = Object.new
|
37
37
|
|
@@ -41,7 +41,7 @@ module Synapse
|
|
41
41
|
handler.subscribe bus
|
42
42
|
end
|
43
43
|
|
44
|
-
|
44
|
+
should 'unsubscribe handler from the command bus for each wired command type' do
|
45
45
|
handler = ExampleWiringCommandHandler.new
|
46
46
|
bus = Object.new
|
47
47
|
|
@@ -2,18 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module Synapse
|
4
4
|
class IdentifierLockTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
lock = IdentifierLock.new
|
7
|
-
identifier = 'some_id'
|
8
|
-
|
9
|
-
lock.obtain_lock identifier
|
10
|
-
lock.release_lock identifier
|
11
|
-
|
12
|
-
identifiers = lock.instance_variable_get :@identifiers
|
13
|
-
refute identifiers.has_key? identifier
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_owned?
|
5
|
+
should 'indicate whether the current thread holds a lock' do
|
17
6
|
lock = IdentifierLock.new
|
18
7
|
identifier = 'some_id'
|
19
8
|
|
@@ -26,7 +15,7 @@ module Synapse
|
|
26
15
|
refute lock.owned? identifier
|
27
16
|
end
|
28
17
|
|
29
|
-
|
18
|
+
should 'raise an exception when a thread releases a lock it does not own' do
|
30
19
|
lock = IdentifierLock.new
|
31
20
|
assert_raise ThreadError do
|
32
21
|
lock.release_lock 'derp'
|
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module Synapse
|
4
4
|
class PublicLockTest < Test::Unit::TestCase
|
5
|
-
|
5
|
+
should 'raise an exception when a thread tries to obtain a lock twice' do
|
6
6
|
@lock = PublicLock.new
|
7
7
|
@lock.lock
|
8
8
|
|
@@ -11,7 +11,7 @@ module Synapse
|
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
|
14
|
+
should 'properly manage its list of waiting threads' do
|
15
15
|
@lock = PublicLock.new
|
16
16
|
@lock.lock
|
17
17
|
|
@@ -30,7 +30,7 @@ module Synapse
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
|
33
|
+
should 'synchronize the execution of a block' do
|
34
34
|
@lock = PublicLock.new
|
35
35
|
|
36
36
|
refute @lock.owned?
|
@@ -45,7 +45,7 @@ module Synapse
|
|
45
45
|
refute @lock.owned_by? Thread.current
|
46
46
|
end
|
47
47
|
|
48
|
-
|
48
|
+
should 'raise an exception when a thread releases a lock it does not own' do
|
49
49
|
@lock = PublicLock.new
|
50
50
|
|
51
51
|
assert_raise ThreadError do
|
@@ -53,7 +53,7 @@ module Synapse
|
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
|
-
|
56
|
+
should 'provide a non-blocking lock attempt' do
|
57
57
|
@lock = PublicLock.new
|
58
58
|
|
59
59
|
t = Thread.new do
|
@@ -71,7 +71,7 @@ module Synapse
|
|
71
71
|
t.wakeup
|
72
72
|
end
|
73
73
|
|
74
|
-
|
74
|
+
should 'raise an exception when a thread tries to obtain a non-blocking lock twice' do
|
75
75
|
@lock = PublicLock.new
|
76
76
|
@lock.try_lock
|
77
77
|
|
@@ -7,7 +7,7 @@ module Synapse
|
|
7
7
|
@message = Message.build
|
8
8
|
end
|
9
9
|
|
10
|
-
|
10
|
+
should 'raise an exception when a message is recorded more than once' do
|
11
11
|
refute @recorder.recorded? @message
|
12
12
|
|
13
13
|
@recorder.record @message
|
@@ -18,14 +18,14 @@ module Synapse
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
|
21
|
+
should 'be able to forget a message' do
|
22
22
|
@recorder.record @message
|
23
23
|
@recorder.forget @message
|
24
24
|
|
25
25
|
refute @recorder.recorded? @message
|
26
26
|
end
|
27
27
|
|
28
|
-
|
28
|
+
should 'be able to forget messages recorded before a certain time' do
|
29
29
|
@recorder.record @message
|
30
30
|
|
31
31
|
threshold = 60 * 20 # 20 minutes
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Synapse
|
4
|
+
module Configuration
|
5
|
+
class AsynchronousCommandBusDefinitionFactoryTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@container = Container.new
|
8
|
+
@builder = ContainerBuilder.new @container
|
9
|
+
@builder.unit_factory
|
10
|
+
end
|
11
|
+
|
12
|
+
should 'build with sensible defaults' do
|
13
|
+
@builder.async_command_bus
|
14
|
+
|
15
|
+
command_bus = @container.resolve :command_bus
|
16
|
+
assert command_bus.is_a? Command::AsynchronousCommandBus
|
17
|
+
|
18
|
+
thread_pool = command_bus.thread_pool
|
19
|
+
assert_equal 4, thread_pool.min
|
20
|
+
assert_equal 4, thread_pool.max
|
21
|
+
end
|
22
|
+
|
23
|
+
should 'build with a custom thread pool size' do
|
24
|
+
@builder.async_command_bus do
|
25
|
+
use_threads 2, 8
|
26
|
+
end
|
27
|
+
|
28
|
+
command_bus = @container.resolve :command_bus
|
29
|
+
|
30
|
+
thread_pool = command_bus.thread_pool
|
31
|
+
assert_equal 2, thread_pool.min
|
32
|
+
assert_equal 8, thread_pool.max
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Synapse
|
4
|
+
module Configuration
|
5
|
+
class SimpleCommandBusDefinitionFactoryTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@container = Container.new
|
8
|
+
@builder = ContainerBuilder.new @container
|
9
|
+
end
|
10
|
+
|
11
|
+
should 'build with sensible defaults' do
|
12
|
+
@builder.unit_factory
|
13
|
+
@builder.simple_command_bus
|
14
|
+
|
15
|
+
command_bus = @container.resolve :command_bus
|
16
|
+
assert command_bus.is_a? Command::SimpleCommandBus
|
17
|
+
end
|
18
|
+
|
19
|
+
should 'build with an alternate unit of work factory' do
|
20
|
+
@builder.unit_factory :alt_unit_factory
|
21
|
+
@builder.simple_command_bus do
|
22
|
+
use_unit_factory :alt_unit_factory
|
23
|
+
end
|
24
|
+
|
25
|
+
command_bus = @container.resolve :command_bus
|
26
|
+
end
|
27
|
+
|
28
|
+
should 'build and register tagged command handlers' do
|
29
|
+
handler_a = Object.new
|
30
|
+
handler_b = Object.new
|
31
|
+
|
32
|
+
@builder.definition :first_handler do
|
33
|
+
tag :command_handler
|
34
|
+
use_instance handler_a
|
35
|
+
end
|
36
|
+
|
37
|
+
@builder.definition :second_handler do
|
38
|
+
tag :alt_command_handler
|
39
|
+
use_instance handler_b
|
40
|
+
end
|
41
|
+
|
42
|
+
@builder.unit_factory
|
43
|
+
@builder.simple_command_bus
|
44
|
+
|
45
|
+
@builder.simple_command_bus :alt_command_bus do
|
46
|
+
use_handler_tag :alt_command_handler
|
47
|
+
end
|
48
|
+
|
49
|
+
mock(handler_a).subscribe(is_a(Command::SimpleCommandBus))
|
50
|
+
command_bus = @container.resolve :command_bus
|
51
|
+
|
52
|
+
mock(handler_b).subscribe(is_a(Command::SimpleCommandBus))
|
53
|
+
command_bus = @container.resolve :alt_command_bus
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Synapse
|
4
|
+
module Configuration
|
5
|
+
class SimpleEventBusDefinitionBuilderTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@container = Container.new
|
8
|
+
@builder = ContainerBuilder.new @container
|
9
|
+
end
|
10
|
+
|
11
|
+
should 'build with sensible defaults' do
|
12
|
+
@builder.simple_event_bus
|
13
|
+
|
14
|
+
factory = @container.resolve :event_bus
|
15
|
+
assert factory.is_a? EventBus::SimpleEventBus
|
16
|
+
end
|
17
|
+
|
18
|
+
should 'build and subscribe tagged event listeners' do
|
19
|
+
@builder.definition :first_listener do
|
20
|
+
tag :event_listener
|
21
|
+
use_factory do
|
22
|
+
TestEventListener.new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
@builder.definition :second_listener do
|
27
|
+
tag :alt_event_listener
|
28
|
+
use_factory do
|
29
|
+
TestAltEventListener.new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Defaults
|
34
|
+
@builder.simple_event_bus
|
35
|
+
|
36
|
+
event_bus = @container.resolve :event_bus
|
37
|
+
assert event_bus.subscribed? @container.resolve :first_listener
|
38
|
+
refute event_bus.subscribed? @container.resolve :second_listener
|
39
|
+
|
40
|
+
# Customized
|
41
|
+
@builder.simple_event_bus :alt_event_bus do
|
42
|
+
use_listener_tag :alt_event_listener
|
43
|
+
end
|
44
|
+
|
45
|
+
event_bus = @container.resolve :alt_event_bus
|
46
|
+
refute event_bus.subscribed? @container.resolve :first_listener
|
47
|
+
assert event_bus.subscribed? @container.resolve :second_listener
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class TestEventListener
|
52
|
+
include EventBus::WiringEventListener
|
53
|
+
end
|
54
|
+
class TestAltEventListener
|
55
|
+
include EventBus::WiringEventListener
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Synapse
|
4
|
+
module Configuration
|
5
|
+
class ConverterFactoryDefinitionBuilderTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@container = Container.new
|
8
|
+
@builder = ContainerBuilder.new @container
|
9
|
+
end
|
10
|
+
|
11
|
+
should 'build with sensible defaults' do
|
12
|
+
@builder.converter_factory
|
13
|
+
|
14
|
+
factory = @container.resolve :converter_factory
|
15
|
+
assert factory.is_a? Serialization::ConverterFactory
|
16
|
+
end
|
17
|
+
|
18
|
+
should 'build and register tagged converters' do
|
19
|
+
@builder.definition :json2object_converter do
|
20
|
+
tag :converter, :alt_converter
|
21
|
+
use_factory do
|
22
|
+
Serialization::JsonToObjectConverter.new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
@builder.definition :json2object_alt_converter do
|
26
|
+
tag :alt_converter
|
27
|
+
use_factory do
|
28
|
+
Serialization::JsonToObjectConverter.new
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Defaults
|
33
|
+
@builder.converter_factory
|
34
|
+
|
35
|
+
factory = @container.resolve :converter_factory
|
36
|
+
factory.converters.at(0).is_a? Serialization::JsonToObjectConverter
|
37
|
+
|
38
|
+
# Customized
|
39
|
+
@builder.converter_factory :alt_factory do
|
40
|
+
use_converter_tag :alt_converter
|
41
|
+
end
|
42
|
+
|
43
|
+
factory = @container.resolve :alt_factory
|
44
|
+
factory.converters.at(0).is_a? Serialization::JsonToObjectConverter
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Synapse
|
4
|
+
module Configuration
|
5
|
+
class SerializerDefinitionBuilderTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@container = Container.new
|
8
|
+
@builder = ContainerBuilder.new @container
|
9
|
+
end
|
10
|
+
|
11
|
+
should 'build with an alternate converter factory' do
|
12
|
+
@builder.converter_factory :alt_converter_factory
|
13
|
+
@builder.serializer do
|
14
|
+
use_converter_factory :alt_converter_factory
|
15
|
+
end
|
16
|
+
|
17
|
+
serializer = @container.resolve :serializer
|
18
|
+
converter_factory = @container.resolve :alt_converter_factory
|
19
|
+
assert_same converter_factory, serializer.converter_factory
|
20
|
+
end
|
21
|
+
|
22
|
+
should 'build with AttributeSerializer' do
|
23
|
+
@builder.converter_factory
|
24
|
+
@builder.serializer do
|
25
|
+
use_attribute
|
26
|
+
end
|
27
|
+
|
28
|
+
serializer = @container.resolve :serializer
|
29
|
+
assert serializer.is_a? Serialization::AttributeSerializer
|
30
|
+
end
|
31
|
+
|
32
|
+
should 'build with MarshalSerializer' do
|
33
|
+
@builder.converter_factory
|
34
|
+
@builder.serializer do
|
35
|
+
use_marshal
|
36
|
+
end
|
37
|
+
|
38
|
+
serializer = @container.resolve :serializer
|
39
|
+
assert serializer.is_a? Serialization::MarshalSerializer
|
40
|
+
end
|
41
|
+
|
42
|
+
should 'build with OxSerializer' do
|
43
|
+
skip 'Ox not supported on JRuby' if defined? JRUBY_VERSION
|
44
|
+
|
45
|
+
serialize_options = { :circular => true }
|
46
|
+
|
47
|
+
@builder.converter_factory
|
48
|
+
@builder.serializer do
|
49
|
+
use_ox
|
50
|
+
use_serialize_options serialize_options
|
51
|
+
end
|
52
|
+
|
53
|
+
serializer = @container.resolve :serializer
|
54
|
+
assert serializer.is_a? Serialization::OxSerializer
|
55
|
+
assert_equal serialize_options, serializer.serialize_options
|
56
|
+
end
|
57
|
+
|
58
|
+
should 'build with OjSerializer' do
|
59
|
+
skip 'Oj not supported on JRuby' if defined? JRUBY_VERSION
|
60
|
+
|
61
|
+
serialize_options = { :indent => 2, :circular => true }
|
62
|
+
deserialize_options = { :symbol_keys => true }
|
63
|
+
|
64
|
+
@builder.converter_factory
|
65
|
+
@builder.serializer do
|
66
|
+
use_oj
|
67
|
+
use_serialize_options serialize_options
|
68
|
+
use_deserialize_options deserialize_options
|
69
|
+
end
|
70
|
+
|
71
|
+
serializer = @container.resolve :serializer
|
72
|
+
assert serializer.is_a? Serialization::OjSerializer
|
73
|
+
assert_equal serialize_options, serializer.serialize_options
|
74
|
+
assert_equal deserialize_options, serializer.deserialize_options
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Synapse
|
4
|
+
module Configuration
|
5
|
+
class UnitOfWorkFactoryDefinitionBuilderTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@container = Container.new
|
8
|
+
@builder = ContainerBuilder.new @container
|
9
|
+
end
|
10
|
+
|
11
|
+
should 'build with sensible defaults' do
|
12
|
+
@builder.factory :transaction_manager do
|
13
|
+
UnitOfWork::TransactionManager.new
|
14
|
+
end
|
15
|
+
@builder.unit_factory
|
16
|
+
|
17
|
+
factory = @container.resolve :unit_factory
|
18
|
+
assert factory.is_a? UnitOfWork::UnitOfWorkFactory
|
19
|
+
assert factory.transaction_manager
|
20
|
+
end
|
21
|
+
|
22
|
+
should 'build with an alternate transaction manager' do
|
23
|
+
@builder.factory :alt_tx_manager do
|
24
|
+
UnitOfWork::TransactionManager.new
|
25
|
+
end
|
26
|
+
@builder.unit_factory do
|
27
|
+
use_transaction_manager :alt_tx_manager
|
28
|
+
end
|
29
|
+
|
30
|
+
factory = @container.resolve :unit_factory
|
31
|
+
assert factory.transaction_manager
|
32
|
+
end
|
33
|
+
|
34
|
+
should 'build with an alternate unit of work provider' do
|
35
|
+
@builder.factory :alt_unit_provider do
|
36
|
+
UnitOfWork::UnitOfWorkProvider.new
|
37
|
+
end
|
38
|
+
@builder.unit_factory do
|
39
|
+
use_unit_provider :alt_unit_provider
|
40
|
+
end
|
41
|
+
|
42
|
+
@container.resolve :unit_factory
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Synapse
|
4
|
+
module Configuration
|
5
|
+
class ContainerBuilderTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
# Backup any existing initializers
|
8
|
+
@initializers = ContainerBuilder.initializers
|
9
|
+
ContainerBuilder.initializers = nil
|
10
|
+
|
11
|
+
# Create a container that will be built
|
12
|
+
@container = Container.new
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
# Restore any existing initializers
|
17
|
+
ContainerBuilder.initializers = @initializers
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_initializers
|
21
|
+
ContainerBuilder.initializer do
|
22
|
+
definition :test_definition do
|
23
|
+
tag :derp
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
builder = ContainerBuilder.new @container
|
28
|
+
|
29
|
+
assert @container.registered? :test_definition
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_factory
|
33
|
+
builder = ContainerBuilder.new @container
|
34
|
+
builder.build_with do
|
35
|
+
factory :derp_service, :tag => [:first_tag, :nth_tag] do
|
36
|
+
123
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
assert @container.registered? :derp_service
|
41
|
+
assert_equal 123, @container.resolve(:derp_service)
|
42
|
+
assert_equal [123], @container.resolve_tagged(:first_tag)
|
43
|
+
assert_equal [123], @container.resolve_tagged(:nth_tag)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'configuration/fixtures/dependent'
|
3
|
+
|
4
|
+
module Synapse
|
5
|
+
module Configuration
|
6
|
+
class ContainerTest < Test::Unit::TestCase
|
7
|
+
|
8
|
+
should 'inject services into a Dependent object' do
|
9
|
+
container = Container.new
|
10
|
+
|
11
|
+
service_a = Object.new
|
12
|
+
service_b = Object.new
|
13
|
+
|
14
|
+
DefinitionBuilder.build container, :service_a do
|
15
|
+
use_instance service_a
|
16
|
+
end
|
17
|
+
|
18
|
+
DefinitionBuilder.build container, :service_b do
|
19
|
+
use_instance service_b
|
20
|
+
end
|
21
|
+
|
22
|
+
dependent = ExampleDependent.new
|
23
|
+
container.inject_into dependent
|
24
|
+
|
25
|
+
assert_same service_a, dependent.service_a
|
26
|
+
assert_same service_b, dependent.some_service
|
27
|
+
end
|
28
|
+
|
29
|
+
should 'not attempt to inject services into a non-Dependent object' do
|
30
|
+
container = Container.new
|
31
|
+
object = Object.new
|
32
|
+
|
33
|
+
container.inject_into object
|
34
|
+
end
|
35
|
+
|
36
|
+
should 'resolve a service from a definition by its identifier' do
|
37
|
+
reference = Object.new
|
38
|
+
container = Container.new
|
39
|
+
|
40
|
+
DefinitionBuilder.build container, :some_service do
|
41
|
+
use_instance reference
|
42
|
+
end
|
43
|
+
|
44
|
+
assert_same reference, container.resolve(:some_service)
|
45
|
+
end
|
46
|
+
|
47
|
+
should 'resolve a service from a definition by its tag' do
|
48
|
+
container = Container.new
|
49
|
+
some_service = Object.new
|
50
|
+
some_other_service = Object.new
|
51
|
+
|
52
|
+
DefinitionBuilder.build container, :some_service do
|
53
|
+
tag :some_tag
|
54
|
+
use_instance some_service
|
55
|
+
end
|
56
|
+
|
57
|
+
DefinitionBuilder.build container, :some_other_service do
|
58
|
+
tag :some_other_tag
|
59
|
+
use_instance some_other_service
|
60
|
+
end
|
61
|
+
|
62
|
+
# Do it breh
|
63
|
+
tagged = container.resolve_tagged :some_tag
|
64
|
+
assert tagged.include? some_service
|
65
|
+
end
|
66
|
+
|
67
|
+
should 'support optional service resolution' do
|
68
|
+
container = Container.new
|
69
|
+
|
70
|
+
assert_nil container.resolve :some_service, true
|
71
|
+
assert_raise ConfigurationError do
|
72
|
+
container.resolve :some_service
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
should 'log when a definition is replaced' do
|
77
|
+
logger = Logging.logger[Container]
|
78
|
+
|
79
|
+
mock(logger).info(anything)
|
80
|
+
|
81
|
+
container = Container.new
|
82
|
+
container.register :some_service, Object.new
|
83
|
+
container.register :some_service, Object.new
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|