use of okhttp3.Dispatcher in project OpenRefine by OpenRefine.
the class ExtendDataOperationTests method testFetchRecord.
/**
* Test fetch records (multiple values per reconciled cell)
*/
@Test
public void testFetchRecord() throws Exception {
DataExtensionConfig extension = DataExtensionConfig.reconstruct("{\"properties\":[{\"id\":\"P38\",\"name\":\"currency\",\"settings\":{\"rank\":\"any\"}}]}");
mockHttpCall("{\"ids\":[\"Q863\",\"Q794\",\"Q17\",\"Q30\"],\"properties\":[{\"id\":\"P38\",\"settings\":{\"rank\":\"any\"}}]}", "{\"rows\": {" + " \"Q794\": {\"P38\": [{\"name\": \"Iranian rial\", \"id\": \"Q188608\"}]}," + " \"Q863\": {\"P38\": [{\"name\": \"Tajikistani somoni\", \"id\": \"Q199886\"}, {\"name\": \"Tajikistani ruble\", \"id\": \"Q2423956\"}]}," + " \"Q30\": {\"P38\": [{\"name\": \"United States dollar\", \"id\": \"Q4917\"}]}," + " \"Q17\": {\"P38\": [{\"name\": \"Japanese yen\", \"id\": \"Q8146\"}]}" + "}," + "\"meta\": [" + " {\"settings\": {\"rank\": \"any\"}, \"name\": \"currency\", \"id\": \"P38\"}" + "]}");
try (MockWebServer server = new MockWebServer()) {
server.start();
server.setDispatcher(dispatcher);
EngineDependentOperation op = new ExtendDataOperation(engine_config, "country", server.url("/reconcile").url().toString(), RECON_IDENTIFIER_SPACE, RECON_SCHEMA_SPACE, extension, 1);
LongRunningProcessStub process = new LongRunningProcessStub(op.createProcess(project, options));
process.run();
/*
* Tajikistan has one "preferred" currency and one "normal" one (in terms of statement ranks). The second
* currency is fetched as well, which creates a record (the cell to the left of it is left blank).
*/
Assert.assertTrue("Tajikistani somoni".equals(project.rows.get(2).getCellValue(1)), "Bad currency name for Tajikistan");
Assert.assertTrue("Tajikistani ruble".equals(project.rows.get(3).getCellValue(1)), "Bad currency name for Tajikistan");
Assert.assertTrue(null == project.rows.get(3).getCellValue(0));
// Make sure all the values are reconciled
Assert.assertTrue(project.columnModel.getColumnByName("currency").getReconStats().matchedTopics == 5);
}
}
use of okhttp3.Dispatcher in project alluxio by Alluxio.
the class KodoUnderFileSystem method initializeKodoClientConfig.
private static Builder initializeKodoClientConfig(UnderFileSystemConfiguration conf) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(conf.getInt(PropertyKey.UNDERFS_KODO_REQUESTS_MAX));
builder.connectTimeout(conf.getMs(PropertyKey.UNDERFS_KODO_CONNECT_TIMEOUT), TimeUnit.SECONDS);
return builder;
}
use of okhttp3.Dispatcher in project sonarlint-core by SonarSource.
the class MockWebServerExtension method beforeEach.
@Override
public void beforeEach(ExtensionContext context) throws Exception {
server = new MockWebServer();
responsesByPath.clear();
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (responsesByPath.containsKey(request.getPath())) {
return responsesByPath.get(request.getPath());
}
return new MockResponse().setResponseCode(404);
}
};
server.setDispatcher(dispatcher);
server.start();
}
use of okhttp3.Dispatcher in project brave by openzipkin.
the class ITTracingCallFactory method currentSpanVisibleToUserInterceptors.
@Test
public void currentSpanVisibleToUserInterceptors() throws IOException {
server.enqueue(new MockResponse());
closeClient(client);
client = TracingCallFactory.create(httpTracing, new OkHttpClient.Builder().addInterceptor(chain -> chain.proceed(chain.request().newBuilder().addHeader("my-id", currentTraceContext.get().traceIdString()).build())).dispatcher(dispatcher).build());
TraceContext parent = newTraceContext(SamplingFlags.SAMPLED);
try (Scope scope = currentTraceContext.newScope(parent)) {
get(client, "/foo");
}
RecordedRequest request = takeRequest();
assertThat(request.getHeader("x-b3-traceId")).isEqualTo(request.getHeader("my-id"));
testSpanHandler.takeRemoteSpan(Span.Kind.CLIENT);
}
use of okhttp3.Dispatcher in project java-cloudant by cloudant.
the class HttpTest method singleSessionRequestOnExpiry.
/**
* This test checks that only a single session renewal request is made on expiry.
* Flow:
* - First request to _all_dbs
* - sends a _session request and gets OK_COOKIE
* - _all_dbs returns ["a"]
* - Multi-threaded requests to root endpoint
* - Any that occur before session renewal get a 401 unauthorized and try to renew the session
* - a _session request will return OK_COOKIE_2 but can only be invoked once for test purposes
* - any requests after session renewal will get an OK response
*
* @throws Exception
*/
@TestTemplate
public void singleSessionRequestOnExpiry() throws Exception {
final AtomicInteger sessionCounter = new AtomicInteger();
mockWebServer.setDispatcher(new Dispatcher() {
// Use 444 response for error cases as we know this will get an exception without retries
private final MockResponse FAIL = new MockResponse().setStatus("HTTP/1.1 444 session locking fail");
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().endsWith("_session")) {
int session = sessionCounter.incrementAndGet();
switch(session) {
case 1:
return OK_COOKIE;
case 2:
return OK_COOKIE_2;
default:
return FAIL;
}
} else if (request.getPath().endsWith("_all_dbs")) {
return new MockResponse().setBody("[\"a\"]");
} else {
String cookie = request.getHeader("COOKIE");
if (cookie.contains(EXPECTED_OK_COOKIE)) {
// Request in first session
return new MockResponse().setResponseCode(401);
} else if (cookie.contains(EXPECTED_OK_COOKIE_2)) {
// Request in second session, return OK
return new MockResponse();
} else {
return FAIL;
}
}
}
});
CloudantClient c = CloudantClientHelper.newMockWebServerClientBuilder(mockWebServer).username("a").password("b").build();
// Do a single request to start the first session
c.getAllDbs();
// Now run lots of requests simultaneously
int threads = 25;
int requests = 1250;
ExecutorService executorService = Executors.newFixedThreadPool(threads);
List<ServerInfoCallable> tasks = new ArrayList<ServerInfoCallable>(requests);
for (int i = 0; i < requests; i++) {
tasks.add(new ServerInfoCallable(c));
}
List<Future<Throwable>> results = executorService.invokeAll(tasks);
for (Future<Throwable> result : results) {
assertNull(result.get(), "There should be no exceptions.");
}
assertEquals(2, sessionCounter.get(), "There should only be 2 session requests");
}
Aggregations