09-futures/04-deadline-observation.protos
- Repository
- guillermomolina/protos
- Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635- Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492- Blob
d139002e7057f5d1094f0ac30220b9bf1fdf341d- Source
- View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.
// Deadline observation with the monotonic Timing capability.
//
// Requires: bin/protos --allow-timing protos/tutorials/09-futures/04-deadline-observation.protos
//
// `timing` exists only when the host grants it. Coordinates are exact Integer
// nanoseconds of a monotonic, non-civil domain: never wall-clock time. This
// lesson checks its own results and reports them through an explicit Process
// writer; it prints no raw timing values and asserts no elapsed durations.
Assertions: import("std:test/assertions")
ProcessStreams: import("std:io/process_streams")
out: ProcessStreams.stdoutWriter(process)
report: (line) => { out.writeLine(line).value() }
millisecond: 1000000
// 1. Sample the monotonic coordinate: an Integer that never decreases.
first: timing.monotonicNanoseconds()
second: timing.monotonicNanoseconds()
Assertions.require(second >= first)
report("monotonic samples never decrease")
// 2. A one-shot timer resolves to null no earlier than its absolute deadline.
Assertions.require(timing.atNanoseconds(timing.monotonicNanoseconds() + millisecond).value() === null)
report("one-shot timer resolved")
// 3. An already successful Future observed against a future deadline mirrors
// the Future's value.
done: (() => { 6 * 7 }).future()
done.value()
observed: timing.observeUntil(done, timing.monotonicNanoseconds() + 1000 * millisecond)
Assertions.require(observed.value() == 42)
report("completed work observed before the deadline")
// 4. Admission at or after the deadline deterministically selects
// DeadlineExceeded, whatever the source's state.
slow: timing.atNanoseconds(timing.monotonicNanoseconds() + 60000 * millisecond)
reached: timing.monotonicNanoseconds()
expired: DeadlineExceeded.handle(
() => { timing.observeUntil(slow, reached).value(); false },
(error) => { true }
)
Assertions.require(expired)
report("reached deadline selected DeadlineExceeded")
// 5. Observation does not own the source: the slow timer is still pending and
// is cancelled only by its own explicit decision.
slow.cancel()
Assertions.require(Cancelled.handle(() => { slow.value(); false }, (error) => { true }))
report("source work remained independent of the observation")
out.close().value()