Search in sources :

Example 21 with Dispatcher

use of mockwebserver3.Dispatcher in project apollo-android by apollographql.

the class BaseFetcherTest method setUp.

@Before
public void setUp() {
    server = new MockWebServer();
    OkHttpClient okHttpClient = new OkHttpClient.Builder().writeTimeout(2, TimeUnit.SECONDS).readTimeout(2, TimeUnit.SECONDS).dispatcher(new Dispatcher(Utils.immediateExecutorService())).build();
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).normalizedCache(new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION), new IdFieldCacheKeyResolver()).dispatcher(Utils.immediateExecutor()).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) LruNormalizedCacheFactory(com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory) MockWebServer(okhttp3.mockwebserver.MockWebServer) Dispatcher(okhttp3.Dispatcher) IdFieldCacheKeyResolver(com.apollographql.apollo.IdFieldCacheKeyResolver) Before(org.junit.Before)

Example 22 with Dispatcher

use of mockwebserver3.Dispatcher in project okhttp by square.

the class InterceptorTest method applicationInterceptorReturnsNull.

@Test
public void applicationInterceptorReturnsNull() throws Exception {
    server.enqueue(new MockResponse());
    Interceptor interceptor = chain -> {
        chain.proceed(chain.request());
        return null;
    };
    client = client.newBuilder().addInterceptor(interceptor).build();
    ExceptionCatchingExecutor executor = new ExceptionCatchingExecutor();
    client = client.newBuilder().dispatcher(new Dispatcher(executor)).build();
    Request request = new Request.Builder().url(server.url("/")).build();
    try {
        client.newCall(request).execute();
        fail();
    } catch (NullPointerException expected) {
        assertThat(expected.getMessage()).isEqualTo(("interceptor " + interceptor + " returned null"));
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Okio(okio.Okio) BeforeEach(org.junit.jupiter.api.BeforeEach) Socket(java.net.Socket) Source(okio.Source) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) AtomicReference(java.util.concurrent.atomic.AtomicReference) Sink(okio.Sink) MockWebServer(mockwebserver3.MockWebServer) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) SocketPolicy(mockwebserver3.SocketPolicy) BufferedSink(okio.BufferedSink) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Locale(java.util.Locale) SocketTimeoutException(java.net.SocketTimeoutException) Duration(java.time.Duration) ForwardingSource(okio.ForwardingSource) Tag(org.junit.jupiter.api.Tag) Buffer(okio.Buffer) SynchronousQueue(java.util.concurrent.SynchronousQueue) IOException(java.io.IOException) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Unit(kotlin.Unit) GzipSink(okio.GzipSink) ForwardingSink(okio.ForwardingSink) MockResponse(mockwebserver3.MockResponse) TestUtil.assertSuppressed(okhttp3.TestUtil.assertSuppressed) RecordedRequest(mockwebserver3.RecordedRequest) MockResponse(mockwebserver3.MockResponse) RecordedRequest(mockwebserver3.RecordedRequest) Test(org.junit.jupiter.api.Test)

Example 23 with Dispatcher

use of mockwebserver3.Dispatcher in project okhttp by square.

the class WebSocketHttpTest method readTimeoutAppliesWithinFrames.

/**
 * There's no read timeout when reading the first byte of a new frame. But as soon as we start
 * reading a frame we enable the read timeout. In this test we have the server returning the first
 * byte of a frame but no more frames.
 */
@Test
public void readTimeoutAppliesWithinFrames() {
    webServer.setDispatcher(new Dispatcher() {

        @Override
        public MockResponse dispatch(RecordedRequest request) {
            return upgradeResponse(request).setBody(// Truncated frame.
            new Buffer().write(ByteString.decodeHex("81"))).removeHeader("Content-Length").setSocketPolicy(SocketPolicy.KEEP_OPEN);
        }
    });
    WebSocket webSocket = newWebSocket();
    clientListener.assertOpen();
    clientListener.assertFailure(SocketTimeoutException.class, "timeout", "Read timed out");
    assertThat(webSocket.close(1000, null)).isFalse();
}
Also used : RecordedRequest(mockwebserver3.RecordedRequest) Buffer(okio.Buffer) MockResponse(mockwebserver3.MockResponse) Dispatcher(mockwebserver3.Dispatcher) WebSocket(okhttp3.WebSocket) Test(org.junit.jupiter.api.Test)

Example 24 with Dispatcher

use of mockwebserver3.Dispatcher in project MVPArms by JessYanCoding.

the class ClientModule method provideClient.

/**
 * 提供 {@link OkHttpClient}
 *
 * @param application     {@link Application}
 * @param configuration   {@link OkhttpConfiguration}
 * @param builder         {@link OkHttpClient.Builder}
 * @param intercept       {@link Interceptor}
 * @param interceptors    {@link List<Interceptor>}
 * @param handler         {@link GlobalHttpHandler}
 * @param executorService {@link ExecutorService}
 * @return {@link OkHttpClient}
 */
@Singleton
@Provides
static OkHttpClient provideClient(Application application, @Nullable OkhttpConfiguration configuration, OkHttpClient.Builder builder, Interceptor intercept, @Nullable List<Interceptor> interceptors, @Nullable GlobalHttpHandler handler, ExecutorService executorService) {
    builder.connectTimeout(TIME_OUT, TimeUnit.SECONDS).readTimeout(TIME_OUT, TimeUnit.SECONDS).addNetworkInterceptor(intercept);
    if (handler != null) {
        builder.addInterceptor(chain -> chain.proceed(handler.onHttpRequestBefore(chain, chain.request())));
    }
    // 如果外部提供了 Interceptor 的集合则遍历添加
    if (interceptors != null) {
        for (Interceptor interceptor : interceptors) {
            builder.addInterceptor(interceptor);
        }
    }
    // 为 OkHttp 设置默认的线程池
    builder.dispatcher(new Dispatcher(executorService));
    if (configuration != null) {
        configuration.configOkhttp(application, builder);
    }
    return builder.build();
}
Also used : Dispatcher(okhttp3.Dispatcher) RequestInterceptor(com.jess.arms.http.log.RequestInterceptor) Interceptor(okhttp3.Interceptor) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 25 with Dispatcher

use of mockwebserver3.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)

Aggregations

Dispatcher (okhttp3.Dispatcher)43 OkHttpClient (okhttp3.OkHttpClient)28 Before (org.junit.Before)14 LruNormalizedCacheFactory (com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory)10 IOException (java.io.IOException)9 Interceptor (okhttp3.Interceptor)7 Test (org.junit.Test)7 Call (okhttp3.Call)6 RecordedRequest (mockwebserver3.RecordedRequest)5 ApolloHttpCache (com.apollographql.apollo.cache.http.ApolloHttpCache)3 CustomTypeValue (com.apollographql.apollo.response.CustomTypeValue)3 ParseException (java.text.ParseException)3 MockResponse (mockwebserver3.MockResponse)3 ConnectionPool (okhttp3.ConnectionPool)3 Request (okhttp3.Request)3 Response (okhttp3.Response)3 Buffer (okio.Buffer)3 NonNull (android.support.annotation.NonNull)2 IdFieldCacheKeyResolver (com.apollographql.apollo.IdFieldCacheKeyResolver)2 Response (com.apollographql.apollo.api.Response)2