1 // Copyright 2009 Google Inc. All Rights Reserved.
2 package com
.google
.appengine
.tools
.development
.testing
;
4 import com
.google
.appengine
.api
.datastore
.Key
;
5 import com
.google
.appengine
.api
.datastore
.dev
.DefaultHighRepJobPolicy
;
6 import com
.google
.appengine
.api
.datastore
.dev
.HighRepJobPolicy
;
7 import com
.google
.appengine
.api
.datastore
.dev
.LocalDatastoreService
;
8 import com
.google
.appengine
.api
.datastore
.dev
.LocalDatastoreService
.AutoIdAllocationPolicy
;
9 import com
.google
.appengine
.tools
.development
.ApiProxyLocal
;
12 * Config for accessing the local datastore service in tests.
13 * Default behavior is to configure the local datastore to only store data
14 * in-memory, to not write anything to disk, and for all jobs to apply on the
15 * first attempt (master/slave consistency model). {@link #tearDown()} wipes
16 * out all in-memory state so that the datastore is empty at the end of every
20 public final class LocalDatastoreServiceTestConfig
implements LocalServiceTestConfig
{
22 private boolean noStorage
= true;
23 private AutoIdAllocationPolicy autoIdAllocationPolicy
= AutoIdAllocationPolicy
.SEQUENTIAL
;private Integer maxQueryLifetimeMs
;private Integer maxTxnLifetimeMs
;private Integer storeDelayMs
;private String backingStoreLocation
;
24 private boolean noIndexAutoGen
= true;private Long defaultHighRepJobPolicyRandomSeed
;private Float defaultHighRepJobPolicyUnappliedJobPercentage
;private Class
<?
extends HighRepJobPolicy
> alternateHighRepJobPolicyClass
;
26 public boolean isNoStorage() {
31 * True to put the datastore into "memory-only" mode.
33 * @return {@code this} (for chaining)
35 public LocalDatastoreServiceTestConfig
setNoStorage(boolean noStorage
) {
36 this.noStorage
= noStorage
;
40 public AutoIdAllocationPolicy
getAutoIdAllocationPolicy() {
41 return autoIdAllocationPolicy
;
45 * Dictate how Put() assigns auto IDs.
46 * @param autoIdAllocationPolicy
47 * @return {@code this} (for chaining)
49 public LocalDatastoreServiceTestConfig
setAutoIdAllocationPolicy(
50 AutoIdAllocationPolicy autoIdAllocationPolicy
) {
51 this.autoIdAllocationPolicy
= autoIdAllocationPolicy
;
55 public Integer
getMaxQueryLifetimeMs() {
56 return maxQueryLifetimeMs
;
60 * Sets how long a query can stay "live" before we expire it.
61 * @param maxQueryLifetimeMs
62 * @return {@code this} (for chaining)
64 public LocalDatastoreServiceTestConfig
setMaxQueryLifetimeMs(int maxQueryLifetimeMs
) {
65 this.maxQueryLifetimeMs
= maxQueryLifetimeMs
;
69 public Integer
getMaxTxnLifetimeMs() {
70 return maxTxnLifetimeMs
;
74 * Sets how long a txn can stay "live" before we expire it.
75 * @param maxTxnLifetimeMs
76 * @return {@code this} (for chaining)
78 public LocalDatastoreServiceTestConfig
setMaxTxnLifetimeMs(int maxTxnLifetimeMs
) {
79 this.maxTxnLifetimeMs
= maxTxnLifetimeMs
;
83 public Integer
getStoreDelayMs() {
88 * Sets how long to wait before updating the persistent store. Only useful
89 * if {@link #isNoStorage()} returns {@code false}.
91 * @return {@code this} (for chaining)
93 public LocalDatastoreServiceTestConfig
setStoreDelayMs(int storeDelayMs
) {
94 this.storeDelayMs
= storeDelayMs
;
98 public String
getBackingStoreLocation() {
99 return backingStoreLocation
;
103 * Where to read/store the datastore from/to.
104 * @param backingStoreLocation
105 * @return {@code this} (for chaining)
107 public LocalDatastoreServiceTestConfig
setBackingStoreLocation(String backingStoreLocation
) {
108 this.backingStoreLocation
= backingStoreLocation
;
112 public boolean isNoIndexAutoGen() {
113 return noIndexAutoGen
;
117 * True to prevent the datastore from writing the auto-generated index file.
118 * @param noIndexAutoGen
119 * @return {@code this} (for chaining)
121 public LocalDatastoreServiceTestConfig
setNoIndexAutoGen(boolean noIndexAutoGen
) {
122 this.noIndexAutoGen
= noIndexAutoGen
;
126 public Long
getDefaultHighRepJobPolicyRandomSeed() {
127 return defaultHighRepJobPolicyRandomSeed
;
131 * A seed for a {@link java.util.Random} used by
132 * {@link DefaultHighRepJobPolicy} to determine whether or not a job
133 * application attempt fails. This method cannot be used in combination with
134 * {@link #setAlternateHighRepJobPolicyClass(Class)}.
136 * @param defaultHighRepJobPolicyRandomSeed The random seed.
137 * @return {@code this} (for chaining)
138 * @throws IllegalArgumentException If
139 * {@link #setAlternateHighRepJobPolicyClass(Class)} has been called.
141 public LocalDatastoreServiceTestConfig
setDefaultHighRepJobPolicyRandomSeed(
142 long defaultHighRepJobPolicyRandomSeed
) {
143 if (alternateHighRepJobPolicyClass
!= null) {
144 throw new IllegalArgumentException(
145 "setAlternateHighRepJobPolicyClass() has already been called.");
147 this.defaultHighRepJobPolicyRandomSeed
= defaultHighRepJobPolicyRandomSeed
;
151 public Float
getDefaultHighRepJobPolicyUnappliedJobPercentage() {
152 return defaultHighRepJobPolicyUnappliedJobPercentage
;
156 * The percentage of job application attempts that will fail. Must be
157 * >= 0 and <= 100. This validation is performed during the initialization
158 * of {@link LocalDatastoreService}, not in the setter, so it will not fail
159 * fast. In addition, any portion of the value beyond two decimal places
160 * will be truncated. This method cannot be used in combination with
161 * {@link #setAlternateHighRepJobPolicyClass(Class)}.
163 * @param defaultHighRepJobPolicyUnappliedJobPercentage The percentage of job
164 * application attempts that should fail.
165 * @return {@code this} (for chaining)
166 * @throws IllegalArgumentException If
167 * {@link #setAlternateHighRepJobPolicyClass(Class)} has been called.
169 public LocalDatastoreServiceTestConfig
setDefaultHighRepJobPolicyUnappliedJobPercentage(
170 float defaultHighRepJobPolicyUnappliedJobPercentage
) {
171 if (alternateHighRepJobPolicyClass
!= null) {
172 throw new IllegalArgumentException(
173 "setAlternateHighRepJobPolicyClass() has already been called.");
175 this.defaultHighRepJobPolicyUnappliedJobPercentage
=
176 defaultHighRepJobPolicyUnappliedJobPercentage
;
180 public Class
<?
extends HighRepJobPolicy
> getAlternateHighRepJobPolicyClass() {
181 return alternateHighRepJobPolicyClass
;
185 * An alternate {@link HighRepJobPolicy} implementation. The class must have
186 * a no-arg constructor, but this validation is performed during the
187 * iniitialization of {@link LocalDatastoreService}, not in the setter, so it
188 * will not fail fast. This method cannot be used in combination with
189 * {@link #setDefaultHighRepJobPolicyRandomSeed(long)} or
190 * {@link #setDefaultHighRepJobPolicyUnappliedJobPercentage(float)}.
192 * @param alternateHighRepJobPolicyClass The {@link HighRepJobPolicy}
194 * @return {@code this} (for chaining)
195 * @throws IllegalArgumentException If
196 * {@link #setDefaultHighRepJobPolicyRandomSeed(long)} or
197 * {@link #setDefaultHighRepJobPolicyUnappliedJobPercentage(float)} has been
200 public LocalDatastoreServiceTestConfig
setAlternateHighRepJobPolicyClass(
201 Class
<?
extends HighRepJobPolicy
> alternateHighRepJobPolicyClass
) {
202 if (defaultHighRepJobPolicyRandomSeed
!= null) {
203 throw new IllegalArgumentException(
204 "setDefaultHighRepJobPolicyRandomSeed() has already been called.");
206 if (defaultHighRepJobPolicyUnappliedJobPercentage
!= null) {
207 throw new IllegalArgumentException(
208 "defaultHighRepJobPolicyUnappliedJobPercentage() has already been called.");
210 this.alternateHighRepJobPolicyClass
= alternateHighRepJobPolicyClass
;
215 * Make the datastore a high-replication datastore, but with all jobs applying
216 * immediately (simplifies tests that use eventually-consistent queries).
218 public LocalDatastoreServiceTestConfig
setApplyAllHighRepJobPolicy() {
219 return setAlternateHighRepJobPolicyClass(ApplyAll
.class);
222 static class ApplyAll
implements HighRepJobPolicy
{
224 public boolean shouldApplyNewJob(Key entityGroup
) {
229 public boolean shouldRollForwardExistingJob(Key entityGroup
) {
235 public void setUp() {
236 ApiProxyLocal proxy
= LocalServiceTestHelper
.getApiProxyLocal();
237 proxy
.setProperty(LocalDatastoreService
.NO_STORAGE_PROPERTY
, Boolean
.toString(noStorage
));
238 proxy
.setProperty(LocalDatastoreService
.AUTO_ID_ALLOCATION_POLICY_PROPERTY
,
239 autoIdAllocationPolicy
.toString());
241 if (maxQueryLifetimeMs
!= null) {
242 proxy
.setProperty(LocalDatastoreService
.MAX_QUERY_LIFETIME_PROPERTY
,
243 Integer
.valueOf(maxQueryLifetimeMs
).toString());
246 if (maxTxnLifetimeMs
!= null) {
247 proxy
.setProperty(LocalDatastoreService
.MAX_TRANSACTION_LIFETIME_PROPERTY
,
248 Integer
.valueOf(maxTxnLifetimeMs
).toString());
251 if (storeDelayMs
!= null) {
252 proxy
.setProperty(LocalDatastoreService
.STORE_DELAY_PROPERTY
,
253 Integer
.valueOf(storeDelayMs
).toString());
256 if (backingStoreLocation
!= null) {
257 proxy
.setProperty(LocalDatastoreService
.BACKING_STORE_PROPERTY
, backingStoreLocation
);
259 proxy
.setProperty(LocalDatastoreService
.NO_INDEX_AUTO_GEN_PROP
,
260 Boolean
.toString(noIndexAutoGen
));
261 if (defaultHighRepJobPolicyRandomSeed
!= null) {
262 proxy
.setProperty(DefaultHighRepJobPolicy
.RANDOM_SEED_PROPERTY
,
263 Long
.valueOf(defaultHighRepJobPolicyRandomSeed
).toString());
266 if (defaultHighRepJobPolicyUnappliedJobPercentage
!= null) {
267 proxy
.setProperty(DefaultHighRepJobPolicy
.UNAPPLIED_JOB_PERCENTAGE_PROPERTY
,
268 Float
.valueOf(defaultHighRepJobPolicyUnappliedJobPercentage
).toString());
271 if (alternateHighRepJobPolicyClass
!= null) {
272 proxy
.setProperty(LocalDatastoreService
.HIGH_REP_JOB_POLICY_CLASS_PROPERTY
,
273 alternateHighRepJobPolicyClass
.getName());
278 public void tearDown() {
279 LocalDatastoreService datastoreService
= getLocalDatastoreService();
280 datastoreService
.clearProfiles();
281 datastoreService
.clearQueryHistory();
284 public static LocalDatastoreService
getLocalDatastoreService() {
285 return (LocalDatastoreService
) LocalServiceTestHelper
.getLocalService(
286 LocalDatastoreService
.PACKAGE
);