-
Notifications
You must be signed in to change notification settings - Fork 47
Allow in-process server and channel outside of test module #187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
onobc
wants to merge
2
commits into
spring-projects:main
Choose a base branch
from
onobc:GH-144-in-process-channels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
62 changes: 62 additions & 0 deletions
62
...-grpc-core/src/main/java/org/springframework/grpc/client/CompositeGrpcChannelFactory.java
This file contains hidden or 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,62 @@ | ||
/* | ||
* Copyright 2025-2025 the original author or 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 | ||
* | ||
* https://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 org.springframework.grpc.client; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.springframework.util.Assert; | ||
|
||
import io.grpc.ManagedChannel; | ||
|
||
/** | ||
* A composite {@link GrpcChannelFactory} that combines a list of channel factories. | ||
* <p> | ||
* The composite delegates channel creation to the first composed factory that supports | ||
* the given target string. | ||
* | ||
* @author Chris Bono | ||
*/ | ||
public class CompositeGrpcChannelFactory implements GrpcChannelFactory { | ||
|
||
private List<GrpcChannelFactory> channelFactories = new ArrayList<>(); | ||
|
||
/** | ||
* Creates a new CompositeGrpcChannelFactory with the given factories. | ||
* @param channelFactories the channel factories | ||
*/ | ||
public CompositeGrpcChannelFactory(List<GrpcChannelFactory> channelFactories) { | ||
Assert.notEmpty(channelFactories, "composite channel factory requires at least one channel factory"); | ||
this.channelFactories.addAll(channelFactories); | ||
} | ||
|
||
@Override | ||
public boolean supports(String target) { | ||
return this.channelFactories.stream().anyMatch((cf) -> cf.supports(target)); | ||
} | ||
|
||
@Override | ||
public ManagedChannel createChannel(final String target, ChannelBuilderOptions options) { | ||
return this.channelFactories.stream() | ||
.filter((cf) -> cf.supports(target)) | ||
.findFirst() | ||
.orElseThrow( | ||
() -> new IllegalStateException("No grpc channel factory found that supports target : " + target)) | ||
.createChannel(target, options); | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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
62 changes: 62 additions & 0 deletions
62
...-grpc-core/src/main/java/org/springframework/grpc/client/InProcessGrpcChannelFactory.java
This file contains hidden or 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,62 @@ | ||
/* | ||
* Copyright 2024-2025 the original author or 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 | ||
* | ||
* https://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 org.springframework.grpc.client; | ||
|
||
import java.util.List; | ||
|
||
import io.grpc.ChannelCredentials; | ||
import io.grpc.Grpc; | ||
import io.grpc.inprocess.InProcessChannelBuilder; | ||
|
||
/** | ||
* {@link GrpcChannelFactory} that creates in-process gRPC channels. | ||
* | ||
* @author Chris Bono | ||
*/ | ||
public class InProcessGrpcChannelFactory extends DefaultGrpcChannelFactory<InProcessChannelBuilder> { | ||
|
||
/** | ||
* Construct an in-process channel factory instance and sets the | ||
* {@link #setVirtualTargets virtualTargets} to the identity function so that the | ||
* exact passed in target string is used as the target of the channel factory. | ||
* @param globalCustomizers the global customizers to apply to all created channels | ||
* @param interceptorsConfigurer configures the client interceptors on the created | ||
* channels | ||
*/ | ||
public InProcessGrpcChannelFactory(List<GrpcChannelBuilderCustomizer<InProcessChannelBuilder>> globalCustomizers, | ||
ClientInterceptorsConfigurer interceptorsConfigurer) { | ||
super(globalCustomizers, interceptorsConfigurer); | ||
setVirtualTargets((p) -> p); | ||
} | ||
|
||
/** | ||
* Whether this factory supports the given target string. The target can be either a | ||
* valid nameresolver-compliant URI, an authority string as described in | ||
* {@link Grpc#newChannelBuilder(String, ChannelCredentials)}. | ||
* @param target the target string as described in method javadocs | ||
* @return true if the target begins with 'in-process:' | ||
*/ | ||
@Override | ||
public boolean supports(String target) { | ||
return target.startsWith("in-process:"); | ||
} | ||
|
||
@Override | ||
protected InProcessChannelBuilder newChannelBuilder(String target, ChannelCredentials creds) { | ||
return InProcessChannelBuilder.forName(target.substring(11)); | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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
112 changes: 112 additions & 0 deletions
112
...-core/src/test/java/org/springframework/grpc/client/CompositeGrpcChannelFactoryTests.java
This file contains hidden or 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,112 @@ | ||
/* | ||
* Copyright 2023-2024 the original author or 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 | ||
* | ||
* https://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 org.springframework.grpc.client; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.grpc.ManagedChannel; | ||
|
||
/** | ||
* Unit tests for the {@link CompositeGrpcChannelFactory}. | ||
*/ | ||
@SuppressWarnings({ "unchecked", "rawtypes" }) | ||
class CompositeGrpcChannelFactoryTests { | ||
|
||
private TestChannelFactory fooChannelFactory; | ||
|
||
private TestChannelFactory barChannelFactory; | ||
|
||
private CompositeGrpcChannelFactory compositeChannelFactory; | ||
|
||
@BeforeEach | ||
void prepareFactories() { | ||
this.fooChannelFactory = new TestChannelFactory("foo"); | ||
this.barChannelFactory = new TestChannelFactory("bar"); | ||
this.compositeChannelFactory = new CompositeGrpcChannelFactory(List.of(fooChannelFactory, barChannelFactory)); | ||
} | ||
|
||
@Test | ||
void atLeastOneChannelFactoryRequired() { | ||
assertThatIllegalArgumentException().isThrownBy(() -> new CompositeGrpcChannelFactory(null)) | ||
.withMessage("composite channel factory requires at least one channel factory"); | ||
assertThatIllegalArgumentException().isThrownBy(() -> new CompositeGrpcChannelFactory(List.of())) | ||
.withMessage("composite channel factory requires at least one channel factory"); | ||
} | ||
|
||
@Test | ||
void supportsDependsOnSupportsOfComposedFactories() { | ||
assertThat(compositeChannelFactory.supports("foo")).isTrue(); | ||
assertThat(compositeChannelFactory.supports("bar")).isTrue(); | ||
assertThat(compositeChannelFactory.supports("zaa")).isFalse(); | ||
} | ||
|
||
@Test | ||
void firstComposedChannelFactorySupportsTarget() { | ||
assertThat(compositeChannelFactory.createChannel("foo")).isNotNull(); | ||
assertThat(fooChannelFactory.getActualTarget()).isEqualTo("foo"); | ||
assertThat(barChannelFactory.getActualTarget()).isNull(); | ||
} | ||
|
||
@Test | ||
void secondComposedChannelFactorySupportsTarget() { | ||
assertThat(compositeChannelFactory.createChannel("bar")).isNotNull(); | ||
assertThat(fooChannelFactory.getActualTarget()).isNull(); | ||
assertThat(barChannelFactory.getActualTarget()).isEqualTo("bar"); | ||
} | ||
|
||
@Test | ||
void noComposedChannelFactorySupportsTarget() { | ||
assertThatIllegalStateException().isThrownBy(() -> compositeChannelFactory.createChannel("zaa")) | ||
.withMessage("No grpc channel factory found that supports target : zaa"); | ||
assertThat(fooChannelFactory.getActualTarget()).isNull(); | ||
assertThat(barChannelFactory.getActualTarget()).isNull(); | ||
} | ||
|
||
static class TestChannelFactory implements GrpcChannelFactory { | ||
|
||
private String expectedTarget; | ||
|
||
private String actualTarget; | ||
|
||
TestChannelFactory(String expectedTarget) { | ||
this.expectedTarget = expectedTarget; | ||
} | ||
|
||
public boolean supports(String target) { | ||
return target.equals(this.expectedTarget); | ||
} | ||
|
||
@Override | ||
public ManagedChannel createChannel(String target, ChannelBuilderOptions options) { | ||
this.actualTarget = target; | ||
return mock(); | ||
} | ||
|
||
String getActualTarget() { | ||
return this.actualTarget; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.