コード例 #1
0
static void consumer_wait_for(struct consumer *consumer)
{
    int res;
    struct timeval start = ast_tvnow();
    struct timespec end = {
        .tv_sec = start.tv_sec + 10,
        .tv_nsec = start.tv_usec * 1000
    };

    SCOPED_AO2LOCK(lock, consumer);

    while (!consumer->already_out) {
        res = ast_cond_timedwait(&consumer->out, ao2_object_get_lockaddr(consumer), &end);
        if (!res || res == ETIMEDOUT) {
            break;
        }
    }
}

static int remove_device_states_cb(void *obj, void *arg, int flags)
{
    struct stasis_message *msg = obj;
    struct ast_device_state_message *device_state = stasis_message_data(msg);

    if (strcmp(UNIT_TEST_DEVICE_IDENTIFIER, device_state->device)) {
        /* Not a unit test device */
        return 0;
    }

    msg = stasis_cache_clear_create(msg);
    if (msg) {
        /* topic guaranteed to have been created by this point */
        stasis_publish(ast_device_state_topic(device_state->device), msg);
    }
    ao2_cleanup(msg);
    return 0;
}

static void cache_cleanup(int unused)
{
    struct ao2_container *cache_dump;

    /* remove all device states created during this test */
    cache_dump = stasis_cache_dump_all(ast_device_state_cache(), NULL);
    if (!cache_dump) {
        return;
    }
    ao2_callback(cache_dump, 0, remove_device_states_cb, NULL);
    ao2_cleanup(cache_dump);
}

AST_TEST_DEFINE(device_state_aggregation_test)
{
    RAII_VAR(struct consumer *, consumer, NULL, ao2_cleanup);
    RAII_VAR(struct stasis_message_router *, device_msg_router, NULL, stasis_message_router_unsubscribe);
    RAII_VAR(struct ast_eid *, foreign_eid, NULL, ast_free);
    RAII_VAR(int, cleanup_cache, 0, cache_cleanup);
    RAII_VAR(struct stasis_message *, msg, NULL, ao2_cleanup);
    int res;
    struct ast_device_state_message *device_state;

    switch (cmd) {
    case TEST_INIT:
        info->name = "device_state_aggregation_test";
        info->category = "/main/devicestate/";
        info->summary = "Tests message routing and aggregation through the Stasis device state system.";
        info->description =
            "Verifies that the device state system passes "
            "messages appropriately, that the aggregator is "
            "working properly, that the aggregate results match "
            "the expected combined devstate, and that the cached "
            "aggregate devstate is correct.";
        return AST_TEST_NOT_RUN;
    case TEST_EXECUTE:
        break;
    }

    foreign_eid = ast_malloc(sizeof(*foreign_eid));
    ast_test_validate(test, NULL != foreign_eid);
    memset(foreign_eid, 0xFF, sizeof(*foreign_eid));

    consumer = consumer_create();
    ast_test_validate(test, NULL != consumer);

    device_msg_router = stasis_message_router_create(ast_device_state_topic_cached());
    ast_test_validate(test, NULL != device_msg_router);

    ao2_ref(consumer, +1);
    res = stasis_message_router_add(device_msg_router, stasis_cache_update_type(), consumer_exec, consumer);
    ast_test_validate(test, !res);

    res = stasis_message_router_add(device_msg_router, stasis_subscription_change_type(), consumer_finalize, consumer);
    ast_test_validate(test, !res);

    /* push local state */
    ast_publish_device_state(UNIT_TEST_DEVICE_IDENTIFIER, AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE);

    /* Check cache aggregate state immediately */
    ao2_cleanup(msg);
    msg = stasis_cache_get_by_eid(ast_device_state_cache(), ast_device_state_message_type(), UNIT_TEST_DEVICE_IDENTIFIER, NULL);
    device_state = stasis_message_data(msg);
    ast_test_validate(test, AST_DEVICE_NOT_INUSE == device_state->state);

    consumer_wait_for(consumer);
    ast_test_validate(test, AST_DEVICE_NOT_INUSE == consumer->state);
    ast_test_validate(test, AST_DEVICE_NOT_INUSE == consumer->aggregate_state);
    ast_test_validate(test, 2 == consumer->event_count);
    consumer_reset(consumer);

    /* push remote state */
    /* this will not produce a new aggregate state message since the aggregate state does not change */
    consumer->sig_on_non_aggregate_state = 1;
    ast_publish_device_state_full(UNIT_TEST_DEVICE_IDENTIFIER, AST_DEVICE_NOT_INUSE, AST_DEVSTATE_CACHABLE, foreign_eid);

    /* Check cache aggregate state immediately */
    ao2_cleanup(msg);
    msg = stasis_cache_get_by_eid(ast_device_state_cache(), ast_device_state_message_type(), UNIT_TEST_DEVICE_IDENTIFIER, NULL);
    device_state = stasis_message_data(msg);
    ast_test_validate(test, AST_DEVICE_NOT_INUSE == device_state->state);

    /* Check for expected events. */
    consumer_wait_for(consumer);
    ast_test_validate(test, AST_DEVICE_NOT_INUSE == consumer->state);
    ast_test_validate(test, AST_DEVICE_TOTAL == consumer->aggregate_state);
    ast_test_validate(test, 1 == consumer->event_count);
    consumer_reset(consumer);

    /* push remote state different from local state */
    ast_publish_device_state_full(UNIT_TEST_DEVICE_IDENTIFIER, AST_DEVICE_INUSE, AST_DEVSTATE_CACHABLE, foreign_eid);

    /* Check cache aggregate state immediately */
    ao2_cleanup(msg);
    msg = stasis_cache_get_by_eid(ast_device_state_cache(), ast_device_state_message_type(), UNIT_TEST_DEVICE_IDENTIFIER, NULL);
    device_state = stasis_message_data(msg);
    ast_test_validate(test, AST_DEVICE_INUSE == device_state->state);

    /* Check for expected events. */
    consumer_wait_for(consumer);
    ast_test_validate(test, AST_DEVICE_INUSE == consumer->state);
    ast_test_validate(test, AST_DEVICE_INUSE == consumer->aggregate_state);
    ast_test_validate(test, 2 == consumer->event_count);
    consumer_reset(consumer);

    /* push local state that will cause aggregated state different from local non-aggregate state */
    ast_publish_device_state(UNIT_TEST_DEVICE_IDENTIFIER, AST_DEVICE_RINGING, AST_DEVSTATE_CACHABLE);

    /* Check cache aggregate state immediately */
    ao2_cleanup(msg);
    msg = stasis_cache_get_by_eid(ast_device_state_cache(), ast_device_state_message_type(), UNIT_TEST_DEVICE_IDENTIFIER, NULL);
    device_state = stasis_message_data(msg);
    ast_test_validate(test, AST_DEVICE_RINGINUSE == device_state->state);

    /* Check for expected events. */
    consumer_wait_for(consumer);
    ast_test_validate(test, AST_DEVICE_RINGING == consumer->state);
    ast_test_validate(test, AST_DEVICE_RINGINUSE == consumer->aggregate_state);
    ast_test_validate(test, 2 == consumer->event_count);
    consumer_reset(consumer);

    return AST_TEST_PASS;
}

static int unload_module(void)
{
    AST_TEST_UNREGISTER(device2extenstate_test);
    AST_TEST_UNREGISTER(device_state_aggregation_test);
    return 0;
}
コード例 #2
0
static int scheduler(struct ast_test *test, int serialized)
{
	RAII_VAR(struct ast_taskprocessor *, tp1, NULL, ast_taskprocessor_unreference);
	RAII_VAR(struct test_data *, test_data1, ao2_alloc(sizeof(*test_data1), data_cleanup), ao2_cleanup);
	RAII_VAR(struct test_data *, test_data2, ao2_alloc(sizeof(*test_data2), data_cleanup), ao2_cleanup);
	RAII_VAR(struct ast_sip_sched_task *, task1, NULL, ao2_cleanup);
	RAII_VAR(struct ast_sip_sched_task *, task2, NULL, ao2_cleanup);
	int duration;
	int delay;
	struct timeval task1_start;

	ast_test_validate(test, test_data1 != NULL);
	ast_test_validate(test, test_data2 != NULL);

	test_data1->test = test;
	test_data1->test_start = ast_tvnow();
	test_data1->interval = 2000;
	test_data1->sleep = 1000;
	ast_mutex_init(&test_data1->lock);
	ast_cond_init(&test_data1->cond, NULL);

	test_data2->test = test;
	test_data2->test_start = ast_tvnow();
	test_data2->interval = 2000;
	test_data2->sleep = 1000;
	ast_mutex_init(&test_data2->lock);
	ast_cond_init(&test_data2->cond, NULL);

	if (serialized) {
		ast_test_status_update(test, "This test will take about %3.1f seconds\n",
			(test_data1->interval + test_data1->sleep + (MAX(test_data1->interval - test_data2->interval, 0)) + test_data2->sleep) / 1000.0);
		tp1 = ast_sip_create_serializer("test-scheduler-serializer");
		ast_test_validate(test, (tp1 != NULL));
	} else {
		ast_test_status_update(test, "This test will take about %3.1f seconds\n",
			((MAX(test_data1->interval, test_data2->interval) + MAX(test_data1->sleep, test_data2->sleep)) / 1000.0));
	}

	task1 = ast_sip_schedule_task(tp1, test_data1->interval, task_1, NULL, test_data1, AST_SIP_SCHED_TASK_FIXED);
	ast_test_validate(test, task1 != NULL);

	task2 = ast_sip_schedule_task(tp1, test_data2->interval, task_1, NULL, test_data2, AST_SIP_SCHED_TASK_FIXED);
	ast_test_validate(test, task2 != NULL);

	waitfor(test_data1);
	ast_sip_sched_task_cancel(task1);
	ast_test_validate(test, test_data1->is_servant);

	duration = ast_tvdiff_ms(test_data1->task_end, test_data1->test_start);
	ast_test_validate(test, (duration > ((test_data1->interval + test_data1->sleep) * 0.9))
		&& (duration < ((test_data1->interval + test_data1->sleep) * 1.1)));

	ast_sip_sched_task_get_times(task1, NULL, &task1_start, NULL);
	delay = ast_tvdiff_ms(task1_start, test_data1->test_start);
	ast_test_validate(test, (delay > (test_data1->interval * 0.9)
		&& (delay < (test_data1->interval * 1.1))));

	waitfor(test_data2);
	ast_sip_sched_task_cancel(task2);
	ast_test_validate(test, test_data2->is_servant);

	if (serialized) {
		ast_test_validate(test, test_data1->tid == test_data2->tid);
		ast_test_validate(test, ast_tvdiff_ms(test_data2->task_start, test_data1->task_end) >= 0);
	} else {
		ast_test_validate(test, test_data1->tid != test_data2->tid);
	}

	return AST_TEST_PASS;
}