-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from jonparrott/master
Merging in updated unit testing samples
- Loading branch information
Showing
20 changed files
with
588 additions
and
535 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<queue-entries> | ||
<queue> | ||
<name>default</name> | ||
<rate>1/s</rate> | ||
</queue> | ||
<queue> | ||
<name>my-queue-name</name> | ||
<rate>3/s</rate> | ||
</queue> | ||
</queue-entries> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> | ||
|
||
<?xml version="1.0" encoding="utf-8"?> | ||
<web-app | ||
version="2.5" | ||
xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> | ||
</web-app> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
unittests/src/test/java/com/google/appengine/samples/DeferredTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Copyright 2015 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.google.appengine.samples; | ||
|
||
// [START DeferredTaskTest] | ||
|
||
import com.google.appengine.api.taskqueue.DeferredTask; | ||
import com.google.appengine.api.taskqueue.QueueFactory; | ||
import com.google.appengine.api.taskqueue.TaskOptions; | ||
import com.google.appengine.tools.development.testing.LocalServiceTestHelper; | ||
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.Assert.assertTrue; | ||
|
||
public class DeferredTaskTest { | ||
|
||
// Unlike CountDownLatch, TaskCountDownlatch lets us reset. | ||
private final LocalTaskQueueTestConfig.TaskCountDownLatch latch = | ||
new LocalTaskQueueTestConfig.TaskCountDownLatch(1); | ||
|
||
private final LocalServiceTestHelper helper = | ||
new LocalServiceTestHelper(new LocalTaskQueueTestConfig() | ||
.setDisableAutoTaskExecution(false) | ||
.setCallbackClass(LocalTaskQueueTestConfig.DeferredTaskCallback.class) | ||
.setTaskExecutionLatch(latch)); | ||
|
||
private static class MyTask implements DeferredTask { | ||
private static boolean taskRan = false; | ||
|
||
@Override | ||
public void run() { | ||
taskRan = true; | ||
} | ||
} | ||
|
||
@Before | ||
public void setUp() { | ||
helper.setUp(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
MyTask.taskRan = false; | ||
latch.reset(); | ||
helper.tearDown(); | ||
} | ||
|
||
@Test | ||
public void testTaskGetsRun() throws InterruptedException { | ||
QueueFactory.getDefaultQueue().add( | ||
TaskOptions.Builder.withPayload(new MyTask())); | ||
assertTrue(latch.await(5, TimeUnit.SECONDS)); | ||
assertTrue(MyTask.taskRan); | ||
} | ||
} | ||
// [END DeferredTaskTest] |
75 changes: 75 additions & 0 deletions
75
...sts/src/test/java/com/google/appengine/samples/LocalCustomPolicyHighRepDatastoreTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* Copyright 2015 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.google.appengine.samples; | ||
|
||
// [START LocalCustomPolicyHighRepDatastoreTest] | ||
|
||
import com.google.appengine.api.datastore.*; | ||
import com.google.appengine.api.datastore.dev.HighRepJobPolicy; | ||
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; | ||
import com.google.appengine.tools.development.testing.LocalServiceTestHelper; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static com.google.appengine.api.datastore.FetchOptions.Builder.withLimit; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class LocalCustomPolicyHighRepDatastoreTest { | ||
private static final class CustomHighRepJobPolicy implements HighRepJobPolicy { | ||
static int newJobCounter = 0; | ||
static int existingJobCounter = 0; | ||
|
||
@Override | ||
public boolean shouldApplyNewJob(Key entityGroup) { | ||
// every other new job fails to apply | ||
return newJobCounter++ % 2 == 0; | ||
} | ||
|
||
@Override | ||
public boolean shouldRollForwardExistingJob(Key entityGroup) { | ||
// every other existing job fails to apply | ||
return existingJobCounter++ % 2 == 0; | ||
} | ||
} | ||
|
||
private final LocalServiceTestHelper helper = | ||
new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig() | ||
.setAlternateHighRepJobPolicyClass(CustomHighRepJobPolicy.class)); | ||
|
||
@Before | ||
public void setUp() { | ||
helper.setUp(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
helper.tearDown(); | ||
} | ||
|
||
@Test | ||
public void testEventuallyConsistentGlobalQueryResult() { | ||
DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); | ||
ds.put(new Entity("yam")); // applies | ||
ds.put(new Entity("yam")); // does not apply | ||
// first global query only sees the first Entity | ||
assertEquals(1, ds.prepare(new Query("yam")).countEntities(withLimit(10))); | ||
// second global query sees both Entities because we "groom" (attempt to | ||
// apply unapplied jobs) after every query | ||
assertEquals(2, ds.prepare(new Query("yam")).countEntities(withLimit(10))); | ||
} | ||
} | ||
// [END LocalCustomPolicyHighRepDatastoreTest] |
Oops, something went wrong.