Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datastore Read Consistency Policy sample. #210

Merged
merged 1 commit into from
Apr 28, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* Copyright 2016 Google Inc. 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 com.example.appengine;

import static com.google.common.truth.Truth.assertThat;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceConfig;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.ReadPolicy;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.common.collect.ImmutableList;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.List;

/**
* Unit tests for {@link ReadPolicy}.
*/
@RunWith(JUnit4.class)
public class ReadPolicyTest {
private final LocalServiceTestHelper helper =
new LocalServiceTestHelper(
// Set 100% eventual consistency, so we can test with other job policies.
// https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests
new LocalDatastoreServiceTestConfig()
.setDefaultHighRepJobPolicyUnappliedJobPercentage(100));

@Before
public void setUp() {
helper.setUp();
}

@After
public void tearDown() {
helper.tearDown();
}

@Test
public void readPolicy_eventual_returnsNoResults() {
// [START data_consistency]
double deadline = 5.0;

// Construct a read policy for eventual consistency
ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.EVENTUAL);

// Set the read policy
DatastoreServiceConfig eventuallyConsistentConfig =
DatastoreServiceConfig.Builder.withReadPolicy(policy);

// Set the call deadline
DatastoreServiceConfig deadlineConfig = DatastoreServiceConfig.Builder.withDeadline(deadline);

// Set both the read policy and the call deadline
DatastoreServiceConfig datastoreConfig =
DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline);

// Get Datastore service with the given configuration
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig);
// [END data_consistency]

Entity parent = new Entity("Person", "a");
Entity child = new Entity("Person", "b", parent.getKey());
datastore.put(ImmutableList.<Entity>of(parent, child));

// Even though we are using an ancestor query, the policy is set to
// eventual, so we should get eventually-consistent results. Since the
// local data store test config is set to 100% unapplied jobs, there
// should be no results.
Query q = new Query("Person").setAncestor(parent.getKey());
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").isEmpty();
}

@Test
public void readPolicy_strong_returnsAllResults() {
double deadline = 5.0;
ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.STRONG);
DatastoreServiceConfig datastoreConfig =
DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig);

Entity parent = new Entity("Person", "a");
Entity child = new Entity("Person", "b", parent.getKey());
datastore.put(ImmutableList.<Entity>of(parent, child));

Query q = new Query("Person").setAncestor(parent.getKey());
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
assertThat(results).named("query results").hasSize(2);
}
}