-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide common HTTP Service interface for all API variants. (#2456)
Motivation: Right now four different Service API variants are provided, all with their async and sync listen* overloads. If the service type is known at compile time those overloads suffice, but in situations where the type is only determined at runtime (for example when DI is used) the user needs to write verbose code that can be provided by ServiceTalk and improve the developer experience. Modifications: This changeset introduces a new "Service" interface in the http package and it now acts as a parent for the four different service implementations. The HttpServerBuilder gets two more overloads (blocking and non-blocking) to accept the Service type and then internally performs the runtime type checking and delegates to the appropriate methods. Result: Better developer experience when the service type is not known at compile time, for example when dependency injection is used.
- Loading branch information
Showing
7 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
servicetalk-http-api/src/main/java/io/servicetalk/http/api/HttpServiceBase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.http.api; | ||
|
||
/** | ||
* Parent interface for all available HTTP services. | ||
* | ||
* @see HttpService | ||
* @see StreamingHttpService | ||
* @see BlockingHttpService | ||
* @see BlockingStreamingHttpService | ||
*/ | ||
public interface HttpServiceBase extends HttpExecutionStrategyInfluencer { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
servicetalk-http-netty/src/test/java/io/servicetalk/http/netty/HttpServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright © 2022 Apple Inc. and the ServiceTalk project authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.servicetalk.http.netty; | ||
|
||
import io.servicetalk.concurrent.api.Single; | ||
import io.servicetalk.http.api.BlockingHttpService; | ||
import io.servicetalk.http.api.BlockingStreamingHttpService; | ||
import io.servicetalk.http.api.HttpExecutionStrategy; | ||
import io.servicetalk.http.api.HttpServerContext; | ||
import io.servicetalk.http.api.HttpService; | ||
import io.servicetalk.http.api.HttpServiceBase; | ||
import io.servicetalk.http.api.StreamingHttpService; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import static io.servicetalk.transport.netty.internal.AddressUtils.localAddress; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Named.named; | ||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
||
class HttpServiceTest { | ||
|
||
@ParameterizedTest(name = "{displayName} [{index}] {0}") | ||
@MethodSource("serviceProvider") | ||
void supportsHttpServiceVariantAtRuntime(HttpServiceBase service) throws Exception { | ||
assertNotNull(HttpServers.forAddress(localAddress(0)).listenServiceAndAwait(service)); | ||
} | ||
|
||
@Test | ||
void failsOnUnknownService() { | ||
assertThrows(IllegalArgumentException.class, () -> { | ||
HttpServiceBase service = new HttpServiceBase() { | ||
@Override | ||
public HttpExecutionStrategy requiredOffloads() { | ||
return HttpServiceBase.super.requiredOffloads(); | ||
} | ||
}; | ||
|
||
try (HttpServerContext ctx = HttpServers.forAddress(localAddress(0)).listenServiceAndAwait(service)) { | ||
ctx.closeGracefully(); | ||
} | ||
}); | ||
} | ||
|
||
static Stream<Arguments> serviceProvider() { | ||
return Stream.of( | ||
arguments(named("BlockingHttpService", | ||
(BlockingHttpService) (ctx, request, responseFactory) -> responseFactory.ok())), | ||
arguments(named("BlockingStreamingHttpService", | ||
(BlockingStreamingHttpService) (ctx, request, response) -> { })), | ||
arguments(named("HttpService", | ||
(HttpService) (ctx, request, responseFactory) -> | ||
Single.succeeded(responseFactory.ok()))), | ||
arguments(named("StreamingHttpService", | ||
(StreamingHttpService) (ctx, request, responseFactory) -> | ||
Single.succeeded(responseFactory.ok()))) | ||
); | ||
} | ||
} |