Skip to content

Commit

Permalink
Merge a5984ed
Browse files Browse the repository at this point in the history
  • Loading branch information
syrel committed Mar 27, 2024
2 parents 0480cac + a5984ed commit 8642e87
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 16 deletions.
224 changes: 224 additions & 0 deletions src/Futures-Examples/TaskAtQueueExamples.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
Class {
#name : #TaskAtQueueExamples,
#superclass : #Object,
#traits : 'TAssertable',
#classTraits : 'TAssertable classTrait',
#category : #'Futures-Examples'
}

{ #category : #examples }
TaskAtQueueExamples >> checkQueueOrder [
"Ensure that tasks are retrieved from the queue in chronological order"
<gtExample>

| queue now entries |

queue := TaskAtQueue new.
now := DateAndTime now.
queue schedule: 1 at: now + 10 seconds.
queue schedule: 5 at: now + 50 seconds.
queue schedule: 3 at: now + 30 seconds.
queue schedule: 7 at: now + 70 seconds.
queue schedule: 2 at: now + 20 seconds.

"Set the queue process time so entryPriority can be calculated"
queue processTime: now.

entries := OrderedCollection new.
5 timesRepeat: [ entries add: queue next valuable ].

self
assert: entries asArray
equals: #(1 2 3 5 7).

]

{ #category : #examples }
TaskAtQueueExamples >> removeTask [
"Demonstrate removing a task from the queue"
<gtExample>
| clock queue now task executed |

executed := 0.
clock := BlSimulatedTime new.
queue := TaskAtQueue new
clock: clock.
now := clock currentTime.
task := TaskAtQueueTask
valuable: [ executed := executed + 1 ]
at: now + 10 seconds
entryIdentifier: self class name.
queue schedule: task.
self assert: executed equals: 0.

queue pulse.
self assert: executed equals: 0.

queue removeTask: task.
clock wait: 12 seconds.

queue pulse.
self assert: executed equals: 0.
]

{ #category : #examples }
TaskAtQueueExamples >> rescheduleTaskBackward [
"{{gtClass:BlTaskAtQueue}} allows tasks to be rescheduled, changing their time forwards or backwards.
Demonstrate rescheduling tasks, changing it's time so that it is run earlier"
<gtExample>
| clock queue now task executed |

executed := 0.
clock := BlSimulatedTime new.
queue := TaskAtQueue new
clock: clock.
now := clock currentTime.
task := TaskAtQueueTask
valuable: [ executed := executed + 1 ]
at: now + 10 seconds
entryIdentifier: self class name.
queue schedule: task.
self assert: executed equals: 0.

clock wait: 2 seconds.
queue pulse.
self assert: executed equals: 0.

task scheduledTime: now + 2 seconds.
queue schedule: task.
queue pulse.
self assert: executed equals: 1.

]

{ #category : #examples }
TaskAtQueueExamples >> rescheduleTaskForward [
"{{gtClass:BlTaskAtQueue}} allows tasks to be rescheduled, changing their time forwards or backwards.
Demonstrate rescheduling tasks, changing it's time so that it is run earlier"
<gtExample>
| clock queue now task executed |

executed := 0.
clock := BlSimulatedTime new.
queue := TaskAtQueue new
clock: clock.
now := clock currentTime.
task := TaskAtQueueTask
valuable: [ executed := executed + 1 ]
at: now + 10 seconds
entryIdentifier: self class name.
queue schedule: task.
self assert: executed equals: 0.

queue pulse.
self assert: executed equals: 0.

clock wait: 2 seconds.

queue pulse.
self assert: executed equals: 0.

task scheduledTime: now + 1 second.
queue schedule: task.
queue pulse.
self assert: executed equals: 1.

]

{ #category : #examples }
TaskAtQueueExamples >> runNow [
"Queue a task that has a scheduled time in the past."
<gtExample>
| queue taskRun |

queue := self taskAtQueue.
taskRun := false.
queue schedule: [ taskRun := true ]
at: self startTime.
self assert: queue size equals: 1.
queue processNextEntry.
self assert: taskRun.
]

{ #category : #examples }
TaskAtQueueExamples >> runOneInOneSecond [
"Queue a task that has a scheduled time in the past."
<gtExample>
| queue taskRun currentTime |

currentTime := self startTime.
queue := self taskAtQueue.
taskRun := false.
queue schedule: [ taskRun := true ]
at: self startTime + 1 second.
self assert: queue size equals: 1.
queue processNextEntry.
self deny: taskRun.
self assert: queue size equals: 1.
queue clock date: (currentTime := currentTime + 1 second).
queue pulse.
self assert: taskRun.
]

{ #category : #examples }
TaskAtQueueExamples >> runPastTime [
"Queue a task that has a scheduled time in the past."
<gtExample>
| queue taskRun |

queue := self taskAtQueue.
taskRun := false.
queue schedule: [ taskRun := true ]
at: self startTime - 1 second.
self assert: queue size equals: 1.
queue processNextEntry.
self assert: taskRun.
]

{ #category : #examples }
TaskAtQueueExamples >> runTwoOneThree [
"Queue three tasks, out of order, and confirm correct execution."
<gtExample>
| queue value currentTime |

currentTime := self startTime.
queue := self taskAtQueue.
value := 0.
queue schedule: [ value := 2 ]
at: self startTime + 2 second.
queue schedule: [ value := 1 ]
at: self startTime + 1 second.
queue schedule: [ value := 3 ]
at: self startTime + 3 second.
self assert: queue size equals: 3.
queue processNextEntry.
self assert: value equals: 0.
queue clock date: (currentTime := currentTime + 1 second).
queue pulse.
self assert: value equals: 1.
self assert: queue size equals: 2.
queue clock date: (currentTime := currentTime + 1 second).
queue pulse.
self assert: value equals: 2.
self assert: queue size equals: 1.
queue clock date: (currentTime := currentTime + 1 second).
queue pulse.
self assert: value equals: 3.
self assert: queue size equals: 0.

]

{ #category : #private }
TaskAtQueueExamples >> startTime [

^ DateAndTime fromString: '2020-11-07T12:19:53.275521+01:00'
]

{ #category : #private }
TaskAtQueueExamples >> taskAtQueue [
"Answer the BlTaskAtQueue used for the examples.
The queue is configured to use a simulated clock with a fixed current time for reproduceable tests."

^ TaskAtQueue new
clock: (BlSimulatedTime new date: self startTime)
]
10 changes: 0 additions & 10 deletions src/Futures/BlTaskAtQueue.class.st

This file was deleted.

20 changes: 14 additions & 6 deletions src/Futures/TaskAtQueue.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,26 @@ TaskAtQueue >> queueReaderLoop [
]

{ #category : #operations }
TaskAtQueue >> schedule: aBlTaskAtTask [
TaskAtQueue >> removeTask: aTaskAtTask [
"Remove the supplied task from the queue.
Return quietly if the task wasn't in the queue."

queue nextOrNilSuchThat: [ :each | each == aTaskAtTask ]
]

{ #category : #operations }
TaskAtQueue >> schedule: aTaskAtTask [
"Add the supplied task to the queue and trigger the semaphore (which will either run the task immediately, or update the timeout, if required).
Return the scheduled task or a similar previously scheduled task."
| aResult |

aBlTaskAtTask queue: self.
aResult := queue nextPut: aBlTaskAtTask.
aResult == aBlTaskAtTask ifFalse: [
aBlTaskAtTask handleNotScheduledBecauseOfPreviousTask: aResult.
aTaskAtTask queue: self.
aResult := queue nextPut: aTaskAtTask.
aResult == aTaskAtTask ifFalse: [
aTaskAtTask handleNotScheduledBecauseOfPreviousTask: aResult.
^ aResult ].
scheduleSemaphore signal.
^ aBlTaskAtTask.
^ aTaskAtTask.
]

{ #category : #'api - queue' }
Expand Down

0 comments on commit 8642e87

Please sign in to comment.