Search in sources :

Example 6 with OkHttpResponseWrapper

use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.

the class HttpArtifactCacheTest method testFetchOK.

@Test
public void testFetchOK() throws Exception {
    Path output = Paths.get("output/file");
    final String data = "test";
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    final List<Response> responseList = Lists.newArrayList();
    argsBuilder.setProjectFilesystem(filesystem);
    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), ImmutableMap.of(), ByteSource.wrap(data.getBytes(Charsets.UTF_8)), data)).build();
        responseList.add(response);
        return new OkHttpResponseWrapper(response);
    }));
    HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
    CacheResult result = cache.fetch(ruleKey, LazyPath.ofInstance(output));
    assertEquals(result.cacheError().orElse(""), CacheResultType.HIT, result.getType());
    assertEquals(Optional.of(data), filesystem.readFileIfItExists(output));
    assertEquals(result.artifactSizeBytes(), Optional.of(filesystem.getFileSize(output)));
    assertTrue("response wasn't fully read!", responseList.get(0).body().source().exhausted());
    cache.close();
}
Also used : Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) Response(okhttp3.Response) HttpResponse(com.facebook.buck.slb.HttpResponse) BuckEventBus(com.facebook.buck.event.BuckEventBus) HttpURLConnection(java.net.HttpURLConnection) DataInputStream(java.io.DataInputStream) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BuckEvent(com.facebook.buck.event.BuckEvent) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) BuildId(com.facebook.buck.model.BuildId) RuleKey(com.facebook.buck.rules.RuleKey) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Response(okhttp3.Response) ByteSource(com.google.common.io.ByteSource) HttpResponse(com.facebook.buck.slb.HttpResponse) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) HttpService(com.facebook.buck.slb.HttpService) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) Request(okhttp3.Request) Buffer(okio.Buffer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertTrue(org.junit.Assert.assertTrue) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Matchers(org.hamcrest.Matchers) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) List(java.util.List) Paths(java.nio.file.Paths) LazyPath(com.facebook.buck.io.LazyPath) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) Optional(java.util.Optional) Protocol(okhttp3.Protocol) Assert.assertEquals(org.junit.Assert.assertEquals) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Request(okhttp3.Request) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Test(org.junit.Test)

Example 7 with OkHttpResponseWrapper

use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.

the class HttpArtifactCacheTest method testFetchNotFound.

@Test
public void testFetchNotFound() throws Exception {
    final List<Response> responseList = Lists.newArrayList();
    argsBuilder.setFetchClient(withMakeRequest((path, requestBuilder) -> {
        Response response = new Response.Builder().code(HttpURLConnection.HTTP_NOT_FOUND).body(ResponseBody.create(MediaType.parse("application/octet-stream"), "extraneous")).protocol(Protocol.HTTP_1_1).request(requestBuilder.url(SERVER + path).build()).build();
        responseList.add(response);
        return new OkHttpResponseWrapper(response);
    }));
    HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
    CacheResult result = cache.fetch(new RuleKey("00000000000000000000000000000000"), LazyPath.ofInstance(Paths.get("output/file")));
    assertEquals(result.getType(), CacheResultType.MISS);
    assertTrue("response wasn't fully read!", responseList.get(0).body().source().exhausted());
    cache.close();
}
Also used : Response(okhttp3.Response) HttpResponse(com.facebook.buck.slb.HttpResponse) BuckEventBus(com.facebook.buck.event.BuckEventBus) HttpURLConnection(java.net.HttpURLConnection) DataInputStream(java.io.DataInputStream) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BuckEvent(com.facebook.buck.event.BuckEvent) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) BuildId(com.facebook.buck.model.BuildId) RuleKey(com.facebook.buck.rules.RuleKey) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Response(okhttp3.Response) ByteSource(com.google.common.io.ByteSource) HttpResponse(com.facebook.buck.slb.HttpResponse) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) HttpService(com.facebook.buck.slb.HttpService) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) Request(okhttp3.Request) Buffer(okio.Buffer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertTrue(org.junit.Assert.assertTrue) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Matchers(org.hamcrest.Matchers) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) List(java.util.List) Paths(java.nio.file.Paths) LazyPath(com.facebook.buck.io.LazyPath) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) Optional(java.util.Optional) Protocol(okhttp3.Protocol) Assert.assertEquals(org.junit.Assert.assertEquals) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) RuleKey(com.facebook.buck.rules.RuleKey) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Test(org.junit.Test)

Example 8 with OkHttpResponseWrapper

use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.

the class HttpArtifactCacheTest method testStoreMultipleKeys.

@Test
public void testStoreMultipleKeys() throws Exception {
    final RuleKey ruleKey1 = new RuleKey("00000000000000000000000000000000");
    final RuleKey ruleKey2 = new RuleKey("11111111111111111111111111111111");
    final String data = "data";
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    Path output = Paths.get("output/file");
    filesystem.writeContentsToPath(data, output);
    final Set<RuleKey> stored = Sets.newHashSet();
    argsBuilder.setProjectFilesystem(filesystem);
    argsBuilder.setStoreClient(withMakeRequest(((path, requestBuilder) -> {
        Request request = requestBuilder.url(SERVER).build();
        Buffer buf = new Buffer();
        request.body().writeTo(buf);
        try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(buf.readByteArray()))) {
            int keys = in.readInt();
            for (int i = 0; i < keys; i++) {
                stored.add(new RuleKey(in.readUTF()));
            }
        }
        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(ruleKey1, ruleKey2).build(), output, createFinishedEventBuilder());
    assertThat(stored, Matchers.containsInAnyOrder(ruleKey1, ruleKey2));
    cache.close();
}
Also used : Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) Buffer(okio.Buffer) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Request(okhttp3.Request) DataInputStream(java.io.DataInputStream) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Response(okhttp3.Response) HttpResponse(com.facebook.buck.slb.HttpResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 9 with OkHttpResponseWrapper

use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.

the class HttpArtifactCacheTest method testFetchUrl.

@Test
public void testFetchUrl() throws Exception {
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    final String expectedUri = "/artifacts/key/00000000000000000000000000000000";
    argsBuilder.setFetchClient(withMakeRequest((path, requestBuilder) -> {
        Request request = requestBuilder.url(SERVER + path).build();
        assertEquals(expectedUri, request.url().encodedPath());
        return new OkHttpResponseWrapper(new Response.Builder().request(request).protocol(Protocol.HTTP_1_1).code(HttpURLConnection.HTTP_OK).body(createResponseBody(ImmutableSet.of(ruleKey), ImmutableMap.of(), ByteSource.wrap(new byte[0]), "data")).build());
    }));
    HttpArtifactCache cache = new HttpArtifactCache(argsBuilder.build());
    cache.fetch(ruleKey, LazyPath.ofInstance(Paths.get("output/file")));
    cache.close();
}
Also used : BuckEventBus(com.facebook.buck.event.BuckEventBus) HttpURLConnection(java.net.HttpURLConnection) DataInputStream(java.io.DataInputStream) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BuckEvent(com.facebook.buck.event.BuckEvent) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) BuildId(com.facebook.buck.model.BuildId) RuleKey(com.facebook.buck.rules.RuleKey) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Response(okhttp3.Response) ByteSource(com.google.common.io.ByteSource) HttpResponse(com.facebook.buck.slb.HttpResponse) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) HttpService(com.facebook.buck.slb.HttpService) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) Request(okhttp3.Request) Buffer(okio.Buffer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertTrue(org.junit.Assert.assertTrue) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Matchers(org.hamcrest.Matchers) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) List(java.util.List) Paths(java.nio.file.Paths) LazyPath(com.facebook.buck.io.LazyPath) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) Optional(java.util.Optional) Protocol(okhttp3.Protocol) Assert.assertEquals(org.junit.Assert.assertEquals) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Response(okhttp3.Response) HttpResponse(com.facebook.buck.slb.HttpResponse) RuleKey(com.facebook.buck.rules.RuleKey) Request(okhttp3.Request) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Test(org.junit.Test)

Example 10 with OkHttpResponseWrapper

use of com.facebook.buck.slb.OkHttpResponseWrapper in project buck by facebook.

the class HttpArtifactCacheTest method testFetchExtraPayload.

@Test
public void testFetchExtraPayload() throws Exception {
    FakeProjectFilesystem filesystem = new FakeProjectFilesystem();
    final RuleKey ruleKey = new RuleKey("00000000000000000000000000000000");
    final List<Response> responseList = Lists.newArrayList();
    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), ImmutableMap.of(), ByteSource.wrap("more data than length".getBytes(Charsets.UTF_8)), "small")).build();
        responseList.add(response);
        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("response wasn't fully read!", responseList.get(0).body().source().exhausted());
    cache.close();
}
Also used : Response(okhttp3.Response) HttpResponse(com.facebook.buck.slb.HttpResponse) BuckEventBus(com.facebook.buck.event.BuckEventBus) HttpURLConnection(java.net.HttpURLConnection) DataInputStream(java.io.DataInputStream) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BuckEvent(com.facebook.buck.event.BuckEvent) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ConsoleEvent(com.facebook.buck.event.ConsoleEvent) Assert.assertThat(org.junit.Assert.assertThat) Lists(com.google.common.collect.Lists) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) BuildId(com.facebook.buck.model.BuildId) RuleKey(com.facebook.buck.rules.RuleKey) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Response(okhttp3.Response) ByteSource(com.google.common.io.ByteSource) HttpResponse(com.facebook.buck.slb.HttpResponse) Path(java.nio.file.Path) MediaType(okhttp3.MediaType) ResponseBody(okhttp3.ResponseBody) HttpService(com.facebook.buck.slb.HttpService) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) Request(okhttp3.Request) Buffer(okio.Buffer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertTrue(org.junit.Assert.assertTrue) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Matchers(org.hamcrest.Matchers) Set(java.util.Set) Test(org.junit.Test) IOException(java.io.IOException) Sets(com.google.common.collect.Sets) List(java.util.List) Paths(java.nio.file.Paths) LazyPath(com.facebook.buck.io.LazyPath) IncrementingFakeClock(com.facebook.buck.timing.IncrementingFakeClock) Optional(java.util.Optional) Protocol(okhttp3.Protocol) Assert.assertEquals(org.junit.Assert.assertEquals) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) Path(java.nio.file.Path) LazyPath(com.facebook.buck.io.LazyPath) RuleKey(com.facebook.buck.rules.RuleKey) FakeProjectFilesystem(com.facebook.buck.testutil.FakeProjectFilesystem) Request(okhttp3.Request) OkHttpResponseWrapper(com.facebook.buck.slb.OkHttpResponseWrapper) Test(org.junit.Test)

Aggregations

LazyPath (com.facebook.buck.io.LazyPath)10 RuleKey (com.facebook.buck.rules.RuleKey)10 HttpResponse (com.facebook.buck.slb.HttpResponse)10 OkHttpResponseWrapper (com.facebook.buck.slb.OkHttpResponseWrapper)10 Path (java.nio.file.Path)10 Request (okhttp3.Request)10 Response (okhttp3.Response)10 Test (org.junit.Test)10 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)9 Buffer (okio.Buffer)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 DataInputStream (java.io.DataInputStream)7 DataOutputStream (java.io.DataOutputStream)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 BuckEvent (com.facebook.buck.event.BuckEvent)6 BuckEventBus (com.facebook.buck.event.BuckEventBus)6 ConsoleEvent (com.facebook.buck.event.ConsoleEvent)6 BuildId (com.facebook.buck.model.BuildId)6 HttpService (com.facebook.buck.slb.HttpService)6