protected void waitForServerReady()

in src/main/java/org/apache/sling/testing/serversetup/instance/SlingTestBase.java [294:381]


    protected void waitForServerReady() throws Exception {
        if(slingTestState.isServerReady()) {
            return;
        }
        if(slingTestState.isServerReadyTestFailed()) {
            fail("Server is not ready according to previous tests");
        }

        // Timeout for readiness test
        TimeoutsProvider tp = TimeoutsProvider.getInstance();
        final String sec = systemProperties.getProperty(SERVER_READY_TIMEOUT_PROP, "60");
        final int timeoutSec = tp.getTimeout(Integer.valueOf(sec));

        final String initialDelaySec = systemProperties.getProperty(SERVER_READY_TIMEOUT_INITIAL_DELAY_PROP, "0");
        final int timeoutInitialDelaySec = tp.getTimeout(Integer.valueOf(initialDelaySec));
        final int timeoutInitialDelayMs = timeoutInitialDelaySec * 1000;

        final String delaySec = systemProperties.getProperty(SERVER_READY_TIMEOUT_DELAY_PROP, "1");
        final int timeoutDelaySec = tp.getTimeout(Integer.valueOf(delaySec));
        final int timeoutDelayMs = timeoutDelaySec * 1000;

        log.info("Will wait up to {} seconds for server to become ready with a {} second initial delay and {} seconds between each check",
                new Object[] {timeoutSec, timeoutInitialDelaySec, timeoutDelaySec});
        final long endTime = System.currentTimeMillis() + timeoutSec * 1000L;

        // Get the list of paths to test and expected content regexps
        final List<String> testPaths = new ArrayList<String>();
        final TreeSet<Object> propertyNames = new TreeSet<Object>();
        propertyNames.addAll(systemProperties.keySet());
        for(Object o : propertyNames) {
            final String key = (String)o;
            if(key.startsWith(SERVER_READY_PROP_PREFIX)) {
                testPaths.add(systemProperties.getProperty(key));
            }
        }

        if (timeoutInitialDelayMs > 0) {
            // wait for the initial deal duration
            Thread.sleep(timeoutInitialDelayMs);
        }

        // Consider the server ready if it responds to a GET on each of
        // our configured request paths with a 200 result and content
        // that contains the pattern that's optionally supplied with the
        // path, separated by a colon
        log.info("Checking that GET requests return expected content (timeout={} seconds): {}", timeoutSec, testPaths);
        while (System.currentTimeMillis() < endTime) {
            boolean errors = false;
            for (String p : testPaths) {
                final String [] s = p.split(":");
                final String path = s[0];
                final String pattern = (s.length > 0 ? s[1] : "");
                boolean isRegex = s.length > 1 ? "regexp".equals(s[2]) : false;
                try {
                    URI uri = new URI(path);
                    List<NameValuePair> reqParams = extractParams(uri);
                    SlingHttpResponse get = osgiConsoleClient.doGet(uri.getPath(), reqParams, 200);
                    if (isRegex) {
                        get.checkContentRegexp(pattern);
                    } else {
                        get.checkContentContains(pattern);
                    }
                } catch(ClientException e) {
                    errors = true;
                    log.debug("Request to {}@{} failed, will retry ({})",
                            new Object[] { serverUsername, osgiConsoleClient.getUrl(path), e});
                } catch(Exception e) {
                    errors = true;
                    log.debug("Request to {}@{} failed, will retry ({})",
                            new Object[] { serverUsername, osgiConsoleClient.getUrl(path), pattern, e });
                }
            }

            if (!errors) {
                slingTestState.setServerReady(true);
                log.info("All {} paths return expected content, server ready", testPaths.size());
                break;
            }
            Thread.sleep(timeoutDelayMs);
        }

        if (!slingTestState.isServerReady()) {
            slingTestState.setServerReadyTestFailed(true);
            final String msg = "Server not ready after " + timeoutSec + " seconds, giving up";
            log.info(msg);
            fail(msg);
        }
    }