Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #170: Add announcements for component life-cycle + unit tests. #177

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions src/Molecule-Tests/MolComponentAnnouncementTest.class.st
labordep marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"
A MolComponentInstanciatedTest is a test class for testing the behavior of MolComponentInstanciated
"
Class {
#name : #MolComponentAnnouncementTest,
#superclass : #TestCase,
#category : #'Molecule-Tests-Announcements'
}

{ #category : #running }
MolComponentAnnouncementTest >> setUp [

super setUp.
MolComponentManager cleanUp
]

{ #category : #running }
MolComponentAnnouncementTest >> tearDown [

MolComponentManager cleanUp.
super tearDown
]

{ #category : #tests }
MolComponentAnnouncementTest >> testAnnounceActivated [

| announcement component component2 |
announcement := nil.
SystemAnnouncer uniqueInstance when: MolComponentActivated do:[ :a | announcement := a ].

"test by standard instanciation"
MolMyUserComponentImpl deploy.
component := MolMyUserComponentImpl instantiate.
self assert: announcement isNil.

MolMyUserComponentImpl activate.
self assert: announcement notNil.
self assert: announcement component equals: component.
self assert: announcement componentName equals: MolUtils defaultComponentName.
announcement := nil.

"test by named instanciation"
component2 := MolMyUserComponentImpl instantiate: #myComponent.
MolMyUserComponentImpl activate: #myComponent.
self assert: announcement notNil.
self assert: announcement component equals: component2.
self assert: announcement componentName equals: #myComponent.
]

{ #category : #tests }
MolComponentAnnouncementTest >> testAnnounceInstanciated [

| announcement component component2 |
announcement := nil.
SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ].

"test by standard instanciation"
MolMyUserComponentImpl deploy.
component := MolMyUserComponentImpl instantiate.
self assert: announcement notNil.
self assert: announcement component equals: component.
self assert: announcement componentName equals: MolUtils defaultComponentName.
announcement := nil.

"test by named instanciation"
component2 := MolMyUserComponentImpl instantiate: #myComponent.
self assert: announcement notNil.
self assert: announcement component equals: component2.
self assert: announcement componentName equals: #myComponent.
]

{ #category : #tests }
MolComponentAnnouncementTest >> testAnnouncePassivated [

| announcement component component2 |
announcement := nil.
SystemAnnouncer uniqueInstance when: MolComponentPassivated do:[ :a | announcement := a ].

"test by standard instanciation"
MolMyUserComponentImpl deploy.
component := MolMyUserComponentImpl instantiate.
MolMyUserComponentImpl activate.
self assert: announcement isNil.

MolMyUserComponentImpl passivate.
self assert: announcement notNil.
self assert: announcement component equals: component.
self assert: announcement componentName equals: MolUtils defaultComponentName.
announcement := nil.

"test by named instanciation"
component2 := MolMyUserComponentImpl instantiate: #myComponent.
MolMyUserComponentImpl activate: #myComponent.
MolMyUserComponentImpl passivate: #myComponent.
self assert: announcement notNil.
self assert: announcement component equals: component2.
self assert: announcement componentName equals: #myComponent.
]

{ #category : #tests }
MolComponentAnnouncementTest >> testAnnounceRemoved [

| announcement component component2 |
announcement := nil.
SystemAnnouncer uniqueInstance when: MolComponentRemoved do:[ :a | announcement := a ].

"test by standard instanciation"
MolMyUserComponentImpl deploy.
component := MolMyUserComponentImpl instantiate.
MolMyUserComponentImpl activate.
MolMyUserComponentImpl passivate.
self assert: announcement isNil.

MolMyUserComponentImpl remove.
self assert: announcement notNil.
self assert: announcement component equals: component.
self assert: announcement componentName equals: MolUtils defaultComponentName.
announcement := nil.

"test by named instanciation"
component2 := MolMyUserComponentImpl instantiate: #myComponent.
MolMyUserComponentImpl activate: #myComponent.
MolMyUserComponentImpl passivate: #myComponent.
self assert: announcement isNil.

MolMyUserComponentImpl remove: #myComponent.
self assert: announcement notNil.
self assert: announcement component equals: component2.
self assert: announcement componentName equals: #myComponent.
]
16 changes: 16 additions & 0 deletions src/Molecule-Tests/MolComponentManagerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ MolComponentManagerTest >> testCleanUp [

]

{ #category : #tests }
MolComponentManagerTest >> testCleanUpAnnouncements [
| announcement list selection |
announcement := nil.
SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ].
SystemAnnouncer uniqueInstance when: MolComponentPassivated do:[ :a | announcement := a ].
SystemAnnouncer uniqueInstance when: MolComponentInstanciated do:[ :a | announcement := a ].
list := MolAnnouncement withAllSubclasses.
selection := SystemAnnouncer uniqueInstance subscriptions subscriptions select: [ :e | list includes: e announcementClass] .
self assert: (selection size > 0).
MolComponentManager cleanupMolAnnouncements.
selection := SystemAnnouncer uniqueInstance subscriptions subscriptions select: [ :e | list includes: e announcementClass] .
self assert: (selection size = 0).

]

{ #category : #'tests - general' }
MolComponentManagerTest >> testCleanUpIgnoreErrors [
"Start a manuel tree of components and clean up, components are removed during the cleanUp but some componentRemove methods should remove again an already removed component : a error is reach but ignored during a cleanUp"
Expand Down
5 changes: 5 additions & 0 deletions src/Molecule/MolAnnouncement.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Class {
#name : #MolAnnouncement,
#superclass : #Announcement,
#category : #'Molecule-Announcements'
}
5 changes: 5 additions & 0 deletions src/Molecule/MolComponentActivated.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Class {
#name : #MolComponentActivated,
#superclass : #MolComponentAnnouncement,
#category : #'Molecule-Announcements'
}
33 changes: 33 additions & 0 deletions src/Molecule/MolComponentAnnouncement.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Class {
#name : #MolComponentAnnouncement,
#superclass : #MolAnnouncement,
#instVars : [
'component',
'componentName'
],
#category : #'Molecule-Announcements'
}

{ #category : #accessing }
MolComponentAnnouncement >> component [

^ component
]

{ #category : #accessing }
MolComponentAnnouncement >> component: anObject [

component := anObject
]

{ #category : #accessing }
MolComponentAnnouncement >> componentName [

^ componentName
]

{ #category : #accessing }
MolComponentAnnouncement >> componentName: anObject [

componentName := anObject
]
8 changes: 8 additions & 0 deletions src/Molecule/MolComponentInstanciated.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"
Announcement when a component is instanciated by the ComponentManager.
"
Class {
#name : #MolComponentInstanciated,
#superclass : #MolComponentAnnouncement,
#category : #'Molecule-Announcements'
}
18 changes: 18 additions & 0 deletions src/Molecule/MolComponentManager.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ MolComponentManager class >> cleanUp [
notifiers do: [ :n | n release ].
subscribers do: [ :n | n release ].

self cleanupMolAnnouncements.

MolUtils log: 'End of the cleanUp.'
]

Expand All @@ -57,6 +59,14 @@ MolComponentManager class >> cleanUp: aBoolean [
self deepCleanUp.
]

{ #category : #cleanup }
MolComponentManager class >> cleanupMolAnnouncements [
<script>
| registeredList |
registeredList := SystemAnnouncer uniqueInstance subscriptions subscriptions select: [ :e | self isMolAnnoucement: e announcementClass ].
registeredList do: [ :r | SystemAnnouncer uniqueInstance subscriptions remove: r].
]

{ #category : #cleanup }
MolComponentManager class >> deepCleanUp [

Expand Down Expand Up @@ -142,6 +152,14 @@ MolComponentManager class >> isInitialized [
^ Default notNil
]

{ #category : #cleanup }
MolComponentManager class >> isMolAnnoucement: aClass [

MolAnnouncement withAllSubclasses do: [ :a |
a = aClass ifTrue: [ ^ true ] ].
^ false
]

{ #category : #'initialize-release' }
MolComponentManager class >> isRunningComponents [
<script:'self isRunningComponents inspect'>
Expand Down
5 changes: 5 additions & 0 deletions src/Molecule/MolComponentPassivated.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Class {
#name : #MolComponentPassivated,
#superclass : #MolComponentAnnouncement,
#category : #'Molecule-Announcements'
}
5 changes: 5 additions & 0 deletions src/Molecule/MolComponentRemoved.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Class {
#name : #MolComponentRemoved,
#superclass : #MolComponentAnnouncement,
#category : #'Molecule-Announcements'
}
42 changes: 27 additions & 15 deletions src/Molecule/MolHomeServices.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ MolHomeServices >> activateComponent: aComponentClass named: aName [
MolComponentManager default locatorServices resolveWaitingSubscriptions: component.

component componentActivate.
component triggerEvent: #activateComponent with: component.
self announcer announce: (MolComponentActivated new
component: component; componentName: aName; yourself).

MolUtils log: aComponentClass printString , ' named: ' , aName printString , ' is activated.'.

Expand All @@ -68,6 +69,12 @@ MolHomeServices >> addDeployedComponent: aComponentClass [
self deployedComponents at: aComponentClass ifAbsentPut: [Dictionary new]
]

{ #category : #announcer }
MolHomeServices >> announcer [

^ SystemAnnouncer uniqueInstance
]

{ #category : #private }
MolHomeServices >> checkComponentInstantiationPermission: aComponentClass named: aComponentName [

Expand Down Expand Up @@ -135,9 +142,10 @@ MolHomeServices >> deployedComponents [

{ #category : #'initialize-release' }
MolHomeServices >> initialize [

super initialize.
deployedComponents := Dictionary new.
waitingForActivation := Set new.
waitingForActivation := Set new
]

{ #category : #accessing }
Expand Down Expand Up @@ -191,19 +199,19 @@ MolHomeServices >> instantiateComponent: aComponentClass named: aName [
error ifNotNil: [ error signal. ^ nil ].

component := aComponentClass new componentName: aName; yourself.

MolUtils log:
aComponentClass printString , ' named: ' , aName printString
, ' is instantiated.'.

deployed := self deployedComponents at: aComponentClass.
deployed at: component componentName put: component.

"connect the component to the component model and initialize it"
connector := MolComponentConnector new.
component componentConnector: connector.
component componentInitialize.
component triggerEvent: #instantiateComponent with: component.
self announcer announce: (MolComponentInstanciated new
component: component; componentName: aName; yourself).

MolUtils log:
aComponentClass printString , ' named: ' , aName printString
, ' is instantiated.'.

"add the component to waiting activation list"
waitingForActivation add: component.
Expand Down Expand Up @@ -266,8 +274,9 @@ MolHomeServices >> passivateComponent: aComponentClass named: aName [

component componentPassivate.
component componentConnector passivateComponent.
component triggerEvent: #passivateComponent with: component.

self announcer announce: (MolComponentPassivated new
component: component; componentName: aName; yourself).

MolUtils log: aComponentClass printString , ' named: ' , aName printString, ' is passivated.'.

"add in waiting activation list"
Expand All @@ -278,10 +287,11 @@ MolHomeServices >> passivateComponent: aComponentClass named: aName [

{ #category : #'initialize-release' }
MolHomeServices >> release [

self removeAllComponents.
deployedComponents := nil.
waitingForActivation := nil.
super release.
super release
]

{ #category : #'life cycle' }
Expand Down Expand Up @@ -333,10 +343,7 @@ MolHomeServices >> removeComponent: aComponentClass named: aName ifAbsent: aBloc
"Remove the component"
removedComponent componentRemove.
removedComponent componentConnector: nil.
removedComponent
triggerEvent: #removeComponent
with: removedComponent.


"Get deployed component instances of this component class"
deployedComponentsOfThisClass := self deployedComponents
at: aComponentClass
Expand Down Expand Up @@ -368,9 +375,14 @@ MolHomeServices >> removeComponent: aComponentClass named: aName ifAbsent: aBloc

"Finish release of the component life-cycle"
removedComponent componentName: nil.

self announcer announce: (MolComponentRemoved new
component: removedComponent; componentName: aName; yourself).

MolUtils log:
aComponentClass printString , ' named: ' , aName printString
, ' is removed.'.

^ removedComponent
]

Expand Down
Loading