List of usage examples for org.apache.http.util Asserts check
public static void check(boolean z, String str)
From source file:io.parallec.sample.app.http.HttpDiffRequestsSameServerApp.java
public static void main(String[] args) { ParallelClient pc = new ParallelClient(); Map<String, Object> responseContext = new HashMap<String, Object>(); responseContext.put("temp", null); pc.prepareHttpGet("/userdata/sample_weather_$ZIP.txt") .setReplaceVarMapToSingleTargetSingleVar("ZIP", Arrays.asList("95037", "48824"), "www.parallec.io") .setResponseContext(responseContext).execute(new ParallecResponseHandler() { public void onCompleted(ResponseOnSingleTask res, Map<String, Object> responseContext) { String temp = new FilterRegex("(.*)").filter(res.getResponseContent()); System.out.println("\n!!Temperature: " + temp + " TargetHost: " + res.getHost()); responseContext.put("temp", temp); }/*from w w w. j a v a2 s .c om*/ }); int tempGlobal = Integer.parseInt((String) responseContext.get("temp")); Asserts.check(tempGlobal <= 100 && tempGlobal >= 0, " Fail to extract output from sample weather API. Fail different request to same server test"); pc.releaseExternalResources(); }
From source file:io.parallec.core.main.http.ParallelClientHttpBasicTimeoutTest.java
/** * Hit websites min sync./*w w w .j a v a 2s . c om*/ */ @Test public void hitWebsitesMinSyncTimeout() { ParallelTaskConfig config = new ParallelTaskConfig(); config.setTimeoutInManagerSec(0); Map<String, Object> responseContext = new HashMap<String, Object>(); ParallelTask task = pc.prepareHttpGet("/validateInternals.html").setConcurrency(1700) .setResponseContext(responseContext).handleInWorker().setConfig(config) .setTargetHostsFromString("www.parallec.io www.jeffpei.com www.restcommander.com") .execute(new ParallecResponseHandler() { @Override public void onCompleted(ResponseOnSingleTask res, Map<String, Object> responseContext) { ; } }); Asserts.check(task.getParallelTaskResult().get("www.parallec.io").getSingleTaskResponse().getErrorMessage() .contains("Execution manager timeout"), " Fail to get 3 results"); logger.info("Task Pretty Print: \n{}", task.prettyPrintInfo()); }
From source file:io.parallec.core.main.http.ParallelClientHttpResponseHeaderTest.java
/** * Hit websites and get all headers//from w ww . j a v a2 s .co m */ @Test public void hitWebsitesMinSyncWithResponseHeadersAll() { Map<String, Object> responseContext = new HashMap<String, Object>(); pc.prepareHttpGet("/validateInternals.html").setConcurrency(1700).handleInWorker() .saveResponseHeaders(new ResponseHeaderMeta(null, true)) .setTargetHostsFromString("www.parallec.io www.jeffpei.com www.restcommander.com") .execute(new ParallecResponseHandler() { @Override public void onCompleted(ResponseOnSingleTask res, Map<String, Object> responseContext) { Map<String, List<String>> responseHeaders = res.getResponseHeaders(); for (Entry<String, List<String>> entry : responseHeaders.entrySet()) { logger.info("response header (lowed case key): {} - {}", entry.getKey(), entry.getValue()); } responseContext.put(res.getHost(), responseHeaders.size()); logger.debug(res.toString()); } }); for (Object o : responseContext.values()) { int headerKeySize = Integer.parseInt((String) o); Asserts.check(headerKeySize > 0, " Fail to extract http header"); } // logger.info("Task Pretty Print: \n{}", task.prettyPrintInfo()); }
From source file:io.parallec.core.main.http.ParallelClientHttpBasicTest.java
/** * Hit websites min sync./*from w ww . ja v a 2 s . c o m*/ */ @Test public void hitWebsitesMinSync() { Map<String, Object> responseContext = new HashMap<String, Object>(); ParallelTask task = pc.prepareHttpGet("/validateInternals.html").setConcurrency(1700).handleInWorker() .setTargetHostsFromString("www.parallec.io www.jeffpei.com www.restcommander.com") .execute(new ParallecResponseHandler() { @Override public void onCompleted(ResponseOnSingleTask res, Map<String, Object> responseContext) { String cpu = new FilterRegex(".*<td>CPU-Usage-Percent</td>\\s*<td>(.*?)</td>.*") .filter(res.getResponseContent()); String memory = new FilterRegex(".*<td>Memory-Used-KB</td>\\s*<td>(.*?)</td>.*") .filter(res.getResponseContent()); Map<String, Object> metricMap = new HashMap<String, Object>(); metricMap.put("CpuUsage", cpu); metricMap.put("MemoryUsage", memory); logger.info("cpu:" + cpu + " memory: " + memory + " host: " + res.getHost()); responseContext.put(res.getHost(), cpu); logger.debug(res.toString()); } }); for (Object o : responseContext.values()) { Double cpuDouble = Double.parseDouble((String) o); Asserts.check(cpuDouble <= 100.0 && cpuDouble >= 0.0, " Fail to extract cpu values"); } logger.info("Task Pretty Print: \n{}", task.prettyPrintInfo()); }
From source file:io.parallec.core.main.http.ParallelClientHttpLogTest.java
/** * Hit websites min sync./*from w w w . j a v a2 s . c o m*/ */ @Test public void hitWebsitesMinSync() { Map<String, Object> responseContext = new HashMap<String, Object>(); ParallelTask pt = pc.prepareHttpGet("/validateInternals.html").setConcurrency(1700) .setTargetHostsFromString("www.parallec.io www.jeffpei.com www.restcommander.com") .execute(new ParallecResponseHandler() { @Override public void onCompleted(ResponseOnSingleTask res, Map<String, Object> responseContext) { String cpu = new FilterRegex(".*<td>CPU-Usage-Percent</td>\\s*<td>(.*?)</td>.*") .filter(res.getResponseContent()); String memory = new FilterRegex(".*<td>Memory-Used-KB</td>\\s*<td>(.*?)</td>.*") .filter(res.getResponseContent()); Map<String, Object> metricMap = new HashMap<String, Object>(); metricMap.put("CpuUsage", cpu); metricMap.put("MemoryUsage", memory); logger.info("cpu:" + cpu + " memory: " + memory + " host: " + res.getHost()); responseContext.put(res.getHost(), cpu); // logger.info(res.toString()); } }); logger.info("Response details: " + PcStringUtils.renderJson(pt.getParallelTaskResult())); for (Object o : responseContext.values()) { Double cpuDouble = Double.parseDouble((String) o); Asserts.check(cpuDouble <= 100.0 && cpuDouble >= 0.0, " Fail to extract cpu values"); } }
From source file:io.parallec.core.main.http.ParallelClientHttpBasicMoreOptionsTest.java
/** * basic but auto save response to log .setAutoSaveLogToLocal(true) * .setEnableCapacityAwareTaskScheduler(true) .setSaveResponseToTask(true) *//*from w ww .j a v a 2 s .c om*/ @Test public void hitWebsitesMinSync() { Map<String, Object> responseContext = new HashMap<String, Object>(); ParallelTask task = pc.prepareHttpGet("/validateInternals.html").setConcurrency(1700) .setTargetHostsFromString("www.parallec.io www.jeffpei.com www.restcommander.com") .setSaveResponseToTask(true).setAutoSaveLogToLocal(true).setEnableCapacityAwareTaskScheduler(true) // .setSaveResponseToTask(true) .execute(new ParallecResponseHandler() { @Override public void onCompleted(ResponseOnSingleTask res, Map<String, Object> responseContext) { String cpu = new FilterRegex(".*<td>CPU-Usage-Percent</td>\\s*<td>(.*?)</td>.*") .filter(res.getResponseContent()); String memory = new FilterRegex(".*<td>Memory-Used-KB</td>\\s*<td>(.*?)</td>.*") .filter(res.getResponseContent()); Map<String, Object> metricMap = new HashMap<String, Object>(); metricMap.put("CpuUsage", cpu); metricMap.put("MemoryUsage", memory); logger.info("cpu:" + cpu + " memory: " + memory + " host: " + res.getHost()); responseContext.put(res.getHost(), cpu); logger.info(res.toString()); } }); for (Object o : responseContext.values()) { Double cpuDouble = Double.parseDouble((String) o); Asserts.check(cpuDouble <= 100.0 && cpuDouble >= 0.0, " Fail to extract cpu values"); } logger.info("Task Pretty Print: \n{}", task.prettyPrintInfo()); logger.info("Aggregated results: \n{}", task.getAggregatedResultHumanStr()); }
From source file:io.parallec.core.main.http.ParallelClientHttpResponseHeaderTest.java
/** * Hit websites min sync and get response headers with certain list of keys. * //from w ww . j a v a 2 s .c o m * the x-github-request-id key is not present in amazone S3 responses. So * the response header size should be [3,2,2] * * note that the key set provided in ResponseHeaderMeta in request can be case insensitive. However in the * response headers map returned in the ResponseOnSingleTask, all keys have been lower case for easy access. * */ @Test public void hitWebsitesMinSyncWithResponseHeadersSubset() { Map<String, Object> responseContext = new HashMap<String, Object>(); pc.prepareHttpGet("/validateInternals.html").setConcurrency(1700).handleInWorker() .saveResponseHeaders(new ResponseHeaderMeta( Arrays.asList("Content-Type", "server", "x-github-request-id"), false)) .setTargetHostsFromString("www.parallec.io www.jeffpei.com www.restcommander.com") .execute(new ParallecResponseHandler() { @Override public void onCompleted(ResponseOnSingleTask res, Map<String, Object> responseContext) { Map<String, List<String>> responseHeaders = res.getResponseHeaders(); for (Entry<String, List<String>> entry : responseHeaders.entrySet()) { logger.info("response header (lowed case key): {} - {}", entry.getKey(), entry.getValue()); } responseContext.put(res.getHost(), responseHeaders.size()); logger.debug(res.toString()); } }); for (Object o : responseContext.values()) { int headerKeySize = Integer.parseInt((String) o); Asserts.check(headerKeySize >= 2 && headerKeySize <= 3, " Fail to extract http header subset"); } // logger.info("Task Pretty Print: \n{}", task.prettyPrintInfo()); }
From source file:org.apache.synapse.transport.nhttp.NhttpSharedOutputBuffer.java
/** * {@inheritDoc}// w w w .j a v a2 s . c o m */ public void write(final byte[] b, final int off, final int len) throws IOException { if (b == null) { return; } int pos = off; this.lock.lock(); try { Asserts.check(!this.shutdown && !this.endOfStream, "Buffer already closed for writing"); // Set buffer to write mode setInputMode(); int remaining = len; while (remaining > 0) { // At the moment buffer is in the write mode. So position equals to capacity. // if position >= limit(= capacity) -> true i.e. buffer is full // If this buffer is full, that means the data should be written to output buffer (Need to flush). if (!this.buffer.hasRemaining()) { flushContent(); // If awaitInterrupted was set to false, that means something went wrong(timeout happened) // therefore, we clear the buffer and stop the flow with an exception. if (!awaitInterrupted) { this.buffer.clear(); throw new IOException("Output buffer write time out exceeded"); } // Set buffer to write mode. (Inside flushContent method, buffer was set to read mode) setInputMode(); } final int chunk = Math.min(remaining, this.buffer.remaining()); this.buffer.put(b, pos, chunk); remaining -= chunk; pos += chunk; } } finally { this.lock.unlock(); } }
From source file:com.serphacker.serposcope.scraper.http.extensions.CloseableBasicHttpClientConnectionManager.java
synchronized HttpClientConnection getConnection(final HttpRoute route, final Object state) { Asserts.check(!this.isShutdown.get(), "Connection manager has been shut down"); if (this.log.isDebugEnabled()) { this.log.debug("Get connection for route " + route); }//w w w . j av a2s . c o m Asserts.check(!this.leased, "Connection is still allocated"); if (!LangUtils.equals(this.route, route) || !LangUtils.equals(this.state, state)) { closeConnection(); } this.route = route; this.state = state; checkExpiry(); if (this.conn == null) { this.conn = this.connFactory.create(route, this.connConfig); } this.leased = true; return this.conn; }
From source file:com.serphacker.serposcope.scraper.http.extensions.CloseableBasicHttpClientConnectionManager.java
@Override public synchronized void releaseConnection(final HttpClientConnection conn, final Object state, final long keepalive, final TimeUnit tunit) { Args.notNull(conn, "Connection"); Asserts.check(conn == this.conn, "Connection not obtained from this manager"); if (this.log.isDebugEnabled()) { this.log.debug("Releasing connection " + conn); }/*from w w w . j a va 2s. c o m*/ if (this.isShutdown.get()) { return; } try { this.updated = System.currentTimeMillis(); if (!this.conn.isOpen()) { this.conn = null; this.route = null; this.conn = null; this.expiry = Long.MAX_VALUE; } else { this.state = state; if (this.log.isDebugEnabled()) { final String s; if (keepalive > 0) { s = "for " + keepalive + " " + tunit; } else { s = "indefinitely"; } this.log.debug("Connection can be kept alive " + s); } if (keepalive > 0) { this.expiry = this.updated + tunit.toMillis(keepalive); } else { this.expiry = Long.MAX_VALUE; } } } finally { this.leased = false; } }