示例#1
0
int main()
{
    srand(time(NULL));
    HANDLE mutex = CreateMutex(NULL, FALSE, NULL);

    array_t *array = array_create(4);
    consumer_t *consumer1 = consumer_create(array, mutex, 1);
    consumer_t *consumer2 = consumer_create(array, mutex, 2);
    producer_t *producer1 = producer_create(array, mutex);
    producer_t *producer2 = producer_create(array, mutex);

    Sleep(5000);

    consumer_destroy(consumer1);
    consumer_destroy(consumer2);
    producer_destroy(producer1);
    producer_destroy(producer2);
    CloseHandle(mutex);

    return 0;
}
示例#2
0
int main(void){
	srand(time(NULL));

	numbers_t * numbers = numbers_create();                             //consumer.c


	producer_t prod1 = producer_create(numbers);
	consumer_t cons1 = consumer_create(numbers);
	producer_t prod2 = producer_create(numbers);
	consumer_t cons2 = consumer_create(numbers);

	consumer_join(cons2);


	producer_destructor(prod1);
	consumer_destructor(cons1);
	producer_destructor(prod2);
	consumer_destructor(cons2);
	numbers_destructor(numbers);                                        //consumer.c


	return 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;
}