Skip to content

Commit

Permalink
More updates to address PR comments
Browse files Browse the repository at this point in the history
* Making template constants private
* Adding helper methods to extract variables from template paths
* Making it possible to have multiple instances of LocalPubsubHelper
* Removing getChannel from *Api classes (not used anywhere)
* Other cleanup
  • Loading branch information
garrettjonesgoogle committed Nov 19, 2015
1 parent 4fbfb7b commit add0cc3
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import io.gapi.gax.grpc.ServiceApiSettings;
import io.gapi.gax.internal.ApiUtils;
import io.gapi.gax.protobuf.PathTemplate;
import io.grpc.Channel;
import io.grpc.ManagedChannel;

import java.io.IOException;
Expand Down Expand Up @@ -151,14 +150,14 @@ public Iterable<String> extractResources(ListTopicSubscriptionsResponse payload)
* A PathTemplate representing the fully-qualified path to represent
* a project resource.
*/
public static final PathTemplate PROJECT_PATH_TEMPLATE =
private static final PathTemplate PROJECT_PATH_TEMPLATE =
PathTemplate.create("/projects/{project}");

/**
* A PathTemplate representing the fully-qualified path to represent
* a topic resource.
*/
public static final PathTemplate TOPIC_PATH_TEMPLATE =
private static final PathTemplate TOPIC_PATH_TEMPLATE =
PathTemplate.create("/projects/{project}/topics/{topic}");

// ========
Expand Down Expand Up @@ -220,13 +219,28 @@ public static final String createTopicPath(String project, String topic) {
"project", project,"topic", topic);
}

/**
* Extracts the project from the given fully-qualified path which
* represents a project resource.
*/
public static final String extractProjectFromProjectPath(String projectPath) {
return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project");
}

// ========
// Getters
// ========
/**
* Extracts the project from the given fully-qualified path which
* represents a topic resource.
*/
public static final String extractProjectFromTopicPath(String topicPath) {
return TOPIC_PATH_TEMPLATE.parse(topicPath).get("project");
}

public Channel getChannel() {
return channel;
/**
* Extracts the topic from the given fully-qualified path which
* represents a topic resource.
*/
public static final String extractTopicFromTopicPath(String topicPath) {
return TOPIC_PATH_TEMPLATE.parse(topicPath).get("topic");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import io.gapi.gax.grpc.ServiceApiSettings;
import io.gapi.gax.internal.ApiUtils;
import io.gapi.gax.protobuf.PathTemplate;
import io.grpc.Channel;
import io.grpc.ManagedChannel;

import java.io.IOException;
Expand Down Expand Up @@ -132,14 +131,14 @@ public Iterable<Subscription> extractResources(ListSubscriptionsResponse payload
* A PathTemplate representing the fully-qualified path to represent
* a project resource.
*/
public static final PathTemplate PROJECT_PATH_TEMPLATE =
private static final PathTemplate PROJECT_PATH_TEMPLATE =
PathTemplate.create("/projects/{project}");

/**
* A PathTemplate representing the fully-qualified path to represent
* a subscription resource.
*/
public static final PathTemplate SUBSCRIPTION_PATH_TEMPLATE =
private static final PathTemplate SUBSCRIPTION_PATH_TEMPLATE =
PathTemplate.create("/projects/{project}/subscriptions/{subscription}");

// ========
Expand Down Expand Up @@ -168,7 +167,11 @@ public static SubscriberApi create(ServiceApiSettings settings) throws IOExcepti
return new SubscriberApi(settings);
}

private SubscriberApi(ServiceApiSettings settings) throws IOException {
/**
* Constructs an instance of SubscriberApi, using the given settings. This is protected so that it
* easy to make a subclass, but otherwise, the static factory methods should be preferred.
*/
protected SubscriberApi(ServiceApiSettings settings) throws IOException {
ServiceApiSettings internalSettings = ApiUtils.settingsWithChannels(settings,
SERVICE_ADDRESS, DEFAULT_SERVICE_PORT, ALL_SCOPES);
this.settings = internalSettings;
Expand Down Expand Up @@ -197,13 +200,28 @@ public static final String createSubscriptionPath(String project, String subscri
"project", project,"subscription", subscription);
}

/**
* Extracts the project from the given fully-qualified path which
* represents a project resource.
*/
public static final String extractProjectFromProjectPath(String projectPath) {
return PROJECT_PATH_TEMPLATE.parse(projectPath).get("project");
}

// ========
// Getters
// ========
/**
* Extracts the project from the given fully-qualified path which
* represents a subscription resource.
*/
public static final String extractProjectFromSubscriptionPath(String subscriptionPath) {
return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("project");
}

public Channel getChannel() {
return channel;
/**
* Extracts the subscription from the given fully-qualified path which
* represents a subscription resource.
*/
public static final String extractSubscriptionFromSubscriptionPath(String subscriptionPath) {
return SUBSCRIPTION_PATH_TEMPLATE.parse(subscriptionPath).get("subscription");
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public void getTopic(GetTopicRequest request, StreamObserver<Topic> responseObse
public void listTopics(ListTopicsRequest request, StreamObserver<ListTopicsResponse> responseObserver) {
List<Topic> responseTopics = new ArrayList<>();
for (String topicName : topics.keySet()) {
String projectOfTopic = PublisherApi.TOPIC_PATH_TEMPLATE.parse(topicName).get("project");
String projectOfRequest = PublisherApi.PROJECT_PATH_TEMPLATE.parse(request.getProject()).get("project");
String projectOfTopic = PublisherApi.extractProjectFromTopicPath(topicName);
String projectOfRequest = PublisherApi.extractProjectFromProjectPath(request.getProject());
if (projectOfTopic.equals(projectOfRequest)) {
Topic topicObj = Topic.newBuilder().setName(topicName).build();
responseTopics.add(topicObj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@
* A class that runs an in-memory Publisher instance for use in tests.
*/
public class LocalPubsubHelper {
private static final SocketAddress address = new LocalAddress("in-process-1");
private static Server server;
private static LocalPublisherImpl publisherImpl;
private static int FLOW_CONTROL_WINDOW = 65 * 1024;

private final SocketAddress address;
private final Server server;
private final LocalPublisherImpl publisherImpl;

/**
* Constructs a new LocalPubsubHelper. The method start() must
* be called before it is used.
*/
public LocalPubsubHelper() {
public LocalPubsubHelper(String addressString) {
address = new LocalAddress(addressString);
publisherImpl = new LocalPublisherImpl();
NettyServerBuilder builder = NettyServerBuilder
.forAddress(address)
.flowControlWindow(65 * 1024)
.flowControlWindow(FLOW_CONTROL_WINDOW)
.channelType(LocalServerChannel.class);
builder.addService(PublisherGrpc.bindService(publisherImpl));
server = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class PublisherApiTest {

@BeforeClass
public static void startStaticServer() {
pubsubHelper = new LocalPubsubHelper().start();
pubsubHelper = new LocalPubsubHelper("in-process-1").start();
}

@AfterClass
Expand Down

0 comments on commit add0cc3

Please sign in to comment.