@Test
public void testPullWithGroupByTag() throws Exception {
String groupTag = "testPullWithGroupTag";
String taskBaseName = groupTag + "_" + getTimeStampRandom();
TaskHandle th1 = queue.add(withMethod(PULL).tag(taskBaseName).payload("foobar", "UTF-8"));
TaskHandle th2 = queue.add(withMethod(PULL).tag(taskBaseName + "other").payload("foofoo", "UTF-8"));
TaskHandle th3 = queue.add(withMethod(PULL).tag(taskBaseName).payload("foofoo".getBytes(), "UTF-8"));
sync(5000); // Give some time for the tasks to be available for lease.
try {
// If options specifies no tag, but does specify groupByTag,
// only tasks having the same tag as the task with earliest eta will be returned.
LeaseOptions options = LeaseOptions.Builder.withLeasePeriod(30, TimeUnit.SECONDS).countLimit(100).groupByTag();
List<TaskHandle> handles = queue.leaseTasks(options);
// The first and third tasks with the same tag should be returned.
assertEquals(2, handles.size());
Set<String> createdHandles = new HashSet<>();
createdHandles.add(th1.getName());
createdHandles.add(th3.getName());
Set<String> returnedHandles = new HashSet<>();
returnedHandles.add(handles.get(0).getName());
returnedHandles.add(handles.get(1).getName());