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

Add TKTService>>signalIterationNow #82

Closed
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
94 changes: 93 additions & 1 deletion TaskIt-Tests/TKTServiceTests.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Class {
#instVars : [
'service'
],
#category : 'TaskIt-Tests-Services'
#category : #'TaskIt-Tests-Services'
}

{ #category : #tests }
Expand Down Expand Up @@ -58,3 +58,95 @@ TKTServiceTests >> testServiceShouldHaveAName [

self should: [TKTService new start] raise: Error
]

{ #category : #test }
TKTServiceTests >> testSignalIterationIsIgnoredNowAfterStop [
| queue tinyDelay |
tinyDelay := 5 milliSeconds asDelay.
queue := AtomicSharedQueue new.
service := TKTParameterizableService new
name: 'test service';
stepDelay: 50 milliSeconds;
step: [ queue nextPut: 'iterated once more' ];
yourself.

"Start iterates once."
service start.
tinyDelay wait.
self assert: queue size equals: 1.

"Stop doesn't iterate again."
service stop.
tinyDelay wait.
self assert: queue size equals: 1.

"After #stop, the signal is ignored."
service signalIterationNow.
tinyDelay wait.
self assert: queue size equals: 1.

]

{ #category : #test }
TKTServiceTests >> testSignalIterationNowIsIgnoredBeforeStart [
| queue tinyDelay |
tinyDelay := 5 milliSeconds asDelay.
queue := AtomicSharedQueue new.
service := TKTParameterizableService new
name: 'test service';
stepDelay: 50 milliSeconds;
step: [ queue nextPut: 'iterated once more' ];
yourself.

"Before #start, the signal is ignored."
service signalIterationNow.
tinyDelay wait.
self assert: queue size equals: 0.

]

{ #category : #test }
TKTServiceTests >> testSignalIterationNowKeepsNormalCycleAfterwards [
| queue tinyDelay |
tinyDelay := 5 milliSeconds asDelay.
queue := AtomicSharedQueue new.
service := TKTParameterizableService new
name: 'test service';
stepDelay: 50 milliSeconds;
step: [ queue nextPut: 'iterated once more' ];
yourself.

"Start iterates once."
service start.
tinyDelay wait.
self assert: queue size equals: 1.

"Many signals (without yielding processor) only result in one single resumed iteration."
20 timesRepeat: [ service signalIterationNow ].
tinyDelay wait.
self assert: queue size equals: 2.

"Check it keeps normal behavior."
service stepDelay wait.
self assert: queue size equals: 3.
]

{ #category : #test }
TKTServiceTests >> testSignalIterationNowResumesCycle [
| queue tinyDelay |
tinyDelay := 5 milliSeconds asDelay.
queue := AtomicSharedQueue new.
service := TKTParameterizableService new
name: 'test service';
stepDelay: 50 milliSeconds;
step: [ queue nextPut: 'iterated once more' ];
yourself.

"Start iterates once, and every time we signal, the next iteration is resumed (in advance)."
service start.
1 to: 5 do: [ :index |
service signalIterationNow.
tinyDelay wait.
self assert: queue size equals: index ].

]
17 changes: 11 additions & 6 deletions TaskIt/TKTService.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ Class {
'stopCallbacks',
'stepDelay',
'recursiveTask',
'currentDelay'
'iterationSemaphore'
],
#category : #'TaskIt-Services'
}
Expand Down Expand Up @@ -99,8 +99,8 @@ TKTService >> isRunning [
{ #category : #stepping }
TKTService >> iterateService [
self stepService.
currentDelay := stepDelay asDelay.
currentDelay wait
iterationSemaphore := Semaphore new.
iterationSemaphore wait: stepDelay
]

{ #category : #private }
Expand Down Expand Up @@ -169,6 +169,11 @@ TKTService >> setUp [
"Hook"
]

{ #category : #starting }
TKTService >> signalIterationNow [
^ iterationSemaphore ifNotNil: [ :s | s signal ]
]

{ #category : #starting }
TKTService >> start [
stopRequested := false.
Expand All @@ -186,8 +191,8 @@ TKTService >> stepDelay [
]

{ #category : #starting }
TKTService >> stepDelay: aTimeDelay [
stepDelay := aTimeDelay
TKTService >> stepDelay: aDuration [
stepDelay := aDuration
]

{ #category : #stepping }
Expand All @@ -203,7 +208,7 @@ TKTService >> stop [
self onStoppedDo: [ :v | futureStop deploySuccess: self ].
stopRequested := true.
TKTConfiguration serviceManager removeService: self.
currentDelay ifNotNil: [:delay | delay delaySemaphore signal ].
self signalIterationNow.
^ futureStop
]

Expand Down