Search in sources :

Example 11 with Dispatcher

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

the class ApolloPrefetchTest method setup.

@Before
public void setup() {
    cacheStore = new MockHttpCacheStore();
    cacheStore.delegate = new DiskLruHttpCacheStore(inMemoryFileSystem, new File("/cache/"), Integer.MAX_VALUE);
    okHttpClient = new OkHttpClient.Builder().addInterceptor(new Interceptor() {

        @Override
        public okhttp3.Response intercept(Chain chain) throws IOException {
            lastHttRequest = chain.request();
            lastHttResponse = chain.proceed(lastHttRequest);
            return lastHttResponse;
        }
    }).dispatcher(new Dispatcher(Utils.immediateExecutorService())).build();
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).dispatcher(Utils.immediateExecutor()).httpCache(new ApolloHttpCache(cacheStore, null)).build();
}
Also used : ApolloHttpCache(com.apollographql.apollo.cache.http.ApolloHttpCache) DiskLruHttpCacheStore(com.apollographql.apollo.cache.http.DiskLruHttpCacheStore) Dispatcher(okhttp3.Dispatcher) File(java.io.File) ApolloServerInterceptor(com.apollographql.apollo.internal.interceptor.ApolloServerInterceptor) Interceptor(okhttp3.Interceptor) Before(org.junit.Before)

Example 12 with Dispatcher

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

the class ApolloWatcherTest method setUp.

@Before
public void setUp() throws IOException {
    OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build();
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).dispatcher(immediateExecutor()).okHttpClient(okHttpClient).logger(new Logger() {

        @Override
        public void log(int priority, @Nonnull String message, @Nonnull Optional<Throwable> t, @Nonnull Object... args) {
            String throwableTrace = "";
            if (t.isPresent()) {
                throwableTrace = t.get().getMessage();
            }
            System.out.println(String.format(message, args) + " " + throwableTrace);
        }
    }).normalizedCache(new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION), new IdFieldCacheKeyResolver()).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Optional(com.apollographql.apollo.api.internal.Optional) LruNormalizedCacheFactory(com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory) Nonnull(javax.annotation.Nonnull) Dispatcher(okhttp3.Dispatcher) Before(org.junit.Before)

Example 13 with Dispatcher

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

the class CacheHeadersTest method testHeadersReceived.

@Test
@SuppressWarnings("CheckReturnValue")
public void testHeadersReceived() throws ApolloException, IOException {
    final AtomicBoolean hasHeader = new AtomicBoolean();
    final NormalizedCache normalizedCache = new NormalizedCache() {

        @Nullable
        @Override
        public Record loadRecord(@NonNull String key, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return null;
        }

        @Nonnull
        @Override
        public Set<String> merge(@NonNull Record record, @NonNull CacheHeaders cacheHeaders) {
            hasHeader.set(cacheHeaders.hasHeader(ApolloCacheHeaders.DO_NOT_STORE));
            return emptySet();
        }

        @Override
        public void clearAll() {
        }

        @Override
        public boolean remove(@Nonnull CacheKey cacheKey) {
            return false;
        }

        @Nonnull
        @Override
        protected Set<String> performMerge(@Nonnull Record apolloRecord, @Nonnull CacheHeaders cacheHeaders) {
            return emptySet();
        }
    };
    final NormalizedCacheFactory<NormalizedCache> cacheFactory = new NormalizedCacheFactory<NormalizedCache>() {

        @Override
        public NormalizedCache create(RecordFieldJsonAdapter recordFieldAdapter) {
            return normalizedCache;
        }
    };
    ApolloClient apolloClient = ApolloClient.builder().normalizedCache(cacheFactory, new IdFieldCacheKeyResolver()).serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).dispatcher(Utils.immediateExecutor()).build();
    server.enqueue(mockResponse("HeroAndFriendsNameResponse.json"));
    CacheHeaders cacheHeaders = CacheHeaders.builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build();
    Rx2Apollo.from(apolloClient.query(new HeroAndFriendsNamesQuery(Input.fromNullable(Episode.NEWHOPE))).cacheHeaders(cacheHeaders)).test();
    assertThat(hasHeader.get()).isTrue();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) Nonnull(javax.annotation.Nonnull) Dispatcher(okhttp3.Dispatcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RecordFieldJsonAdapter(com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter) ApolloCacheHeaders(com.apollographql.apollo.cache.ApolloCacheHeaders) CacheHeaders(com.apollographql.apollo.cache.CacheHeaders) HeroAndFriendsNamesQuery(com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery) NormalizedCache(com.apollographql.apollo.cache.normalized.NormalizedCache) NonNull(android.support.annotation.NonNull) NormalizedCacheFactory(com.apollographql.apollo.cache.normalized.NormalizedCacheFactory) Record(com.apollographql.apollo.cache.normalized.Record) CacheKey(com.apollographql.apollo.cache.normalized.CacheKey) Test(org.junit.Test)

Example 14 with Dispatcher

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

the class HttpCacheTest method noCacheStore.

@Test
public void noCacheStore() throws Exception {
    enqueueResponse("/HttpCacheTestAllPlanets.json");
    ApolloClient apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().addInterceptor(new TrackingInterceptor()).dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).dispatcher(Utils.immediateExecutor()).build();
    Rx2Apollo.from(apolloClient.query(new AllPlanetsQuery())).test().assertValue(new Predicate<Response<AllPlanetsQuery.Data>>() {

        @Override
        public boolean test(Response<AllPlanetsQuery.Data> response) throws Exception {
            return !response.hasErrors();
        }
    });
    checkNoCachedResponse();
}
Also used : Response(com.apollographql.apollo.api.Response) MockResponse(okhttp3.mockwebserver.MockResponse) AllPlanetsQuery(com.apollographql.apollo.integration.httpcache.AllPlanetsQuery) Dispatcher(okhttp3.Dispatcher) ApolloHttpException(com.apollographql.apollo.exception.ApolloHttpException) ParseException(java.text.ParseException) IOException(java.io.IOException) ApolloException(com.apollographql.apollo.exception.ApolloException) Test(org.junit.Test)

Example 15 with Dispatcher

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

the class IntegrationTest method setUp.

@Before
public void setUp() {
    dateCustomTypeAdapter = new CustomTypeAdapter<Date>() {

        @Override
        public Date decode(CustomTypeValue value) {
            try {
                return DATE_FORMAT.parse(value.value.toString());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public CustomTypeValue encode(Date value) {
            return new CustomTypeValue.GraphQLString(DATE_FORMAT.format(value));
        }
    };
    apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter).normalizedCache(new LruNormalizedCacheFactory(EvictionPolicy.NO_EVICTION), new IdFieldCacheKeyResolver()).defaultResponseFetcher(ApolloResponseFetchers.NETWORK_ONLY).dispatcher(Utils.immediateExecutor()).build();
}
Also used : OkHttpClient(okhttp3.OkHttpClient) LruNormalizedCacheFactory(com.apollographql.apollo.cache.normalized.lru.LruNormalizedCacheFactory) CustomTypeValue(com.apollographql.apollo.response.CustomTypeValue) ParseException(java.text.ParseException) Dispatcher(okhttp3.Dispatcher) Date(java.util.Date) Before(org.junit.Before)

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