use of mockwebserver3.Dispatcher in project autorest-clientruntime-for-java by Azure.
the class ConnectionPoolTests method canUseRxThreadPool.
// Simulates a server with response latency of 1 second. A connection pool
// size 2 should only send requests on Rx scheduler.
@Test
public void canUseRxThreadPool() throws Exception {
RestClient restClient = new RestClient.Builder().withBaseUrl("https://microsoft.com").withSerializerAdapter(new JacksonAdapter()).withResponseBuilderFactory(new Factory()).withDispatcher(new Dispatcher(Executors.newFixedThreadPool(2))).useHttpClientThreadPool(false).withInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new IOException(e);
}
return new Response.Builder().request(chain.request()).code(200).message("OK").protocol(Protocol.HTTP_1_1).body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks")).build();
}
}).build();
final Service service = restClient.retrofit().create(Service.class);
final CountDownLatch latch = new CountDownLatch(1);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// Rx Scheduler with no concurrency control
Observable.range(1, 6).flatMap(new Func1<Integer, Observable<?>>() {
@Override
public Observable<?> call(Integer integer) {
return service.getAsync().subscribeOn(Schedulers.io());
}
}).doOnCompleted(new Action0() {
@Override
public void call() {
latch.countDown();
}
}).subscribe();
latch.await();
stopWatch.stop();
Assert.assertTrue(stopWatch.getTime() < 2000);
final CountDownLatch latch2 = new CountDownLatch(1);
stopWatch.reset();
stopWatch.start();
// Rx Scheduler with concurrency control
Observable.range(1, 4).flatMap(new Func1<Integer, Observable<?>>() {
@Override
public Observable<?> call(Integer integer) {
return service.getAsync().subscribeOn(Schedulers.io());
}
}, 2).doOnCompleted(new Action0() {
@Override
public void call() {
latch2.countDown();
}
}).subscribe();
latch2.await();
stopWatch.stop();
Assert.assertTrue(stopWatch.getTime() > 2000);
}
use of mockwebserver3.Dispatcher in project retrofit by square.
the class Crawler method main.
public static void main(String... args) throws Exception {
Dispatcher dispatcher = new Dispatcher(Executors.newFixedThreadPool(20));
dispatcher.setMaxRequests(20);
dispatcher.setMaxRequestsPerHost(1);
OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(dispatcher).connectionPool(new ConnectionPool(100, 30, TimeUnit.SECONDS)).build();
Retrofit retrofit = new Retrofit.Builder().baseUrl(HttpUrl.parse("https://example.com/")).addConverterFactory(PageAdapter.FACTORY).client(okHttpClient).build();
PageService pageService = retrofit.create(PageService.class);
Crawler crawler = new Crawler(pageService);
crawler.crawlPage(HttpUrl.parse(args[0]));
}
use of mockwebserver3.Dispatcher in project apollo-android by apollographql.
the class CacheHeadersTest method testDefaultHeadersReceived.
@Test
@SuppressWarnings("CheckReturnValue")
public void testDefaultHeadersReceived() throws Exception {
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;
}
};
CacheHeaders cacheHeaders = CacheHeaders.builder().addHeader(ApolloCacheHeaders.DO_NOT_STORE, "true").build();
ApolloClient apolloClient = ApolloClient.builder().normalizedCache(cacheFactory, new IdFieldCacheKeyResolver()).serverUrl(server.url("/")).okHttpClient(new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build()).dispatcher(Utils.immediateExecutor()).defaultCacheHeaders(cacheHeaders).build();
server.enqueue(mockResponse("HeroAndFriendsNameResponse.json"));
Rx2Apollo.from(apolloClient.query(new HeroAndFriendsNamesQuery(Input.fromNullable(Episode.NEWHOPE))).cacheHeaders(cacheHeaders)).test();
assertThat(hasHeader.get()).isTrue();
}
use of mockwebserver3.Dispatcher in project apollo-android by apollographql.
the class HttpCacheTest method setUp.
@Before
public void setUp() {
CustomTypeAdapter<Date> 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));
}
};
cacheStore = new MockHttpCacheStore();
cacheStore.delegate = new DiskLruHttpCacheStore(inMemoryFileSystem, new File("/cache/"), Integer.MAX_VALUE);
HttpCache cache = new ApolloHttpCache(cacheStore, null);
okHttpClient = new OkHttpClient.Builder().addInterceptor(new TrackingInterceptor()).addInterceptor(cache.interceptor()).dispatcher(new Dispatcher(Utils.immediateExecutorService())).readTimeout(2, TimeUnit.SECONDS).writeTimeout(2, TimeUnit.SECONDS).build();
apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).dispatcher(Utils.immediateExecutor()).addCustomTypeAdapter(CustomType.DATE, dateCustomTypeAdapter).httpCache(cache).build();
}
use of mockwebserver3.Dispatcher in project apollo-android by apollographql.
the class ApolloCancelCallTest method cancelCallAfterEnqueueNoCallback.
@Test
public void cancelCallAfterEnqueueNoCallback() throws Exception {
OkHttpClient okHttpClient = new OkHttpClient.Builder().dispatcher(new Dispatcher(Utils.immediateExecutorService())).build();
apolloClient = ApolloClient.builder().serverUrl(server.url("/")).okHttpClient(okHttpClient).httpCache(new ApolloHttpCache(cacheStore, null)).build();
server.enqueue(mockResponse("EpisodeHeroNameResponse.json"));
final ApolloCall<EpisodeHeroNameQuery.Data> call = apolloClient.query(new EpisodeHeroNameQuery(Input.fromNullable(Episode.EMPIRE)));
final TestObserver<Response<EpisodeHeroNameQuery.Data>> test = Rx2Apollo.from(call).test();
call.cancel();
test.awaitDone(1, TimeUnit.SECONDS).assertNoErrors().assertNoValues().assertNotComplete();
}
Aggregations