core: add InProcessServerBuilder.generateName()

This commit is contained in:
ZHANG Dapeng 2018-02-23 16:44:10 -08:00 committed by GitHub
parent af2ae2a1e0
commit 4bac7d7084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 2 deletions

View File

@ -23,6 +23,7 @@ import io.grpc.internal.AbstractServerImplBuilder;
import io.grpc.internal.GrpcUtil;
import java.io.File;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
@ -40,11 +41,12 @@ import java.util.concurrent.TimeUnit;
* <h3>Usage example</h3>
* <h4>Server and client channel setup</h4>
* <pre>
* Server server = InProcessServerBuilder.forName("unique-name")
* String uniqueName = InProcessServerBuilder.generateName();
* Server server = InProcessServerBuilder.forName(uniqueName)
* .directExecutor() // directExecutor is fine for unit tests
* .addService(&#47;* your code here *&#47;)
* .build().start();
* ManagedChannel channel = InProcessChannelBuilder.forName("unique-name")
* ManagedChannel channel = InProcessChannelBuilder.forName(uniqueName)
* .directExecutor()
* .build();
* </pre>
@ -76,6 +78,13 @@ public final class InProcessServerBuilder
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 InProcessServerBuilder(String name) {

View File

@ -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);
}
}