END_TEST



START_TEST(insert_create_inspect_test)
  {
  job               pjob;
  char              job_id[PBS_MAXSVRJOBID];

  strcpy(pjob.ji_qs.ji_jobid, jobids[0]);
  pjob.ji_wattr[JOB_ATR_reservation_id].at_val.at_str = rsvids[0];
  pjob.ji_wattr[JOB_ATR_exec_host].at_val.at_str = eh1;

  alps_reservations.clear();

  fail_unless(track_alps_reservation(&pjob) == 0, "couldn't create the reservation");
  fail_unless(alps_reservations.count() == 1, "incorrect count of reservations");
  pjob.ji_wattr[JOB_ATR_reservation_id].at_val.at_str = NULL;
  fail_unless(track_alps_reservation(&pjob) == 0, "track_alps_reservation failed with empty job");
  fail_unless(alps_reservations.count() == 1, "incorrect count after empty job");

  strcpy(pjob.ji_qs.ji_jobid, jobids[0]);
  pjob.ji_wattr[JOB_ATR_reservation_id].at_val.at_str = rsvids[1];
  pjob.ji_wattr[JOB_ATR_exec_host].at_val.at_str = eh1;
  fail_unless(track_alps_reservation(&pjob) == 0, "couldn't create the reservation");

  strcpy(pjob.ji_qs.ji_jobid, jobids[0]);
  pjob.ji_wattr[JOB_ATR_reservation_id].at_val.at_str = rsvids[2];
  pjob.ji_wattr[JOB_ATR_exec_host].at_val.at_str = eh1;
  fail_unless(track_alps_reservation(&pjob) == 0, "couldn't create the reservation");

  strcpy(pjob.ji_qs.ji_jobid, jobids[0]);
  pjob.ji_wattr[JOB_ATR_reservation_id].at_val.at_str = rsvids[3];
  pjob.ji_wattr[JOB_ATR_exec_host].at_val.at_str = eh1;
  fail_unless(track_alps_reservation(&pjob) == 0, "couldn't create the reservation");

  fail_unless(already_recorded(rsvids[0]) == 1, "rsv_id 0 not found");
  fail_unless(already_recorded(rsvids[1]) == 1, "rsv_id 0 not found");
  fail_unless(already_recorded(rsvids[2]) == 1, "rsv_id 0 not found");
  fail_unless(already_recorded(rsvids[3]) == 1, "rsv_id 0 not found");
  fail_unless(already_recorded((char *)"tom") == 0,     "missing rsv_id somehow found");
  fail_unless(already_recorded((char *)"tommy") == 0,   "missing rsv_id somehow found");

  fail_unless(is_orphaned(rsvids[0], job_id) == 1, "no job but not orphaned?");
  fail_unless(is_orphaned(rsvids[1], job_id) == 0, "job 1 returned but orphaned?");
  fail_unless(is_orphaned(rsvids[2], job_id) == 0, "job 2 returned but orphaned?");
  fail_unless(is_orphaned(rsvids[3], job_id) == 1, "completed job but not orphaned?");

  fail_unless(remove_alps_reservation((char *)"00") == THING_NOT_FOUND, "found something that doesn't exist");
  fail_unless(remove_alps_reservation(rsvids[0]) == 0, "couldn't remove reservation 1");
  fail_unless(remove_alps_reservation(rsvids[1]) == 0, "couldn't remove reservation 2");
  fail_unless(remove_alps_reservation(rsvids[2]) == 0, "couldn't remove reservation 3");
  fail_unless(remove_alps_reservation(rsvids[3]) == 0, "couldn't remove reservation 4");
  }
void *check_if_orphaned(

  void *vp)

  {
  char                 *rsv_id = (char *)vp;
  char                  job_id[PBS_MAXSVRJOBID];
  struct batch_request *preq;
  int                   handle = -1;
  int                   retries = 0;
  struct pbsnode       *pnode;
  char                  log_buf[LOCAL_LOG_BUF_SIZE];

  if (is_orphaned(rsv_id, job_id) == TRUE)
    {
    if((preq = alloc_br(PBS_BATCH_DeleteReservation)) == NULL)
      return NULL;
    preq->rq_extend = rsv_id;

    /* Assume the request will be successful and remove the RSV from the hash table */
    remove_alps_reservation(rsv_id);

    if ((pnode = get_next_login_node(NULL)) != NULL)
      {
      struct in_addr hostaddr;
      int            local_errno;
      pbs_net_t      momaddr;

      memcpy(&hostaddr, &pnode->nd_sock_addr.sin_addr, sizeof(hostaddr));
      momaddr = ntohl(hostaddr.s_addr);

      snprintf(log_buf, sizeof(log_buf),
        "Found orphan ALPS reservation ID %s for job %s; asking %s to remove it",
        rsv_id,
        job_id,
        pnode->nd_name);
      log_record(PBSEVENT_DEBUG, PBS_EVENTCLASS_SERVER, __func__, log_buf);

      while ((handle < 0) &&
             (retries < 3))
        {
        handle = svr_connect(momaddr, pnode->nd_mom_port, &local_errno, pnode, NULL, ToServerDIS);
        retries++;
        }

      /* unlock before the network transaction */
      unlock_node(pnode, __func__, NULL, LOGLEVEL);
      
      if (handle >= 0)
        issue_Drequest(handle, preq, true);
        
      free_br(preq);
      }
    }
  else
    free(rsv_id);

  return(NULL);
  } /* END check_if_orphaned() */