Skip to content

Commit

Permalink
Bloc-Tasks: improve example + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tinchodias committed May 18, 2024
1 parent f70c6d4 commit 5a86feb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 23 deletions.
43 changes: 20 additions & 23 deletions src/Bloc-Taskit-Tests/TKTBlocExamples.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ Class {
{ #category : #examples }
TKTBlocExamples >> runnerExample [
<sampleInstance>
" This is equivalent to:
space
enqueueTask:
(BlRepeatedTaskAction new
delay: 200 milliSeconds;
action: [ space root background: Color random ];
yourself)."

| space runner |
space := BlSpace new.
Expand All @@ -19,23 +26,21 @@ TKTBlocExamples >> runnerExample [
[ 17 timesRepeat: [
runner schedule: [
space root background: Color random ] asTask.
250 milliSeconds wait ].
200 milliSeconds wait ] ] fork.

runner stop.
space close ] fork
^ space
]

" We kind of emulate this:
{ #category : #examples }
TKTBlocExamples >> serviceExample [
<sampleInstance>
"This example is equivalent to:
space
enqueueTask:
(BlRepeatedTaskAction new
delay: 250 milliSeconds;
delay: 100 milliSeconds;
action: [ space root background: Color random ];
yourself)."
]

{ #category : #examples }
TKTBlocExamples >> serviceExample [
<sampleInstance>

| space service |
space := BlSpace new.
Expand All @@ -44,20 +49,12 @@ TKTBlocExamples >> serviceExample [
show.

service := TKTBlSpaceService on: space.

service step: [ space root background: Color random ].
service stepDelay: 250 milliSeconds.
service stepDelay: 100 milliSeconds.
service start.

[ 3 seconds wait.
service stop.
space close ] fork

" We kind of emulate this:
space
enqueueTask:
(BlRepeatedTaskAction new
delay: 250 milliSeconds;
action: [ space root background: Color random ];
yourself)."
[ 2 seconds wait.
service stop waitForCompletion: 1 second ] fork.

^ space
]
56 changes: 56 additions & 0 deletions src/Bloc-Taskit-Tests/TKTBlocTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Class {
#name : #TKTBlocTest,
#superclass : #BlParameterizedHostTest,
#category : #'Bloc-Taskit-Tests'
}

{ #category : #running }
TKTBlocTest >> isBeingExecutedInTaskPhase [

^ thisContext stack anySatisfy: [ :each |
each receiver class = BlSpaceFrameTaskPhase ]
]

{ #category : #tests }
TKTBlocTest >> testRunner [

| space runner |
space := self newTestingSpace.
space root background: Color green.

runner := TKTBlSpaceRunner on: space.
runner schedule: [
space root background: Color red.
self assert: self isBeingExecutedInTaskPhase ] asTask.

"Task is not executed immediately"
self
assert: space root background paint color
equals: Color green.
"Check again after pulse"
self waitTestingSpaces.
self
assert: space root background paint color
equals: Color red
]

{ #category : #tests }
TKTBlocTest >> testService [

| space service count |
space := self newTestingSpace.
space show.

service := TKTBlSpaceService on: space.
count := 0.
service step: [
count := count + 1.
self assert: self isBeingExecutedInTaskPhase ].
service stepDelay: 50 milliSeconds.
service start.

0.5 seconds wait.
service stop waitForCompletion: 1 second.

self assert: count > 1
]

0 comments on commit 5a86feb

Please sign in to comment.