use of com.yahoo.vespa.clustercontroller.core.restapiv2.Response in project buck by facebook.
the class HttpArtifactCacheTest method testFetchMetadata.
@Test
public void testFetchMetadata() throws Exception {
Path output = Paths.get("output/file");
final String data = "test";
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final ImmutableMap<String, String> metadata = ImmutableMap.of("some", "metadata");
argsBuilder.setFetchClient(withMakeRequest(((path, requestBuilder) -> {
Request request = requestBuilder.url(SERVER + path).build();
Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(ruleKey), metadata, ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
return new OkHttpResponseWrapper(response);
})));
HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
CacheResult result = cache.fetch(ruleKey, LazyPath.ofInstance(output));
assertEquals(CacheResultType.HIT, result.getType());
assertEquals(metadata, result.getMetadata());
cache.close();
}
use of com.yahoo.vespa.clustercontroller.core.restapiv2.Response in project buck by facebook.
the class HttpArtifactCacheTest method errorTextReplaced.
@Test
public void errorTextReplaced() throws InterruptedException {
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
final String cacheName = "http cache";
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final RuleKey otherRuleKey = new RuleKey("11111111111111111111111111111111");
final String data = "data";
final AtomicBoolean consoleEventReceived = new AtomicBoolean(false);
argsBuilder.setCacheName(cacheName).setProjectFilesystem(filesystem).setBuckEventBus(new BuckEventBus(new IncrementingFakeClock(), new BuildId()) {
@Override
public void post(BuckEvent event) {
if (event instanceof ConsoleEvent) {
consoleEventReceived.set(true);
ConsoleEvent consoleEvent = (ConsoleEvent) event;
assertThat(consoleEvent.getMessage(), Matchers.containsString(cacheName));
assertThat(consoleEvent.getMessage(), Matchers.containsString("incorrect key name"));
}
}
}).setFetchClient(withMakeRequest((path, requestBuilder) -> {
Request request = requestBuilder.url(SERVER + path).build();
Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(otherRuleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
return new OkHttpResponseWrapper(response);
}));
HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
Path output = Paths.get("output/file");
CacheResult result = cache.fetch(ruleKey, LazyPath.ofInstance(output));
assertEquals(CacheResultType.ERROR, result.getType());
assertEquals(Optional.empty(), filesystem.readFileIfItExists(output));
assertTrue(consoleEventReceived.get());
cache.close();
}
use of com.yahoo.vespa.clustercontroller.core.restapiv2.Response in project buck by facebook.
the class HttpArtifactCacheTest method testStore.
@Test
public void testStore() throws Exception {
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final String data = "data";
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
Path output = Paths.get("output/file");
filesystem.writeContentsToPath(data, output);
final AtomicBoolean hasCalled = new AtomicBoolean(false);
argsBuilder.setProjectFilesystem(filesystem);
argsBuilder.setStoreClient(withMakeRequest(((path, requestBuilder) -> {
Request request = requestBuilder.url(SERVER).build();
hasCalled.set(true);
Buffer buf = new Buffer();
request.body().writeTo(buf);
byte[] actualData = buf.readByteArray();
byte[] expectedData;
try (ByteArrayOutputStream out = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(out)) {
dataOut.write(HttpArtifactCacheBinaryProtocol.createKeysHeader(ImmutableSet.of(ruleKey)));
byte[] metadata = HttpArtifactCacheBinaryProtocol.createMetadataHeader(ImmutableSet.of(ruleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)));
dataOut.writeInt(metadata.length);
dataOut.write(metadata);
dataOut.write(data.getBytes(Charsets.UTF_8));
expectedData = out.toByteArray();
}
assertArrayEquals(expectedData, actualData);
Response response = new Response.Builder().body(createDummyBody()).code(HttpURLConnection.HTTP_ACCEPTED).protocol(Protocol.HTTP_1_1).request(request).build();
return new OkHttpResponseWrapper(response);
})));
HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
cache.storeImpl(ArtifactInfo.builder().addRuleKeys(ruleKey).build(), output, createFinishedEventBuilder());
assertTrue(hasCalled.get());
cache.close();
}
use of com.yahoo.vespa.clustercontroller.core.restapiv2.Response in project buck by facebook.
the class HttpArtifactCacheTest method testFetchWrongKey.
@Test
public void testFetchWrongKey() throws Exception {
FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
final RuleKey otherRuleKey = new RuleKey("11111111111111111111111111111111");
final String data = "data";
argsBuilder.setFetchClient(withMakeRequest(((path, requestBuilder) -> {
Request request = requestBuilder.url(SERVER + path).build();
Response response = new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(otherRuleKey), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
return new OkHttpResponseWrapper(response);
})));
HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
Path output = Paths.get("output/file");
CacheResult result = cache.fetch(ruleKey, LazyPath.ofInstance(output));
assertEquals(CacheResultType.ERROR, result.getType());
assertEquals(Optional.empty(), filesystem.readFileIfItExists(output));
cache.close();
}
use of com.yahoo.vespa.clustercontroller.core.restapiv2.Response in project GeekNews by codeestX.
the class HttpModule method provideClient.
@Singleton
@Provides
OkHttpClient provideClient(OkHttpClient.Builder builder) {
if (BuildConfig.DEBUG) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
builder.addInterceptor(loggingInterceptor);
}
File cacheFile = new File(Constants.PATH_CACHE);
Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
Interceptor cacheInterceptor = new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
if (!SystemUtil.isNetworkConnected()) {
request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
}
Response response = chain.proceed(request);
if (SystemUtil.isNetworkConnected()) {
int maxAge = 0;
// 有网络时, 不缓存, 最大保存时长为0
response.newBuilder().header("Cache-Control", "public, max-age=" + maxAge).removeHeader("Pragma").build();
} else {
// 无网络时,设置超时为4周
int maxStale = 60 * 60 * 24 * 28;
response.newBuilder().header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale).removeHeader("Pragma").build();
}
return response;
}
};
// Interceptor apikey = new Interceptor() {
// @Override
// public Response intercept(Chain chain) throws IOException {
// Request request = chain.request();
// request = request.newBuilder()
// .addHeader("apikey",Constants.KEY_API)
// .build();
// return chain.proceed(request);
// }
// }
// 设置统一的请求头部参数
// builder.addInterceptor(apikey);
//设置缓存
builder.addNetworkInterceptor(cacheInterceptor);
builder.addInterceptor(cacheInterceptor);
builder.cache(cache);
//设置超时
builder.connectTimeout(10, TimeUnit.SECONDS);
builder.readTimeout(20, TimeUnit.SECONDS);
builder.writeTimeout(20, TimeUnit.SECONDS);
//错误重连
builder.retryOnConnectionFailure(true);
return builder.build();
}
Aggregations