Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add $*CLOCK so we can mock it in S02-builtin_data_types/instants-and-durations.t #18

Closed
wants to merge 4 commits into from

3 participants

@dwhipp

Defined a role, POSIX-Clock that defines the basic ways that we interact with the system wallclock.
Defined a class, System-Clock, that delegates to time__n, time__I, and sleep__vN pir functions
Define $CLOCK as the current instance of the clock
re-implemented term, term, and sleep to use $
CLOCK

With this change it will be trivial to mock the wallclock so that tests no longer defend on system time (and thus run faster, and are more reliable). For example, S02-builtin_data_types/instants-and-durations.t can add:

class Mock-Clock does POSIX-Clock {
has $.time is rw;
method time_as_real() { $.time }
method sleep($seconds) { $.time += $seconds }
}
$*CLOCK = Mock-Clock.new( :time(42.5) );

@moritz
Owner

I like the idea, but please name the coercion method just Real -- it is convention in Perl 6 to name coercion methods the same as the type they coerce to.

I also don't see the utility of the POSIX-Clock, which by itself would just loop infinitely by mutually recursive calls to time_as_real and time_as_int.

Finally I don't like the class names... maybe Clock::System and Clock::POSIX instead?

@dwhipp

I'm not wedded to the current implementation, but:

To name the coercion method just "Real" (and, presumably, Int) would imply that it is the clock itself that it being coerced. Seems a little strange.

When you say that you don't see the utility of POSIX-Clock, do you mean that you don't see the need for a role, or just that the default implementations of the two "time-as" methods seems unnecessary. The reason for doing it that was was so that concrete clocks need to implement only one of the two methods. The only use-case for this is to use mocks in the test suite, so I could break the cycle and define .Real as {...}.

@scottp scottp referenced this pull request from a commit in scottp/rakudo
@pmichaud pmichaud Release #18 is no longer "planned" -- it "happened". masak++ 7032827
@pmichaud pmichaud closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 3, 2011
  1. @dwhipp
  2. @dwhipp
Commits on Mar 4, 2011
  1. @dwhipp
Commits on Mar 6, 2011
  1. @dwhipp
This page is out of date. Refresh to see the latest.
View
1  build/Makefile.in
@@ -224,6 +224,7 @@ CORE_SOURCES = \
src/core/Regex.pm \
src/core/Junction.pm \
src/core/Grammar.pm \
+ src/core/Clock.pm \
src/core/system.pm \
src/cheats/process.pm \
src/core/tai-utc.pm \
View
2  src/cheats/process.pm
@@ -30,6 +30,8 @@ package PROCESS {
our $TZ = ::DateTime-local-timezone.new;
+ our $CLOCK = ::Clock::System.new;
+
Q:PIR {
## set up $*OS, $*OSVER $*EXECUTABLE_NAME
.include 'sysinfo.pasm'
View
17 src/core/Clock.pm
@@ -0,0 +1,17 @@
+role Clock::POSIX {
+ method Int() { floor self.Real }
+ method Real() { ... }
+ method sleep($seconds) { ... }
+}
+
+sub sleep($seconds = Inf) { # fractional seconds also allowed
+ my $time1 = time;
+ $*CLOCK.sleep($seconds);
+ my $time2 = time;
+ return $time2 - $time1;
+}
+
+sub term:<time>() {
+ $*CLOCK.Int;
+}
+
View
2  src/core/Instant.pm
@@ -107,5 +107,5 @@ our multi sub infix:<->(Instant $a, Real $b) {
our sub term:<now>() {
# FIXME: During a leap second, the returned value is one
# second greater than it should be.
- Instant.from-posix: pir::time__n
+ Instant.from-posix: $*CLOCK.Real;
}
View
20 src/core/system.pm
@@ -17,17 +17,15 @@ sub run($commandline) {
}
}
-sub sleep($seconds = Inf) { # fractional seconds also allowed
- my $time1 = time;
- if $seconds ~~ Inf {
- pir::sleep__vN(1e16) while True;
- } else {
- pir::sleep__vN($seconds);
+class Clock::System does Clock::POSIX {
+ method Int() { pir::time__I() }
+ method Real() { pir::time__n() }
+ method sleep($seconds) {
+ if $seconds ~~ Inf {
+ pir::sleep__vN(1e16) while True;
+ } else {
+ pir::sleep__vN($seconds);
+ }
}
- my $time2 = time;
- return $time2 - $time1;
}
-sub term:<time>() {
- pir::time__I()
-}
Something went wrong with that request. Please try again.