Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #3 from am0c/am0c/condvar

Add `condvar' feature as in AnyEvent
  • Loading branch information...
commit 4d26dabf7b0b626c7f26a720479ce9ed65e5d3d9 2 parents d52c76c + 983f74a
@tadzik authored
Showing with 58 additions and 0 deletions.
  1. +26 −0 lib/MuEvent.pm
  2. +32 −0 t/04-condvar.t
View
26 lib/MuEvent.pm
@@ -6,6 +6,23 @@ my @sockets;
my @idlers;
my $since;
+class MuEvent::Condvar {
+ has &.cb;
+ has $.sent is rw;
+ has $.flag is rw = False;
+
+ method send($data?) {
+ &.cb() if &.cb;
+ $.sent = $data if $data.defined;
+ $.flag = True;
+ }
+ method recv() {
+ $since = clock() unless $since.defined;
+ MuEvent::_poll until $.flag;
+ $.sent;
+ }
+}
+
#= Add an event run after a certain amount of time
our sub timer(:&cb!, :$after!, :$interval, :%params) {
@timers.push: {
@@ -38,6 +55,15 @@ our sub run {
}
}
+our sub _poll {
+ run-once()
+}
+
+#= Condvar
+our sub condvar(:&cb?) {
+ MuEvent::Condvar.new( cb => &cb );
+}
+
sub run-timers {
my $seen-action = False;
for @timers -> $e is rw {
View
32 t/04-condvar.t
@@ -0,0 +1,32 @@
+use Test;
+use MuEvent;
+
+plan 7;
+
+my $start = time;
+my $plan = 1;
+
+{
+ my $cv = MuEvent::condvar;
+ ok $cv.send( "foo" ), "send";
+ ok $cv.recv ~~ "foo", "recv";
+}
+
+{
+ my $cv = MuEvent::condvar;
+ ok $cv.send( 1 .. 4 ), "send";
+ ok <1 2 3 4> ~~ $cv.recv, "recv";
+}
+
+{
+ my $cv = MuEvent::condvar;
+
+ MuEvent::timer(after => 2, cb => sub {
+ ok $plan++ == 2 && $start+1 <= time <= $start+3, "timer occured";
+ $cv.send;
+ });
+
+ ok $plan++ == 1 && time - $start < 1, "not block timer";
+ $cv.recv;
+ ok $plan++ == 3 && $start+1 <= time <= $start+3, "received blocked";
+}
Please sign in to comment.
Something went wrong with that request. Please try again.