Skip to content

Instantly share code, notes, and snippets.

@rikhul
Created April 15, 2012 07:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rikhul/2390821 to your computer and use it in GitHub Desktop.
Save rikhul/2390821 to your computer and use it in GitHub Desktop.
Test of ofy and task queue
package com.example.OfyAndQueues.server;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
@Entity
public class Foo {
@Id public Long id;
public int bar = 0;
}
package com.example.OfyAndQueues;
import com.example.OfyAndQueues.server.Foo;
import com.google.appengine.api.taskqueue.DeferredTask;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
public final class FooUpdateTask implements DeferredTask {
private final long fooId;
private final int newValue;
public FooUpdateTask(long fooId, int newValue) {
this.fooId = fooId;
this.newValue = newValue;
}
@Override
public void run() {
Objectify ofy = ObjectifyService.begin();
Foo f = ofy.load().key(Key.create(Foo.class, fooId)).get();
f.bar = newValue;
ofy.save().entity(f).now();
}
}
package com.example.OfyAndQueues;
import static com.google.appengine.api.taskqueue.TaskOptions.Builder.withPayload;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.example.OfyAndQueues.server.Foo;
import com.google.appengine.api.taskqueue.QueueFactory;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
public class QueueTest {
static {
ObjectifyService.register(Foo.class);
}
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),
new LocalDatastoreServiceTestConfig());
@Before
public void setup() {
helper.setUp();
}
@After
public void tearDown() {
helper.tearDown();
}
@Test
public void test() throws InterruptedException {
Objectify ofy = ObjectifyService.begin();
Foo f = new Foo();
f.bar = 5;
ofy.save().entity(f).now();
f = ofy.load().key(Key.create(Foo.class, f.id)).get();
assertEquals(5, f.bar); // all good
// update f via deferred task
QueueFactory.getDefaultQueue().add(withPayload(new FooUpdateTask(f.id, 10)));
assertTrue(latch.await(1, TimeUnit.SECONDS)); // wait for task
f = ofy.load().key(Key.create(Foo.class, f.id)).get();
System.out.println("ofy loaded " + f.bar); // returns 5, expected 10 here!?
f = ObjectifyService.begin().load().key(Key.create(Foo.class, f.id)).get();
System.out.println("new ofy loaded " + f.bar); // returns the 10 as expected
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment