core: MutableHandlerRegistry#addService with BindableService arg

This commit is contained in:
ZHANG Dapeng 2016-11-04 18:25:46 -07:00 committed by GitHub
parent d954bc2a51
commit 809cf1bc8c
2 changed files with 35 additions and 0 deletions

View File

@ -31,6 +31,7 @@
package io.grpc.util;
import io.grpc.BindableService;
import io.grpc.ExperimentalApi;
import io.grpc.HandlerRegistry;
import io.grpc.MethodDescriptor;
@ -55,11 +56,28 @@ public final class MutableHandlerRegistry extends HandlerRegistry {
private final ConcurrentMap<String, ServerServiceDefinition> services
= new ConcurrentHashMap<String, ServerServiceDefinition>();
/**
* Registers a service.
*
* @return the previously registered service with the same service descriptor name if exists,
* otherwise {@code null}.
*/
@Nullable
public ServerServiceDefinition addService(ServerServiceDefinition service) {
return services.put(service.getServiceDescriptor().getName(), service);
}
/**
* Registers a service.
*
* @return the previously registered service with the same service descriptor name if exists,
* otherwise {@code null}.
*/
@Nullable
public ServerServiceDefinition addService(BindableService bindableService) {
return addService(bindableService.bindService());
}
public boolean removeService(ServerServiceDefinition service) {
return services.remove(service.getServiceDescriptor().getName(), service);
}

View File

@ -38,6 +38,7 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import io.grpc.BindableService;
import io.grpc.MethodDescriptor.Marshaller;
import io.grpc.MethodDescriptor.MethodType;
import io.grpc.MethodDescriptor;
@ -129,6 +130,22 @@ public class MutableHandlerRegistryTest {
assertNull(registry.lookupMethod("completely/random"));
}
@Test
public void simpleLookupWithBindable() {
BindableService bindableService =
new BindableService() {
@Override
public ServerServiceDefinition bindService() {
return basicServiceDefinition;
}
};
assertNull(registry.addService(bindableService));
ServerMethodDefinition<?, ?> method = registry.lookupMethod("basic/flow");
assertSame(flowMethodDefinition, method);
}
@Test
public void multiServiceLookup() {
assertNull(registry.addService(basicServiceDefinition));