Skip to content

Commit

Permalink
pw_rpc: Get a MethodClient from a Method instance
Browse files Browse the repository at this point in the history
- Allow getting a MethodClient from a Method instance, rather than using
  the name or the ID, which is internal.
- Temporarily expose the ID overload of method() so that users can
  migrate to the new API.

Bug: b/235513314
Change-Id: Ia66d3b0f998c9b38351f9737ce247d92fb2cb977
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/97680
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
Commit-Queue: Wyatt Hepler <[email protected]>
Reviewed-by: Ted Pudlik <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Jun 10, 2022
1 parent c294be0 commit e29b5fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pw_rpc/java/main/dev/pigweed/pw_rpc/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,20 @@ public MethodClient method(int channelId, String fullServiceName, String methodN
}
}

/**
* Returns a MethodClient instance from a Method instance.
*/
public MethodClient method(int channelId, Method serviceMethod) {
try {
return method(channelId, serviceMethod.service().id(), serviceMethod.id());
} catch (IllegalArgumentException e) {
// Rethrow the exception with the service and method name instead of the ID.
throw new IllegalArgumentException("Unknown RPC " + serviceMethod.fullName());
}
}

/** Returns a MethodClient with the provided service and method IDs. */
synchronized MethodClient method(int channelId, int serviceId, int methodId) {
public synchronized MethodClient method(int channelId, int serviceId, int methodId) {
Channel channel = channels.get(channelId);
if (channel == null) {
throw new IllegalArgumentException("Unknown channel ID " + channelId);
Expand Down
14 changes: 14 additions & 0 deletions pw_rpc/java/test/dev/pigweed/pw_rpc/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public void method_unknownMethod() {
IllegalArgumentException.class, () -> client.method(CHANNEL_ID, "abc.Service/Method"));
assertThrows(IllegalArgumentException.class,
() -> client.method(CHANNEL_ID, "pw.rpc.test1.TheTestService/NotAnRpc").method());

Service service = new Service("throwaway.NotRealService",
Service.unaryMethod("NotAnRpc", SomeMessage.class, AnotherMessage.class));
assertThrows(IllegalArgumentException.class,
() -> client.method(CHANNEL_ID, service.method("NotAnRpc")));
}

@Test
Expand Down Expand Up @@ -176,6 +181,15 @@ public void method_accessAsServiceAndMethod() {
.isSameInstanceAs(CLIENT_STREAMING_METHOD);
}

@Test
public void method_accessFromMethodInstance() {
assertThat(client.method(CHANNEL_ID, UNARY_METHOD).method()).isSameInstanceAs(UNARY_METHOD);
assertThat(client.method(CHANNEL_ID, SERVER_STREAMING_METHOD).method())
.isSameInstanceAs(SERVER_STREAMING_METHOD);
assertThat(client.method(CHANNEL_ID, CLIENT_STREAMING_METHOD).method())
.isSameInstanceAs(CLIENT_STREAMING_METHOD);
}

@Test
public void processPacket_emptyPacket_isNotProcessed() {
assertThat(client.processPacket(new byte[] {})).isFalse();
Expand Down

0 comments on commit e29b5fd

Please sign in to comment.