From: Michael Stapelberg Date: Thu, 22 Sep 2011 19:30:24 +0000 (+0100) Subject: lib/i3test.pm: Don’t sleep(0.25), but wait until the window was mapped X-Git-Tag: 4.1~139^2~14 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=83560c85d8e972e6915cdc42d3f9b8ac8a315351;p=i3%2Fi3 lib/i3test.pm: Don’t sleep(0.25), but wait until the window was mapped This makes it faster and less racey --- diff --git a/testcases/t/lib/i3test.pm b/testcases/t/lib/i3test.pm index 3d2d85af..f8923667 100644 --- a/testcases/t/lib/i3test.pm +++ b/testcases/t/lib/i3test.pm @@ -7,6 +7,7 @@ use X11::XCB::Rect; use X11::XCB::Window; use X11::XCB qw(:all); use AnyEvent::I3; +use EV; use List::Util qw(first); use List::MoreUtils qw(lastval); use Time::HiRes qw(sleep); @@ -57,6 +58,7 @@ sub open_standard_window { class => WINDOW_CLASS_INPUT_OUTPUT, rect => X11::XCB::Rect->new(x => 0, y => 0, width => 30, height => 30 ), background_color => $color, + event_mask => [ 'structure_notify' ], ); if (defined($floating) && $floating) { @@ -68,7 +70,29 @@ sub open_standard_window { $window->name('Window ' . counter_window()); $window->map; - sleep(0.25); + # wait for the mapped event with a timeout of 0.25s + my $cv = AE::cv; + + my $prep = EV::prepare sub { + $x->flush; + }; + + my $check = EV::check sub { + while (defined(my $event = $x->poll_for_event)) { + if ($event->{response_type} == MAP_NOTIFY) { + $cv->send(0) + } + } + }; + + my $watcher = EV::io $x->get_file_descriptor, EV::READ, sub { + # do nothing, we only need this watcher so that EV picks up the events + }; + + # Trigger timeout after 0.25s + my $timeout = AE::timer 0.5, 0, sub { say STDERR "timeout"; $cv->send(1) }; + + my $result = $cv->recv; return $window; }