Search in sources :

Example 56 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestRestLiSymbolTableProvider method testGetRemoteSymbolTableFetchSuccess.

@Test
public void testGetRemoteSymbolTableFetchSuccess() throws IOException {
    RestResponseBuilder builder = new RestResponseBuilder();
    builder.setStatus(200);
    SymbolTable symbolTable = new InMemorySymbolTable("https://OtherHost:100/service|Test--332004310", Collections.unmodifiableList(Arrays.asList("Haha", "Hehe")));
    builder.setEntity(SymbolTableSerializer.toByteString(ContentType.PROTOBUF2.getCodec(), symbolTable));
    builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, ContentType.PROTOBUF2.getHeaderKey());
    when(_client.restRequest(eq(new RestRequestBuilder(URI.create("https://OtherHost:100/service/symbolTable/Test--332004310")).setHeaders(Collections.singletonMap(RestConstants.HEADER_FETCH_SYMBOL_TABLE, Boolean.TRUE.toString())).build()))).thenReturn(CompletableFuture.completedFuture(builder.build()));
    SymbolTable remoteSymbolTable = _provider.getSymbolTable("https://OtherHost:100/service|Test--332004310");
    Assert.assertNotNull(remoteSymbolTable);
    Assert.assertEquals("https://Host:100/service|Test--332004310", remoteSymbolTable.getName());
    Assert.assertEquals(2, remoteSymbolTable.size());
    // Subsequent fetch should not trigger network fetch and get the table from the cache.
    when(_client.restRequest(any(RestRequest.class))).thenThrow(new IllegalStateException());
    SymbolTable cachedSymbolTable = _provider.getSymbolTable("https://OtherHost:100/service|Test--332004310");
    Assert.assertSame(remoteSymbolTable, cachedSymbolTable);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) Test(org.testng.annotations.Test)

Example 57 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestRestLiSymbolTableProvider method testGetRemoteRequestSymbolTableFetchSuccess.

@Test
public void testGetRemoteRequestSymbolTableFetchSuccess() throws IOException {
    RestResponseBuilder builder = new RestResponseBuilder();
    builder.setStatus(200);
    SymbolTable symbolTable = new InMemorySymbolTable("https://OtherHost:100/service|Test--332004310", Collections.unmodifiableList(Arrays.asList("Haha", "Hehe")));
    builder.setEntity(SymbolTableSerializer.toByteString(ContentType.PROTOBUF2.getCodec(), symbolTable));
    builder.setHeader(RestConstants.HEADER_CONTENT_TYPE, ContentType.PROTOBUF2.getHeaderKey());
    when(_client.restRequest(eq(new RestRequestBuilder(URI.create("d2://someservice/symbolTable")).setHeaders(Collections.singletonMap(RestConstants.HEADER_FETCH_SYMBOL_TABLE, Boolean.TRUE.toString())).build()))).thenReturn(CompletableFuture.completedFuture(builder.build()));
    SymbolTable remoteSymbolTable = _provider.getRequestSymbolTable(URI.create("d2://someservice/path"));
    Assert.assertNotNull(remoteSymbolTable);
    Assert.assertEquals("https://Host:100/service|Test--332004310", remoteSymbolTable.getName());
    Assert.assertEquals(2, remoteSymbolTable.size());
    // Subsequent fetch should not trigger network fetch and get the table from the cache, regardless of
    // whether the table is fetched by request URI or symbol table name.
    when(_client.restRequest(any(RestRequest.class))).thenThrow(new IllegalStateException());
    SymbolTable cachedSymbolTable = _provider.getRequestSymbolTable(URI.create("d2://someservice/path"));
    Assert.assertSame(remoteSymbolTable, cachedSymbolTable);
    cachedSymbolTable = _provider.getSymbolTable("https://OtherHost:100/service|Test--332004310");
    Assert.assertSame(remoteSymbolTable, cachedSymbolTable);
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) SymbolTable(com.linkedin.data.codec.symbol.SymbolTable) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) InMemorySymbolTable(com.linkedin.data.codec.symbol.InMemorySymbolTable) Test(org.testng.annotations.Test)

Example 58 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestDarkClusterVerifierManager method testVerifierEnabled.

@Test
public void testVerifierEnabled() throws InterruptedException {
    setup(true);
    RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("foo")).build();
    RestResponse res = new RestResponseBuilder().build();
    _verifierManager.onDarkResponse(dummyRestRequest, res, DARK_CLUSTER1_NAME);
    _verifierManager.onDarkResponse(dummyRestRequest, res, DARK_CLUSTER1_NAME);
    _verifierManager.onResponse(dummyRestRequest, res);
    waitForLatch();
    Assert.assertEquals(_verifier.onResponseCount, 1, "expected on response count of 1");
    Assert.assertEquals(_verifier.onDarkResponseCount, 2, "expected on dark response count of 2");
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 59 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestDarkClusterVerifierManager method testVerifierDisabled.

@Test
void testVerifierDisabled() throws InterruptedException {
    setup(false);
    RestRequest req = new RestRequestBuilder(URI.create("foo")).build();
    RestResponse res = new RestResponseBuilder().build();
    _verifierManager.onDarkResponse(req, res, DARK_CLUSTER1_NAME);
    _verifierManager.onDarkResponse(req, res, DARK_CLUSTER1_NAME);
    _verifierManager.onResponse(req, res);
    waitForLatch();
    Assert.assertEquals(_verifier.onResponseCount, 0, "expected on response count of 0");
    Assert.assertEquals(_verifier.onDarkResponseCount, 0, "expected on dark response count of 0");
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 60 with RestResponseBuilder

use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.

the class TestDarkClusterFilter method testDarkClusterAssemblyWithDarkCluster.

@Test
public void testDarkClusterAssemblyWithDarkCluster() {
    // we need to have a Mock clusterInfoProvider in order to set up a dark cluster.
    MockClusterInfoProvider clusterInfoProvider = new MockClusterInfoProvider();
    _facilities = new MockFacilities(clusterInfoProvider);
    _darkClusterStrategyFactory = new DarkClusterStrategyFactoryImpl(_facilities, SOURCE_CLUSTER_NAME, _darkClusterDispatcher, _notifier, _random, _verifierManager, _rateLimiterSupplier);
    _darkClusterStrategyFactory.start();
    DarkClusterManager darkClusterManager = new DarkClusterManagerImpl(SOURCE_CLUSTER_NAME, _facilities, _darkClusterStrategyFactory, "", "", _notifier);
    _darkClusterFilter = new DarkClusterFilter(darkClusterManager, _verifierManager);
    // set the multiplier to 1 so that traffic gets sent.
    DarkClusterConfig darkClusterConfig = createRelativeTrafficMultiplierConfig(1.0f);
    clusterInfoProvider.addDarkClusterConfig(SOURCE_CLUSTER_NAME, DARK_CLUSTER_NAME, darkClusterConfig);
    clusterInfoProvider.notifyListenersClusterAdded(SOURCE_CLUSTER_NAME);
    // send the request, expecting it to make it all the way down to the client
    RestRequest restRequest = new RestRequestBuilder(URI.create("foo")).build();
    _darkClusterFilter.onRestRequest(restRequest, new RequestContext(), new HashMap<>(), new DummyNextFilter());
    Assert.assertEquals(_client.requestAuthorityMap.size(), 1, "expected 1 request to be sent");
    _darkClusterFilter.onRestError(new RuntimeException("test"), new RequestContext(), new HashMap<>(), new DummyNextFilter());
    _darkClusterFilter.onRestResponse(new RestResponseBuilder().build(), new RequestContext(), new HashMap<>(), new DummyNextFilter());
}
Also used : DarkClusterFilter(com.linkedin.darkcluster.filter.DarkClusterFilter) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) DarkClusterStrategyFactoryImpl(com.linkedin.darkcluster.impl.DarkClusterStrategyFactoryImpl) RestRequest(com.linkedin.r2.message.rest.RestRequest) DarkClusterConfig(com.linkedin.d2.DarkClusterConfig) DarkClusterManagerImpl(com.linkedin.darkcluster.impl.DarkClusterManagerImpl) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) DarkClusterManager(com.linkedin.darkcluster.api.DarkClusterManager) Test(org.testng.annotations.Test)

Aggregations

RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)87 RestResponse (com.linkedin.r2.message.rest.RestResponse)55 Test (org.testng.annotations.Test)53 RestRequest (com.linkedin.r2.message.rest.RestRequest)22 RestException (com.linkedin.r2.message.rest.RestException)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)17 RequestContext (com.linkedin.r2.message.RequestContext)15 ByteString (com.linkedin.data.ByteString)13 Map (java.util.Map)10 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)8 HashMap (java.util.HashMap)8 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)6 RoutingException (com.linkedin.restli.server.RoutingException)6 DataMap (com.linkedin.data.DataMap)5 FilterChain (com.linkedin.r2.filter.FilterChain)5 CaptureLastCallFilter (com.linkedin.r2.testutils.filter.CaptureLastCallFilter)5 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)5 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)5 IOException (java.io.IOException)5 URI (java.net.URI)5