|
| 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 | +package org.openqa.selenium.net; |
| 18 | + |
| 19 | +import static java.lang.System.currentTimeMillis; |
| 20 | +import static org.hamcrest.Matchers.lessThan; |
| 21 | +import static org.junit.Assert.assertThat; |
| 22 | +import static org.openqa.selenium.net.PortProber.findFreePort; |
| 23 | + |
| 24 | +import org.junit.After; |
| 25 | +import org.junit.Test; |
| 26 | +import org.junit.runner.RunWith; |
| 27 | +import org.junit.runners.JUnit4; |
| 28 | +import org.seleniumhq.jetty9.server.Handler; |
| 29 | +import org.seleniumhq.jetty9.server.HttpConnection; |
| 30 | +import org.seleniumhq.jetty9.server.Request; |
| 31 | +import org.seleniumhq.jetty9.server.Server; |
| 32 | +import org.seleniumhq.jetty9.server.ServerConnector; |
| 33 | +import org.seleniumhq.jetty9.server.handler.AbstractHandler; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.net.URL; |
| 37 | +import java.util.concurrent.Callable; |
| 38 | +import java.util.concurrent.ExecutorService; |
| 39 | +import java.util.concurrent.Executors; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | + |
| 42 | +import javax.servlet.ServletException; |
| 43 | +import javax.servlet.http.HttpServletRequest; |
| 44 | +import javax.servlet.http.HttpServletResponse; |
| 45 | + |
| 46 | +@RunWith(JUnit4.class) |
| 47 | +public class UrlCheckerTest { |
| 48 | + |
| 49 | + private final UrlChecker urlChecker = new UrlChecker(); |
| 50 | + int port = findFreePort(); |
| 51 | + Server server = buildServer(); |
| 52 | + |
| 53 | + private Server buildServer() { |
| 54 | + Server server = new Server(port); |
| 55 | + server.setHandler(new AbstractHandler() { |
| 56 | + @Override |
| 57 | + public void handle(String s, Request request, HttpServletRequest httpServletRequest, |
| 58 | + HttpServletResponse httpServletResponse) |
| 59 | + throws IOException, ServletException { |
| 60 | + httpServletResponse.setStatus(200); |
| 61 | + httpServletResponse.getWriter().println("<h1>Working</h1>"); |
| 62 | + request.setHandled(true); |
| 63 | + } |
| 64 | + }); |
| 65 | + return server; |
| 66 | + } |
| 67 | + |
| 68 | + ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 69 | + |
| 70 | + @Test |
| 71 | + public void testWaitUntilAvailableIsTimely() throws Exception { |
| 72 | + |
| 73 | + executorService.submit(new Callable<Object>() { |
| 74 | + @Override |
| 75 | + public Object call() throws Exception { |
| 76 | + Thread.sleep(10L); |
| 77 | + server.start(); |
| 78 | + return null; |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + long start = currentTimeMillis(); |
| 83 | + urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL("http://localhost:" + port + "/")); |
| 84 | + long elapsed = currentTimeMillis() - start; |
| 85 | + assertThat(elapsed, lessThan(450L)); |
| 86 | + System.out.println(elapsed); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testWaitUntilUnavailableIsTimely() throws Exception { |
| 91 | + |
| 92 | + server.start(); |
| 93 | + urlChecker.waitUntilAvailable(10, TimeUnit.SECONDS, new URL("http://localhost:" + port + "/")); |
| 94 | + |
| 95 | + executorService.submit(new Callable<Object>() { |
| 96 | + @Override |
| 97 | + public Object call() throws Exception { |
| 98 | + Thread.sleep(10L); |
| 99 | + server.stop(); |
| 100 | + return null; |
| 101 | + } |
| 102 | + }); |
| 103 | + |
| 104 | + long start = currentTimeMillis(); |
| 105 | + urlChecker.waitUntilUnavailable(10, TimeUnit.SECONDS, |
| 106 | + new URL("http://localhost:" + port + "/")); |
| 107 | + long elapsed = currentTimeMillis() - start; |
| 108 | + assertThat(elapsed, lessThan(450L)); |
| 109 | + System.out.println(elapsed); |
| 110 | + } |
| 111 | + |
| 112 | + @After |
| 113 | + public void cleanup() throws Exception { |
| 114 | + server.stop(); |
| 115 | + server.join(); |
| 116 | + executorService.shutdown(); |
| 117 | + } |
| 118 | +} |
0 commit comments