Skip to content

Commit

Permalink
Merge branch 'master' into migrate-tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
averikitsch authored May 21, 2018
2 parents f758d5c + c7a8a5a commit c2b9813
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public static void deleteOccurrence(String occurrenceName) throws Exception {

// [START get_occurrence]
/**
* Retrieves an occurrence based on it's name
* Retrieves an occurrence based on its name
*
* @param occurrenceName the name of the occurrence to delete
* format: "projects/{projectId}/occurrences/{occurrence_id}"
Expand All @@ -177,7 +177,7 @@ public static Occurrence getOccurrence(String occurrenceName) throws Exception

// [START get_note]
/**
* Retrieves a note based on it's noteId and projectId
* Retrieves a note based on its noteId and projectId
*
* @param noteId the note's unique identifier
* @param projectId the project's unique identifier
Expand Down Expand Up @@ -280,22 +280,22 @@ public static int getOccurrencesForImage(String imageUrl, String projectId) thro
// [START pubsub]
/**
* Handle incoming occurrences using a pubsub subscription
* @param subscriptionId the user-specified identifier for the pubsub subscription
* @param subId the user-specified identifier for the pubsub subscription
* @param timeout the amount of time to listen for pubsub messages (in seconds)
* @param projectId the project's unique identifier
* @return number of occurrence pubsub messages received
* @throws Exception on errors with the subscription client
*/
public static int pubSub(String subscriptionId, int timeout, String projectId) throws Exception {
public static int pubSub(String subId, int timeout, String projectId) throws Exception {
Subscriber subscriber = null;
MessageReceiverExample receiver = new MessageReceiverExample();

try {
// subscribe to the requested pubsub channel
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
ProjectSubscriptionName subName = ProjectSubscriptionName.of(projectId, subId);
subscriber = Subscriber.newBuilder(subName, receiver).build();
subscriber.startAsync().awaitRunning();
// listen to messages for 'listenTimeout' seconds
// listen to messages for 'timeout' seconds
for (int i = 0; i < timeout; i++) {
sleep(1000);
}
Expand Down Expand Up @@ -330,20 +330,20 @@ public synchronized void receiveMessage(PubsubMessage message, AckReplyConsumer

/**
* Creates and returns a pubsub subscription object listening to the occurrence topic
* @param subscriptionId the identifier you want to associate with the subscription
* @param subId the identifier you want to associate with the subscription
* @param projectId the project's unique identifier
* @throws Exception on errors with the subscription client
*/
public static Subscription createOccurrenceSubscription(String subscriptionId, String projectId)
public static Subscription createOccurrenceSubscription(String subId, String projectId)
throws Exception {
String topicId = "resource-notes-occurrences-v1alpha1";
try (SubscriptionAdminClient client = SubscriptionAdminClient.create()) {
PushConfig config = PushConfig.getDefaultInstance();
ProjectTopicName topicName = ProjectTopicName.of(projectId, topicId);
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId);
Subscription sub = client.createSubscription(subscriptionName, topicName, config, 0);
ProjectSubscriptionName subName = ProjectSubscriptionName.of(projectId, subId);
Subscription sub = client.createSubscription(subName, topicName, config, 0);
return sub;
}
}
// [END pubsub]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Date;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
Expand All @@ -40,6 +41,7 @@
*/
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
@Ignore
public class SamplesTests {

private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
Expand Down Expand Up @@ -164,6 +166,7 @@ public void testOccurrencesForImage() throws Exception {
do {
newCount = Samples.getOccurrencesForImage(imageUrl, PROJECT_ID);
sleep(SLEEP_TIME);
tries += 1;
} while (newCount != 1 && tries < TRY_LIMIT);
assertEquals(1, newCount);
assertEquals(0, origCount);
Expand All @@ -181,6 +184,7 @@ public void testOccurrencesForNote() throws Exception {
do {
newCount = Samples.getOccurrencesForNote(noteId, PROJECT_ID);
sleep(SLEEP_TIME);
tries += 1;
} while (newCount != 1 && tries < TRY_LIMIT);
assertEquals(0, origCount);
assertEquals(1, newCount);
Expand All @@ -193,14 +197,14 @@ public void testOccurrencesForNote() throws Exception {
public void testPubSub() throws Exception {
int newCount;
int tries;
String subscriptionId = "drydockOccurrences";
ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(PROJECT_ID, subscriptionId);
String subId = "drydockOccurrences";
ProjectSubscriptionName subName = ProjectSubscriptionName.of(PROJECT_ID, subId);

Samples.createOccurrenceSubscription(subscriptionId, PROJECT_ID);
Samples.createOccurrenceSubscription(subId, PROJECT_ID);
Subscriber subscriber = null;
Samples.MessageReceiverExample receiver = new Samples.MessageReceiverExample();
try {
subscriber = Subscriber.newBuilder(subscriptionName, receiver).build();
subscriber = Subscriber.newBuilder(subName, receiver).build();
subscriber.startAsync().awaitRunning();
// sleep so any messages in the queue can go through and be counted before we start the test
sleep(SLEEP_TIME);
Expand Down Expand Up @@ -229,7 +233,7 @@ public void testPubSub() throws Exception {
}
//delete subscription now that we're done with it
try (SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create()) {
subscriptionAdminClient.deleteSubscription(subscriptionName);
subscriptionAdminClient.deleteSubscription(subName);
}
}

Expand Down
8 changes: 4 additions & 4 deletions datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@
</dependencies>
<build>
<plugins>
<!-- // [START maven]-->
<!-- // [START datastore_maven]-->

<!-- // [END maven]-->
<!-- // [START exec] -->
<!-- // [END datastore_maven]-->
<!-- // [START datastore_exec] -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
Expand All @@ -69,7 +69,7 @@
<mainClass>com.google.datastore.snippets.TaskList</mainClass>
</configuration>
</plugin>
<!-- // [END exec] -->
<!-- // [END datastore_exec] -->
</plugins>
</build>
</project>
24 changes: 12 additions & 12 deletions datastore/src/main/java/com/google/datastore/snippets/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@
*/
public class TaskList {

// [START build_service]
// [START datastore_build_service]
// Create an authorized Datastore service using Application Default Credentials.
private final Datastore datastore = DatastoreOptions.getDefaultInstance().getService();

// Create a Key factory to construct keys associated with this project.
private final KeyFactory keyFactory = datastore.newKeyFactory().setKind("Task");
// [END build_service]
// [END datastore_build_service]

// [START add_entity]
// [START datastore_add_entity]
/**
* Adds a task entity to the Datastore.
*
Expand All @@ -62,9 +62,9 @@ Key addTask(String description) {
datastore.put(task);
return key;
}
// [END add_entity]
// [END datastore_add_entity]

// [START update_entity]
// [START datastore_update_entity]
/**
* Marks a task entity as done.
*
Expand All @@ -87,9 +87,9 @@ boolean markDone(long id) {
}
}
}
// [END update_entity]
// [END datastore_update_entity]

// [START retrieve_entities]
// [START datastore_retrieve_entities]
/**
* Returns a list of all task entities in ascending order of creation time.
*
Expand All @@ -100,9 +100,9 @@ Iterator<Entity> listTasks() {
Query.newEntityQueryBuilder().setKind("Task").setOrderBy(OrderBy.asc("created")).build();
return datastore.run(query);
}
// [END retrieve_entities]
// [END datastore_retrieve_entities]

// [START delete_entity]
// [START datastore_delete_entity]
/**
* Deletes a task entity.
*
Expand All @@ -112,9 +112,9 @@ Iterator<Entity> listTasks() {
void deleteTask(long id) {
datastore.delete(keyFactory.newKey(id));
}
// [END delete_entity]
// [END datastore_delete_entity]

// [START format_results]
// [START datastore_format_results]
/**
* Converts a list of task entities to a list of formatted task strings.
*
Expand All @@ -135,7 +135,7 @@ static List<String> formatTasks(Iterator<Entity> tasks) {
}
return strings;
}
// [END format_results]
// [END datastore_format_results]

/**
* Handles a single command.
Expand Down
Loading

0 comments on commit c2b9813

Please sign in to comment.