Search in sources :

Example 46 with Dispatcher

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);
    }
}
Also used : LongRunningProcessStub(com.google.refine.process.LongRunningProcessStub) EngineDependentOperation(com.google.refine.operations.EngineDependentOperation) MockWebServer(okhttp3.mockwebserver.MockWebServer) DataExtensionConfig(com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig) RefineTest(com.google.refine.RefineTest) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 47 with Dispatcher

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;
}
Also used : Builder(okhttp3.OkHttpClient.Builder) OkHttpClient(okhttp3.OkHttpClient) Builder(okhttp3.OkHttpClient.Builder) Dispatcher(okhttp3.Dispatcher)

Example 48 with Dispatcher

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();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) MockWebServer(mockwebserver3.MockWebServer) Dispatcher(mockwebserver3.Dispatcher)

Example 49 with Dispatcher

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);
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) OkHttpClient(okhttp3.OkHttpClient) Scope(brave.propagation.CurrentTraceContext.Scope) TraceContext(brave.propagation.TraceContext) Test(org.junit.Test)

Example 50 with Dispatcher

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");
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ArrayList(java.util.ArrayList) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Dispatcher(okhttp3.mockwebserver.Dispatcher) CloudantClient(com.cloudant.client.api.CloudantClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) TestTemplate(org.junit.jupiter.api.TestTemplate)

Aggregations

Dispatcher (okhttp3.Dispatcher)40 OkHttpClient (okhttp3.OkHttpClient)34 MockResponse (okhttp3.mockwebserver.MockResponse)27 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)25 Dispatcher (okhttp3.mockwebserver.Dispatcher)24 IOException (java.io.IOException)17 Before (org.junit.Before)14 Test (org.junit.Test)13 Call (okhttp3.Call)11 MockWebServer (okhttp3.mockwebserver.MockWebServer)11 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)10 ArrayList (java.util.ArrayList)8 Request (okhttp3.Request)8 Response (okhttp3.Response)8 ResponseBody (okhttp3.ResponseBody)7 Gson (com.google.gson.Gson)5 Interceptor (okhttp3.Interceptor)5 CloudantClient (com.cloudant.client.api.CloudantClient)4 RefineTest (com.google.refine.RefineTest)4 DataExtensionConfig (com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig)4