From 779ffc0f8eb51f790fa23670cc39bc4099ddeef2 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Thu, 3 Jun 2021 17:43:00 +0100 Subject: [PATCH] Retry aliases query Also, switch to the stable endpoint, now that Synapse supports it. Fixes https://github.com/matrix-org/sytest/issues/1055 --- run-tests.pl | 45 ++++++++++++++++++++++------------ tests/10apidoc/32room-alias.pl | 34 ++++++++++++++----------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/run-tests.pl b/run-tests.pl index 61b167548..24f0484fe 100755 --- a/run-tests.pl +++ b/run-tests.pl @@ -415,26 +415,41 @@ sub delay $loop->delay_future( after => $secs * $TIMEOUT_FACTOR ); } -# Handy utility wrapper around Future::Utils::try_repeat_until_success which -# includes a delay on retry (and logs the reason for failure) -sub retry_until_success(&) + +# Retries a code block until it returns a successful Future. +# +# Includes a delay between each call, and logs the reason for failure. +# +# The code block is called with the iteration number. +# +# Example: +# +# retry_until_success { +# my ( $iter ) = @_; +# ... +# }, max_iterations => 20, +# initial_delay => 0.01; +# +# Will fail if the code block fails `max_iterations` (default 10) times. +# +sub retry_until_success(&%) { - my ( $code ) = @_; + my ( $code, %params ) = @_; - my $delay = 0.1; + my $delay = $params{initial_delay} // 0.1; + my $max_iter = $params{max_iterations} // 10; my $iter = 0; try_repeat { - ( $iter++ ? - delay( $delay *= 1.5 ) : - Future->done ) - ->then( $code ) - ->on_fail( sub { - my ( $exc ) = @_; - chomp $exc; - log_if_fail("Iteration $iter: not ready yet: $exc"); - }); - } until => sub { !$_[0]->failure }; + ( $iter ? delay( $delay *= 1.5 ) : Future->done ) + ->then( sub { + Future->call( $code, $iter++ ); + })->on_fail( sub { + my ( $exc ) = @_; + chomp $exc; + log_if_fail("Iteration $iter++: not ready yet: $exc"); + }); + } until => sub { $iter > $max_iter || !$_[0]->failure }; } # Another wrapper which repeats (with delay) until the block returns a true diff --git a/tests/10apidoc/32room-alias.pl b/tests/10apidoc/32room-alias.pl index 0f95466a9..ed2ff11f8 100644 --- a/tests/10apidoc/32room-alias.pl +++ b/tests/10apidoc/32room-alias.pl @@ -63,11 +63,11 @@ do_request_json_for( $user, method => "GET", - uri => "/unstable/org.matrix.msc2432/rooms/$room_id/aliases", + uri => "/r0/rooms/$room_id/aliases", ); })->then( sub { my ( $res ) = @_; - log_if_fail "response from /aliases", $res; + log_if_fail "response from /aliases before change:", $res; assert_json_keys( $res, qw( aliases )); assert_json_empty_list( $res->{aliases} ); @@ -79,19 +79,25 @@ uri => "/r0/directory/room/$room_alias", content => { room_id => $room_id }, ); - })->then( sub { - # ... and recheck - do_request_json_for( - $user, - method => "GET", - uri => "/unstable/org.matrix.msc2432/rooms/$room_id/aliases", - ); })->then( sub { my ( $res ) = @_; - log_if_fail "response from /aliases", $res; - - assert_json_keys( $res, qw( aliases )); - assert_deeply_eq($res->{aliases}, [ $room_alias ]); - Future->done; + log_if_fail "response from PUT /directory:", $res; + + # ... and recheck. Might need to try this a few times while the caches + # get flushed. + retry_until_success { + my ( $iter ) = @_; + return do_request_json_for( + $user, + method => "GET", + uri => "/r0/rooms/$room_id/aliases", + )->then( sub { + my ( $res ) = @_; + log_if_fail "$iter: response from /aliases", $res; + assert_json_keys( $res, qw( aliases )); + assert_deeply_eq($res->{aliases}, [ $room_alias ]); + Future->done; + }); + } }); };