mirror of https://github.com/grpc/grpc-java.git
core: add InProcessServerBuilder.generateName()
This commit is contained in:
parent
af2ae2a1e0
commit
4bac7d7084
|
|
@ -23,6 +23,7 @@ import io.grpc.internal.AbstractServerImplBuilder;
|
||||||
import io.grpc.internal.GrpcUtil;
|
import io.grpc.internal.GrpcUtil;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -40,11 +41,12 @@ import java.util.concurrent.TimeUnit;
|
||||||
* <h3>Usage example</h3>
|
* <h3>Usage example</h3>
|
||||||
* <h4>Server and client channel setup</h4>
|
* <h4>Server and client channel setup</h4>
|
||||||
* <pre>
|
* <pre>
|
||||||
* Server server = InProcessServerBuilder.forName("unique-name")
|
* String uniqueName = InProcessServerBuilder.generateName();
|
||||||
|
* Server server = InProcessServerBuilder.forName(uniqueName)
|
||||||
* .directExecutor() // directExecutor is fine for unit tests
|
* .directExecutor() // directExecutor is fine for unit tests
|
||||||
* .addService(/* your code here */)
|
* .addService(/* your code here */)
|
||||||
* .build().start();
|
* .build().start();
|
||||||
* ManagedChannel channel = InProcessChannelBuilder.forName("unique-name")
|
* ManagedChannel channel = InProcessChannelBuilder.forName(uniqueName)
|
||||||
* .directExecutor()
|
* .directExecutor()
|
||||||
* .build();
|
* .build();
|
||||||
* </pre>
|
* </pre>
|
||||||
|
|
@ -76,6 +78,13 @@ public final class InProcessServerBuilder
|
||||||
throw new UnsupportedOperationException("call forName() instead");
|
throw new UnsupportedOperationException("call forName() instead");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a new server name that is unique each time.
|
||||||
|
*/
|
||||||
|
public static String generateName() {
|
||||||
|
return UUID.randomUUID().toString();
|
||||||
|
}
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
|
||||||
private InProcessServerBuilder(String name) {
|
private InProcessServerBuilder(String name) {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,45 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2018, gRPC Authors All rights reserved.
|
||||||
|
*
|
||||||
|
* 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.grpc.inprocess;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.junit.runners.JUnit4;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for {@link InProcessServerBuilder}.
|
||||||
|
*/
|
||||||
|
@RunWith(JUnit4.class)
|
||||||
|
public class InProcessServerBuilderTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void generateName() {
|
||||||
|
String name1 = InProcessServerBuilder.generateName();
|
||||||
|
assertNotNull(name1);
|
||||||
|
assertFalse(name1.isEmpty());
|
||||||
|
|
||||||
|
String name2 = InProcessServerBuilder.generateName();
|
||||||
|
assertNotNull(name2);
|
||||||
|
assertFalse(name2.isEmpty());
|
||||||
|
|
||||||
|
assertNotEquals(name1, name2);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue