Пример #1
0
/* Create a JSON array of 'job' objects, representing the head of the queue.
 * 'max_entries' determines the max number of jobs to return, 0=unlimited.
 * Returns JSON object which the caller must free.  On error, return NULL
 * with errno set:
 *
 * EPROTO - malformed or empty attrs array, max_entries out of range
 * ENOMEM - out of memory
 */
json_t *list_job_array (struct queue *queue, int max_entries, json_t *attrs)
{
    json_t *jobs = NULL;
    struct job *job;
    int saved_errno;

    if (max_entries < 0 || !json_is_array (attrs)
                        || json_array_size (attrs) == 0) {
        errno = EPROTO;
        goto error;
    }
    if (!(jobs = json_array ()))
        goto error_nomem;
    job = queue_first (queue);
    while (job) {
        json_t *o;
        if (!(o = list_one_job (job, attrs)))
            goto error;
        if (json_array_append_new (jobs, o) < 0) {
            json_decref (o);
            goto error_nomem;
        }
        if (json_array_size (jobs) == max_entries)
            break;
        job = queue_next (queue);
    }
    return jobs;
error_nomem:
    errno = ENOMEM;
error:
    saved_errno = errno;
    json_decref (jobs);
    errno = saved_errno;
    return NULL;
}
Пример #2
0
int main (int argc, char *argv[])
{
    const int q_size = 16;
    struct queue *q;
    struct job *j;
    json_t *attrs;
    json_t *badattrs;
    json_t *o;
    json_t *el;
    json_t *id_o;
    flux_jobid_t id;

    plan (NO_PLAN);

    q = make_test_queue (q_size);
    if (!(attrs = json_pack ("[s]", "id")))
        BAIL_OUT ("json_pack failed");

    /* list_job_array */

    o = list_job_array (q, 0, attrs);
    ok (o != NULL && json_is_array (o),
        "list_job_array returns array");
    ok (json_array_size (o) == q_size,
        "array has expected size");
    json_decref (o);

    o = list_job_array (q, 4, attrs);
    ok (o != NULL && json_is_array (o),
        "list_job_array max_entries=4 returns array");
    ok (json_array_size (o) == 4,
        "array has expected size");
    el = json_array_get (o, 1);
    ok (el != NULL && json_is_object (el),
        "array[1] is an object");
    ok ((id_o = json_object_get (el, "id")) != NULL,
        "array[1] id is set");
    id = json_integer_value (id_o);
    ok (id == 1,
        "array[1] id=1");
    if (id != 1)
        diag ("id=%d", (int)id);
    ok (json_object_size (el) == 1,
        "array[1] size=1");
    json_decref (o);

    errno = 0;
    ok (list_job_array (q, -1, attrs) == NULL && errno == EPROTO,
        "list_job_array max_entries < 0 fails with EPROTO");

    errno = 0;
    ok (list_job_array (q, -1, NULL) == NULL && errno == EPROTO,
        "list_job_array attrs=NULL fails with EPROTO");

    /* list_one_job */

    if (!(j = queue_lookup_by_id (q, 0)))
        BAIL_OUT ("queue_lookup_by_id 0 failed");
    if (!(badattrs = json_pack ("[s]", "foo")))
        BAIL_OUT ("json_pack failed");
    errno = 0;
    ok (list_one_job (j, badattrs) == NULL && errno == EINVAL,
        "list_one_job attrs=[\"foo\"] fails with EINVAL");
    json_decref (badattrs);

    if (!(badattrs = json_pack ("[i]", 42)))
        BAIL_OUT ("json_pack failed");
    errno = 0;
    ok (list_one_job (j, badattrs) == NULL && errno == EPROTO,
        "list_one_job attrs=[42] fails with EPROTO");
    json_decref (badattrs);


    json_decref (attrs);
    queue_destroy (q);

    done_testing ();
}