|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.remote.http; |
| 19 | + |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import org.junit.Before; |
| 22 | +import org.junit.Test; |
| 23 | +import org.openqa.selenium.TimeoutException; |
| 24 | +import org.openqa.selenium.environment.webserver.AppServer; |
| 25 | +import org.openqa.selenium.environment.webserver.NettyAppServer; |
| 26 | +import org.openqa.selenium.remote.http.netty.NettyClient; |
| 27 | + |
| 28 | +import java.io.UncheckedIOException; |
| 29 | +import java.net.MalformedURLException; |
| 30 | +import java.net.URI; |
| 31 | +import java.time.Duration; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | + |
| 34 | +import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR; |
| 35 | +import static java.net.HttpURLConnection.HTTP_OK; |
| 36 | +import static java.net.HttpURLConnection.HTTP_UNAVAILABLE; |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.openqa.selenium.remote.http.Contents.asJson; |
| 39 | +import static org.openqa.selenium.remote.http.HttpMethod.GET; |
| 40 | + |
| 41 | +public class RetryRequestTest { |
| 42 | + |
| 43 | + private HttpClient client; |
| 44 | + private static final String REQUEST_PATH = "http://%s:%s/hello"; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setUp() throws MalformedURLException { |
| 48 | + ClientConfig config = ClientConfig.defaultConfig() |
| 49 | + .baseUrl(URI.create("http://localhost:2345").toURL()) |
| 50 | + .readTimeout(Duration.ofMillis(1000)); |
| 51 | + client = new NettyClient.Factory().createClient(config); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void shouldBeAbleToHandleARequest() { |
| 56 | + AtomicInteger count = new AtomicInteger(0); |
| 57 | + AppServer server = new NettyAppServer(req -> { |
| 58 | + count.incrementAndGet(); |
| 59 | + return new HttpResponse(); |
| 60 | + }); |
| 61 | + server.start(); |
| 62 | + |
| 63 | + URI uri = URI.create(server.whereIs("/")); |
| 64 | + HttpRequest request = new HttpRequest( |
| 65 | + GET, |
| 66 | + String.format(REQUEST_PATH, uri.getHost(), uri.getPort())); |
| 67 | + HttpResponse response = client.execute(request); |
| 68 | + |
| 69 | + assertThat(response).extracting(HttpResponse::getStatus).isEqualTo(HTTP_OK); |
| 70 | + |
| 71 | + assertThat(count.get()).isEqualTo(1); |
| 72 | + server.stop(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void shouldBeAbleToRetryARequestOnInternalServerError() { |
| 77 | + AtomicInteger count = new AtomicInteger(0); |
| 78 | + AppServer server = new NettyAppServer(req -> { |
| 79 | + count.incrementAndGet(); |
| 80 | + return new HttpResponse().setStatus(500); |
| 81 | + }); |
| 82 | + server.start(); |
| 83 | + |
| 84 | + URI uri = URI.create(server.whereIs("/")); |
| 85 | + HttpRequest request = new HttpRequest( |
| 86 | + GET, |
| 87 | + String.format(REQUEST_PATH, uri.getHost(), uri.getPort())); |
| 88 | + HttpResponse response = client.execute(request); |
| 89 | + |
| 90 | + assertThat(response).extracting(HttpResponse::getStatus).isEqualTo(HTTP_INTERNAL_ERROR); |
| 91 | + assertThat(count.get()).isEqualTo(3); |
| 92 | + |
| 93 | + server.stop(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void shouldNotRetryRequestOnInternalServerErrorWithContent() { |
| 98 | + AtomicInteger count = new AtomicInteger(0); |
| 99 | + AppServer server = new NettyAppServer(req -> { |
| 100 | + count.incrementAndGet(); |
| 101 | + return new HttpResponse() |
| 102 | + .setStatus(500) |
| 103 | + .setContent(asJson(ImmutableMap.of("error", "non-transient"))); |
| 104 | + }); |
| 105 | + server.start(); |
| 106 | + |
| 107 | + URI uri = URI.create(server.whereIs("/")); |
| 108 | + HttpRequest request = new HttpRequest( |
| 109 | + GET, |
| 110 | + String.format(REQUEST_PATH, uri.getHost(), uri.getPort())); |
| 111 | + HttpResponse response = client.execute(request); |
| 112 | + |
| 113 | + assertThat(response).extracting(HttpResponse::getStatus).isEqualTo(HTTP_INTERNAL_ERROR); |
| 114 | + assertThat(count.get()).isEqualTo(1); |
| 115 | + |
| 116 | + server.stop(); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void shouldRetryRequestOnServerUnavailableError() { |
| 121 | + AtomicInteger count = new AtomicInteger(0); |
| 122 | + AppServer server = new NettyAppServer(req -> { |
| 123 | + count.incrementAndGet(); |
| 124 | + return new HttpResponse() |
| 125 | + .setStatus(503) |
| 126 | + .setContent(asJson(ImmutableMap.of("error", "server down"))); |
| 127 | + }); |
| 128 | + server.start(); |
| 129 | + |
| 130 | + URI uri = URI.create(server.whereIs("/")); |
| 131 | + HttpRequest request = new HttpRequest( |
| 132 | + GET, |
| 133 | + String.format(REQUEST_PATH, uri.getHost(), uri.getPort())); |
| 134 | + HttpResponse response = client.execute(request); |
| 135 | + |
| 136 | + assertThat(response).extracting(HttpResponse::getStatus).isEqualTo(HTTP_UNAVAILABLE); |
| 137 | + assertThat(count.get()).isEqualTo(3); |
| 138 | + |
| 139 | + server.stop(); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void shouldBeAbleToRetryARequestOnTimeout() { |
| 144 | + AtomicInteger count = new AtomicInteger(0); |
| 145 | + AppServer server = new NettyAppServer(req -> { |
| 146 | + count.incrementAndGet(); |
| 147 | + try { |
| 148 | + Thread.sleep(2000); |
| 149 | + } catch (InterruptedException e) { |
| 150 | + e.printStackTrace(); |
| 151 | + } |
| 152 | + return new HttpResponse(); |
| 153 | + }); |
| 154 | + server.start(); |
| 155 | + |
| 156 | + URI uri = URI.create(server.whereIs("/")); |
| 157 | + HttpRequest request = new HttpRequest( |
| 158 | + GET, |
| 159 | + String.format(REQUEST_PATH, uri.getHost(), uri.getPort())); |
| 160 | + |
| 161 | + try { |
| 162 | + client.execute(request); |
| 163 | + } catch (TimeoutException e) { |
| 164 | + assertThat(count.get()).isEqualTo(4); |
| 165 | + } finally { |
| 166 | + server.stop(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + @Test(expected = UncheckedIOException.class) |
| 171 | + public void shouldBeAbleToRetryARequestOnConnectionFailure() { |
| 172 | + AppServer server = new NettyAppServer(req -> new HttpResponse()); |
| 173 | + |
| 174 | + URI uri = URI.create(server.whereIs("/")); |
| 175 | + HttpRequest request = new HttpRequest( |
| 176 | + GET, |
| 177 | + String.format(REQUEST_PATH, uri.getHost(), uri.getPort())); |
| 178 | + |
| 179 | + client.execute(request); |
| 180 | + } |
| 181 | +} |
0 commit comments