Skip to content

Commit 2c8cc14

Browse files
tswastlesv
authored andcommitted
1 parent ad0af31 commit 2c8cc14

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.appengine.api.datastore.DatastoreService;
22+
import com.google.appengine.api.datastore.DatastoreServiceConfig;
23+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
24+
import com.google.appengine.api.datastore.Entity;
25+
import com.google.appengine.api.datastore.FetchOptions;
26+
import com.google.appengine.api.datastore.Query;
27+
import com.google.appengine.api.datastore.ReadPolicy;
28+
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
29+
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
30+
import com.google.common.collect.ImmutableList;
31+
import org.junit.After;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
import org.junit.runner.RunWith;
35+
import org.junit.runners.JUnit4;
36+
37+
import java.util.List;
38+
39+
/**
40+
* Unit tests for {@link ReadPolicy}.
41+
*/
42+
@RunWith(JUnit4.class)
43+
public class ReadPolicyTest {
44+
private final LocalServiceTestHelper helper =
45+
new LocalServiceTestHelper(
46+
// Set 100% eventual consistency, so we can test with other job policies.
47+
// https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests
48+
new LocalDatastoreServiceTestConfig()
49+
.setDefaultHighRepJobPolicyUnappliedJobPercentage(100));
50+
51+
@Before
52+
public void setUp() {
53+
helper.setUp();
54+
}
55+
56+
@After
57+
public void tearDown() {
58+
helper.tearDown();
59+
}
60+
61+
@Test
62+
public void readPolicy_eventual_returnsNoResults() {
63+
// [START data_consistency]
64+
double deadline = 5.0;
65+
66+
// Construct a read policy for eventual consistency
67+
ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.EVENTUAL);
68+
69+
// Set the read policy
70+
DatastoreServiceConfig eventuallyConsistentConfig =
71+
DatastoreServiceConfig.Builder.withReadPolicy(policy);
72+
73+
// Set the call deadline
74+
DatastoreServiceConfig deadlineConfig = DatastoreServiceConfig.Builder.withDeadline(deadline);
75+
76+
// Set both the read policy and the call deadline
77+
DatastoreServiceConfig datastoreConfig =
78+
DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline);
79+
80+
// Get Datastore service with the given configuration
81+
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig);
82+
// [END data_consistency]
83+
84+
Entity parent = new Entity("Person", "a");
85+
Entity child = new Entity("Person", "b", parent.getKey());
86+
datastore.put(ImmutableList.<Entity>of(parent, child));
87+
88+
// Even though we are using an ancestor query, the policy is set to
89+
// eventual, so we should get eventually-consistent results. Since the
90+
// local data store test config is set to 100% unapplied jobs, there
91+
// should be no results.
92+
Query q = new Query("Person").setAncestor(parent.getKey());
93+
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
94+
assertThat(results).named("query results").isEmpty();
95+
}
96+
97+
@Test
98+
public void readPolicy_strong_returnsAllResults() {
99+
double deadline = 5.0;
100+
ReadPolicy policy = new ReadPolicy(ReadPolicy.Consistency.STRONG);
101+
DatastoreServiceConfig datastoreConfig =
102+
DatastoreServiceConfig.Builder.withReadPolicy(policy).deadline(deadline);
103+
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(datastoreConfig);
104+
105+
Entity parent = new Entity("Person", "a");
106+
Entity child = new Entity("Person", "b", parent.getKey());
107+
datastore.put(ImmutableList.<Entity>of(parent, child));
108+
109+
Query q = new Query("Person").setAncestor(parent.getKey());
110+
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
111+
assertThat(results).named("query results").hasSize(2);
112+
}
113+
}

0 commit comments

Comments
 (0)