def RandomTask():
            """Creates a new task and randomly populates values."""
            task = taskqueue_service_pb.TaskQueueQueryTasksResponse_Task()
            task.set_task_name(''.join(
                random.choice(string.ascii_lowercase) for x in range(20)))

            task.set_eta_usec(
                int(now_sec * 1e6) + random.randint(-10e6, 600e6))

            task.set_creation_time_usec(
                min(now_sec * 1e6, task.eta_usec()) - random.randint(0, 2e7))

            task.set_url(random.choice(['/a', '/b', '/c', '/d']))
            if random.random() < 0.2:
                task.set_method(
                    taskqueue_service_pb.TaskQueueQueryTasksResponse_Task.POST)
                task.set_body('A' * 2000)
            else:
                task.set_method(
                    taskqueue_service_pb.TaskQueueQueryTasksResponse_Task.GET)
            task.set_retry_count(max(0, random.randint(-10, 5)))
            if random.random() < 0.3:
                random_headers = [('nexus', 'one'), ('foo', 'bar'),
                                  ('content-type', 'text/plain'),
                                  ('from', '*****@*****.**')]
                for _ in xrange(random.randint(1, 4)):
                    elem = random.randint(0, len(random_headers) - 1)
                    key, value = random_headers.pop(elem)
                    header_proto = task.add_header()
                    header_proto.set_key(key)
                    header_proto.set_value(value)
            return task
    def Add(self, request):
        """Inserts a new task into the store.

    Args:
      request: A taskqueue_service_pb.TaskQueueAddRequest.

    Raises:
      apiproxy_errors.ApplicationError: If a task with the same name is already
      in the store.
    """

        pos = bisect.bisect_left(self._sorted_by_name, (request.task_name(), ))
        if (pos < len(self._sorted_by_name)
                and self._sorted_by_name[pos][0] == request.task_name()):
            raise apiproxy_errors.ApplicationError(
                taskqueue_service_pb.TaskQueueServiceError.TASK_ALREADY_EXISTS)

        now = datetime.datetime.utcnow()
        now_sec = time.mktime(now.timetuple())
        task = taskqueue_service_pb.TaskQueueQueryTasksResponse_Task()
        task.set_task_name(request.task_name())
        task.set_eta_usec(request.eta_usec())
        task.set_creation_time_usec(now_sec * 1e6)
        task.set_url(request.url())
        task.set_method(request.method())
        for keyvalue in task.header_list():
            header = task.add_header()
            header.set_key(keyvalue.key())
            header.set_value(keyvalue.value())
        if request.has_description():
            task.set_description(request.description())
        if request.has_body():
            task.set_body(request.body())
        if request.has_crontimetable():
            task.mutable_crontimetable().set_schedule(
                request.crontimetable().schedule())
            task.mutable_crontimetable().set_timezone(
                request.crontimetable().timezone())
        self._InsertTask(task)