def testNextIndexBusyWait(self):
        """Tests busy waiting for a new index."""
        calls = [3]

        def incr(*args, **kwargs):
            result = memcache.incr(*args, **kwargs)
            memcache.incr(TEST_QUEUE.index_name)
            if calls[0] > 0:
                calls[0] -= 1
                return 100
            else:
                return result

        last_index = TEST_QUEUE.next_index(memincr=incr)
        for i in xrange(1, 4):
            self.assertEquals(
                TEST_QUEUE.FAKE_ZERO,
                int(
                    memcache.get(TEST_QUEUE.add_counter_template %
                                 fork_join_queue.knuth_hash(i))))
        self.assertEquals(
            TEST_QUEUE.FAKE_ZERO + 1,  # Extra 1 is for the writer lock!
            int(
                memcache.get(TEST_QUEUE.add_counter_template %
                             fork_join_queue.knuth_hash(4))))
  def testNextIndexBusyWait(self):
    """Tests busy waiting for a new index."""
    calls = [3]
    def incr(*args, **kwargs):
      result = memcache.incr(*args, **kwargs)
      memcache.incr(TEST_QUEUE.index_name)
      if calls[0] > 0:
        calls[0] -= 1
        return 100
      else:
        return result

    last_index = TEST_QUEUE.next_index(memincr=incr)
    for i in xrange(1, 4):
      self.assertEquals(
          TEST_QUEUE.FAKE_ZERO,
          int(memcache.get(TEST_QUEUE.add_counter_template %
                           fork_join_queue.knuth_hash(i))))
    self.assertEquals(
        TEST_QUEUE.FAKE_ZERO + 1,  # Extra 1 is for the writer lock!
        int(memcache.get(TEST_QUEUE.add_counter_template %
                         fork_join_queue.knuth_hash(4))))
    def testZeroBatchTime(self):
        """Tests that zero batch time results in no task ETA."""
        work_index = TEST_QUEUE_ZERO_BATCH_TIME.next_index()
        task = TestModel(work_index=work_index, number=1)
        db.put(task)

        before_index = memcache.get(TEST_QUEUE_ZERO_BATCH_TIME.index_name)
        self.assertEquals(work_index, fork_join_queue.knuth_hash(before_index))
        TEST_QUEUE_ZERO_BATCH_TIME.add(work_index, gettime=self.gettime1)

        # This confirms the behavior that batch_period_ms of zero will cause
        # immediate increment after adding the tasks.
        after_index = memcache.get(TEST_QUEUE_ZERO_BATCH_TIME.index_name)
        self.assertEquals(before_index + 1, after_index)

        self.assertTasksEqual(
            [self.expect_task(work_index)], testutil.get_tasks("default", usec_eta=True), check_eta=False
        )
    def testZeroBatchTime(self):
        """Tests that zero batch time results in no task ETA."""
        work_index = TEST_QUEUE_ZERO_BATCH_TIME.next_index()
        task = TestModel(work_index=work_index, number=1)
        db.put(task)

        before_index = memcache.get(TEST_QUEUE_ZERO_BATCH_TIME.index_name)
        self.assertEquals(work_index, fork_join_queue.knuth_hash(before_index))
        TEST_QUEUE_ZERO_BATCH_TIME.add(work_index, gettime=self.gettime1)

        # This confirms the behavior that batch_period_ms of zero will cause
        # immediate increment after adding the tasks.
        after_index = memcache.get(TEST_QUEUE_ZERO_BATCH_TIME.index_name)
        self.assertEquals(before_index + 1, after_index)

        self.assertTasksEqual([self.expect_task(work_index)],
                              testutil.get_tasks('default', usec_eta=True),
                              check_eta=False)