Skip to content

Commit fda97ff

Browse files
Merge pull request #242 from OpenSmock/dev-issue241
Dev issue241
2 parents df602af + 1f576aa commit fda97ff

File tree

2 files changed

+106
-6
lines changed

2 files changed

+106
-6
lines changed

src/Molecule-Tests/MolEventsTest.class.st

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,102 @@ MolEventsTest >> tearDown [
2222
super tearDown.
2323
]
2424

25+
{ #category : #tests }
26+
MolEventsTest >> testOneListenerWithSeveralWarnersWithoutSameSubscribers [
27+
28+
| warner1 warner2 listener subscriptions subscribers listener2 subscribers2 subscriptions2 |
29+
"Deployment"
30+
MolMyWarnerComponentImpl deploy.
31+
MolMyListenerComponentImpl deploy.
32+
33+
"Instanciation"
34+
warner1 := MolMyWarnerComponentImpl instantiate: #warner1.
35+
warner2 := MolMyWarnerComponentImpl instantiate: #warner2.
36+
listener := MolMyListenerComponentImpl instantiate: #listener1.
37+
listener2 := MolMyListenerComponentImpl instantiate: #listener2.
38+
39+
"Subscriptions"
40+
subscriptions := Array with: #warner1.
41+
listener forEvents: MolMyStateChangeEvents useAllProducers: subscriptions.
42+
43+
subscriptions2 := Array with: #warner2.
44+
listener2 forEvents: MolMyStateChangeEvents useAllProducers: subscriptions2.
45+
46+
subscribers := listener eventsSubscribers.
47+
self assert: subscribers size equals: 1.
48+
self assert: (subscribers at: MolMyStateChangeEvents) equals: #warner1.
49+
50+
subscribers2 := listener2 eventsSubscribers.
51+
self assert: subscribers2 size equals: 1.
52+
self assert: (subscribers2 at: MolMyStateChangeEvents) equals: #warner2.
53+
54+
"Activation"
55+
MolMyWarnerComponentImpl activate: #warner1.
56+
MolMyWarnerComponentImpl activate: #warner2.
57+
MolMyListenerComponentImpl activate: #listener1.
58+
MolMyListenerComponentImpl activate: #listener2.
59+
60+
"Tests"
61+
warner1 operationalEvent.
62+
self assert: listener currentState equals: #operational.
63+
self assert: listener2 currentState isNil.
64+
warner2 failureEvent. "warner 2 is not active, so he can't send an event"
65+
self assert: listener currentState equals: #operational.
66+
self assert: listener2 currentState equals: #failure
67+
]
68+
69+
{ #category : #tests }
70+
MolEventsTest >> testOneListenerWithSeveralWarnersWithoutSameSubscribers2 [
71+
72+
| warner1 warner2 listener subscriptions subscribers listener2 subscribers2 subscriptions2 |
73+
"Deployment"
74+
MolMyWarnerComponentImpl deploy.
75+
MolMyListenerComponentImpl deploy.
76+
77+
"Instanciation"
78+
warner1 := MolMyWarnerComponentImpl instantiate: #warner1.
79+
warner2 := MolMyWarnerComponentImpl instantiate: #warner2.
80+
listener := MolMyListenerComponentImpl instantiate: #listener1.
81+
listener2 := MolMyListenerComponentImpl instantiate: #listener2.
82+
83+
"Subscriptions"
84+
subscriptions := #warner1.
85+
listener forEvents: MolMyStateChangeEvents useProducer: subscriptions.
86+
87+
subscriptions2 := #warner2.
88+
listener2
89+
forEvents: MolMyStateChangeEvents
90+
useProducer: subscriptions2.
91+
92+
subscribers := listener eventsSubscribers.
93+
self assert: subscribers size equals: 1.
94+
self
95+
assert: (subscribers at: MolMyStateChangeEvents)
96+
equals: #warner1.
97+
98+
subscribers2 := listener2 eventsSubscribers.
99+
self assert: subscribers2 size equals: 1.
100+
self
101+
assert: (subscribers2 at: MolMyStateChangeEvents)
102+
equals: #warner2.
103+
104+
"Activation"
105+
106+
MolMyWarnerComponentImpl activate: #warner1.
107+
108+
MolMyListenerComponentImpl activate: #listener1.
109+
MolMyListenerComponentImpl activate: #listener2.
110+
111+
"Tests"
112+
113+
warner1 operationalEvent.
114+
self assert: listener currentState equals: #operational.
115+
self assert: listener2 currentState isNil.
116+
warner2 failureEvent. "warner 2 is not active, so he can't send an event"
117+
self assert: listener currentState equals: #operational.
118+
self assert: listener2 currentState isNil.
119+
]
120+
25121
{ #category : #tests }
26122
MolEventsTest >> testSeveralListeners [
27123

src/Molecule/MolEventSubscriber.class.st

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,16 @@ MolEventSubscriber >> connectAllOriginatorsTo: aComponent [
6060
MolEventSubscriber >> connectOriginator: componentName to: aComponent [
6161
"Connect a component to event pipeline"
6262

63-
self events allSelectors do: [ :event | | originator |
64-
65-
originator := self originatorsLinks at: componentName.
66-
originator ifNil: [ ^ self error: 'Component originator is nil' ].
67-
originator when: event send: event to: aComponent.
68-
].
63+
| key connect |
64+
key := aComponent eventsSubscribers at: self events ifAbsent: [ nil ].
65+
connect := key isSymbol ifTrue:[ componentName = key ] ifFalse:[ key includes: componentName ].
66+
67+
connect ifTrue: [
68+
self events allSelectors do: [ :event |
69+
| originator |
70+
originator := self originatorsLinks at: componentName.
71+
originator ifNil: [ ^ self error: 'Component originator is nil' ].
72+
originator when: event send: event to: aComponent ] ]
6973
]
7074

7175
{ #category : #private }

0 commit comments

Comments
 (0)