src/test/irc_queue.c
author Tero Marttila <terom@fixme.fi>
Mon, 04 May 2009 20:55:04 +0300
branchnew-transport
changeset 168 a58ad50911fc
child 171 b54f393c3df0
permissions -rw-r--r--
refactor test.c into tests/*
/**
 * @file
 *
 * Test functions for the irc_queue module
 */
#include "test.h"
#include "transport.h"

#include "../irc_queue.h"

static struct line_proto_callbacks _lp_callbacks = {
    .on_line    = NULL,
    .on_error   = NULL
};

void test_irc_queue (void)
{
    struct transport_test *tp = transport_test_create(NULL);
    transport_t *transport = transport_test_cast(tp);
    struct line_proto *lp;
    struct irc_queue *queue;
    struct irc_queue_entry *queue_entry;
    struct error_info err;

    // create the lp
    assert_success(line_proto_create(&lp, transport, 128, &_lp_callbacks, NULL, &err));

    // create the queue
    assert_success(irc_queue_create(&queue, _test_ctx.ev_base, lp, &err));

    struct irc_line line = {
        NULL, "TEST", { "fooX" }
    };

    // then test simple writes, we should be able to push five lines directly
    log_info("test irc_queue_process (irc_queue_send_direct)");
    line.args[0] = "foo0"; assert_success(irc_queue_process(queue, &line));
    line.args[0] = "foo1"; assert_success(irc_queue_process(queue, &line));
    line.args[0] = "foo2"; assert_success(irc_queue_process(queue, &line));
    line.args[0] = "foo3"; assert_success(irc_queue_process(queue, &line));
    line.args[0] = "foo4"; assert_success(irc_queue_process(queue, &line));

    // they should all be output
    assert_transport_data(tp,
            "TEST foo0\r\n"
            "TEST foo1\r\n"
            "TEST foo2\r\n"
            "TEST foo3\r\n"
            "TEST foo4\r\n"
    );

    // then enqueue
    log_info("test irc_queue_process (irc_queue_put)");
    line.args[0] = "foo5"; assert_success(irc_queue_process(queue, &line));

    // ensure it was enqueued
    assert((queue_entry = TAILQ_FIRST(&queue->list)) != NULL);
    assert_strcmp(queue_entry->line_buf, "TEST foo5\r\n");

    // ensure timer is set
    assert(event_pending(queue->ev, EV_TIMEOUT, NULL));

    // run the event loop to let the timer run
    log_info("running the event loop once...");
    assert(event_base_loop(_test_ctx.ev_base, EVLOOP_ONCE) == 0);

    // test to check that the line was now sent
    log_info("checking that the delayed line was sent...");
    assert_transport_data(tp, "TEST foo5\r\n");
    assert(TAILQ_EMPTY(&queue->list));
    assert(!event_pending(queue->ev, EV_TIMEOUT, NULL));

    // cleanup
    irc_queue_destroy(queue);
}