[appengine-java] Re: How do I run DeferredTasks in a Unit Test
Thanks for the question and answer, Brendan. Can you say what getQueue() is? -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine-java/-/xNPxK1h9BaIJ. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
[appengine-java] Re: How do I run DeferredTasks in a Unit Test
private QueueStateInfo getQueue(String queueName) { return LocalTaskQueueTestConfig.getLocalTaskQueue().getQueueStateInfo().get(queueName); } will do. Thanks again! -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine-java/-/r3vJS5G9gkAJ. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
Re: [appengine-java] Re: How do I run DeferredTasks in a Unit Test
This is a blast from the past. There are now official tools for unit testing deferred tasks: http://code.google.com/appengine/docs/java/tools/localunittesting.html#Writing_Deferred_Task_Tests However, ignore the latch stuff - it works erratically, and if you have an even slightly complicated system it's hard to predict how many tasks you expect ahead of time. I just add a 1s delay for the tasks to settle. If you care about unit testing with task queues, please star these two issues: http://code.google.com/p/googleappengine/issues/detail?id=6818 http://code.google.com/p/googleappengine/issues/detail?id=6819 Jeff On Thu, Feb 2, 2012 at 3:20 PM, Riley Eynon-Lynch rileyl...@gmail.com wrote: private QueueStateInfo getQueue(String queueName) { return LocalTaskQueueTestConfig.getLocalTaskQueue().getQueueStateInfo().get(queueName); } will do. Thanks again! -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine-java/-/r3vJS5G9gkAJ. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en. -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
[appengine-java] Re: How do I run DeferredTasks in a Unit Test
I've also tried the code below, however I get the following error on the line client.sendRequest(request); Perhaps I'm not encoding the task body correctly? com.google.apphosting.utils.servlet.DeferredTaskServlet: Deferred task failed exception: com.google.apphosting.utils.servlet.DeferredTaskServlet$DeferredTaskException: java.io.StreamCorruptedException: invalid stream header: EFBFBDEF String queueName = QueueFactory.getDefaultQueue().getQueueName(); ServletRunner sr = new ServletRunner(); sr.registerServlet( /_ah/queue/__deferred__, com.google.apphosting.utils.servlet.DeferredTaskServlet.class.getCanonicalName()); LocalTaskQueue localTaskQueue = LocalTaskQueueTestConfig.getLocalTaskQueue(); ServletUnitClient client = sr.newClient(); while (!getQueue().getTaskInfo().isEmpty()) { TaskStateInfo taskInfo = getQueue().getTaskInfo().iterator().next(); String queuedTask = taskInfo.getBody(); String taskName = taskInfo.getTaskName(); WebRequest request = new PostMethodWebRequest(http:/_ah/queue/__deferred__, new ByteArrayInputStream(queuedTask.getBytes()), application/x-binary-app-engine-java-runnable-task); request.setHeaderField(X-AppEngine-QueueName, queueName); try { client.sendRequest(request); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } // localTaskQueue.runTask(queueName, taskName); localTaskQueue.deleteTask(queueName, taskName); } localTaskQueue.flushQueue(queueName); -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
Re: [appengine-java] Re: How do I run DeferredTasks in a Unit Test
I'm not sure what this is not working (given that it worked previously). I'll take a look. The task is binary data. My first guess is this:- String queuedTask = taskInfo.getBody(); String is UTF-16 encoded while the body is a bunch of bytes. The process of decoding and re-encoding has a potential of corrupting the data. I suggest using *getBodyAsByteshttp://code.google.com/appengine/docs/java/tools/localunittesting/javadoc/com/google/appengine/api/taskqueue/dev/QueueStateInfo.TaskStateInfo.html#getBodyAsBytes() instead.* On Tue, Apr 26, 2011 at 11:00 PM, Brendan Doherty bren...@propertysimplified.com wrote: I've also tried the code below, however I get the following error on the line client.sendRequest(request); Perhaps I'm not encoding the task body correctly? com.google.apphosting.utils.servlet.DeferredTaskServlet: Deferred task failed exception: com.google.apphosting.utils.servlet.DeferredTaskServlet$DeferredTaskException: java.io.StreamCorruptedException: invalid stream header: EFBFBDEF String queueName = QueueFactory.getDefaultQueue().getQueueName(); ServletRunner sr = new ServletRunner(); sr.registerServlet( /_ah/queue/__deferred__, com.google.apphosting.utils.servlet.DeferredTaskServlet.class.getCanonicalName()); LocalTaskQueue localTaskQueue = LocalTaskQueueTestConfig.getLocalTaskQueue(); ServletUnitClient client = sr.newClient(); while (!getQueue().getTaskInfo().isEmpty()) { TaskStateInfo taskInfo = getQueue().getTaskInfo().iterator().next(); String queuedTask = taskInfo.getBody(); String taskName = taskInfo.getTaskName(); WebRequest request = new PostMethodWebRequest(http:/_ah/queue/__deferred__, new ByteArrayInputStream(queuedTask.getBytes()), application/x-binary-app-engine-java-runnable-task); request.setHeaderField(X-AppEngine-QueueName, queueName); try { client.sendRequest(request); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } // localTaskQueue.runTask(queueName, taskName); localTaskQueue.deleteTask(queueName, taskName); } localTaskQueue.flushQueue(queueName); -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en. -- Gianni Mariani Google, Sydney -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
Re: [appengine-java] Re: How do I run DeferredTasks in a Unit Test
Thanks. I'll give that a try and report back. Do you know if the first approach I tried with LocalTaskQueueTestConfig.getLocalTaskQueue().runTask(queueName, taskName) should ever work from a JUnit test case, or will ServletUnit always be the way to go? It seems like something didn't work properly when it reported Local task queue initialized with base url http://localhost:8080;, as later on couldn't connect with Connection to http://localhost:8080 refused. Brendan New Zealand -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.
Re: [appengine-java] Re: How do I run DeferredTasks in a Unit Test
getBodyAsBytes() solves the problem. The following code will run each of the queued DeferredTasks from within a JUnit test. String queueName = QueueFactory.getDefaultQueue().getQueueName(); ServletRunner sr = new ServletRunner(); sr.registerServlet( /_ah/queue/__deferred__, com.google.apphosting.utils.servlet.DeferredTaskServlet.class.getCanonicalName()); LocalTaskQueue localTaskQueue = LocalTaskQueueTestConfig.getLocalTaskQueue(); ServletUnitClient client = sr.newClient(); while (!getQueue().getTaskInfo().isEmpty()) { TaskStateInfo taskInfo = getQueue().getTaskInfo().iterator().next(); byte[] taskBody = taskInfo.getBodyAsBytes(); String taskName = taskInfo.getTaskName(); WebRequest request = new PostMethodWebRequest(http:/_ah/queue/__deferred__, new ByteArrayInputStream(taskBody), application/x-binary-app-engine-java-runnable-task); request.setHeaderField(X-AppEngine-QueueName, queueName); try { client.sendRequest(request); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { localTaskQueue.deleteTask(queueName, taskName); } } localTaskQueue.flushQueue(queueName); -- You received this message because you are subscribed to the Google Groups Google App Engine for Java group. To post to this group, send email to google-appengine-java@googlegroups.com. To unsubscribe from this group, send email to google-appengine-java+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/google-appengine-java?hl=en.