CASE_TEST(atbus_endpoint, is_child)
{
    atbus::node::conf_t conf;
    atbus::node::default_conf(&conf);
    conf.children_mask = 16;

    {
        atbus::node::ptr_t node = atbus::node::create();
        node->init(0x12345678, &conf);


        // 0值边界检测
        CASE_EXPECT_TRUE(node->is_child_node(0x12340000));
        CASE_EXPECT_TRUE(node->is_child_node(0x1234FFFF));
        CASE_EXPECT_FALSE(node->is_child_node(0x1233FFFF));
        CASE_EXPECT_FALSE(node->is_child_node(0x12350000));

        // 自己是自己的子节点
        CASE_EXPECT_TRUE(node->is_child_node(node->get_id()));
    }

    {
        conf.children_mask = 0;
        atbus::node::ptr_t node = atbus::node::create();
        node->init(0x12345678, &conf);
        // 0值判定,无子节点
        CASE_EXPECT_TRUE(node->is_child_node(0x12345678));
        CASE_EXPECT_FALSE(node->is_child_node(0x12345679));
    }
}
CASE_TEST(atbus_endpoint, is_brother)
{
    uint32_t fake_mask = 24;
    atbus::node::conf_t conf;
    atbus::node::default_conf(&conf);
    conf.children_mask = 16;

    atbus::node::ptr_t node = atbus::node::create();
    node->init(0x12345678, &conf);

    // 自己不是自己的兄弟节点
    CASE_EXPECT_FALSE(node->get_self_endpoint()->is_brother_node(node->get_id(), fake_mask));

    /*       F               F
    //      / \             / \
    //    [A]  B          [A]  F
    //                        / \
    //                       X   B
    // 兄弟节点的子节点仍然是兄弟节点
    */
    CASE_EXPECT_TRUE(node->get_self_endpoint()->is_brother_node(0x12335678, fake_mask));

    /*       B
    //      / \
    //    [A]  X
    // 父节点是兄弟节点
    */
    CASE_EXPECT_TRUE(node->get_self_endpoint()->is_brother_node(0x12000001, fake_mask));
    
    
    /*      [A]
    //      / \
    //     B   X
    // 子节点不是兄弟节点
    */
    CASE_EXPECT_FALSE(node->get_self_endpoint()->is_brother_node(0x12340001, fake_mask));

    /*         F
    //        / \
    //       F   B
    //      / \
    //    [A]  X
    // 父节点的兄弟节点不是兄弟节点
    */
    CASE_EXPECT_FALSE(node->get_self_endpoint()->is_brother_node(0x11345678, fake_mask));

    // 0值判定,无父节点
    CASE_EXPECT_TRUE(node->get_self_endpoint()->is_brother_node(0x12000001, 0));
    CASE_EXPECT_FALSE(node->get_self_endpoint()->is_brother_node(0x12340001, 0));
}
Exemplo n.º 3
0
CASE_TEST(coroutine_task, auto_finish)
{
    typedef std::shared_ptr< cotask::task<> > task_ptr_type;
    {
        test_context_task_mem_function obj;
        task_ptr_type co_task = cotask::task<>::create(&test_context_task_mem_function::real_run, &obj);
        g_test_coroutine_task_status = 0;
        obj.task_id_ = co_task->get_id();
    }
    CASE_EXPECT_EQ(0, g_test_coroutine_task_status); 

    {
        test_context_task_mem_function obj;
        task_ptr_type co_task = cotask::task<>::create(&test_context_task_mem_function::real_run, &obj);
        g_test_coroutine_task_status = 0;
        obj.task_id_ = co_task->get_id();

        CASE_EXPECT_EQ(0, co_task->start());

        ++ g_test_coroutine_task_status;
        CASE_EXPECT_EQ(g_test_coroutine_task_status, 2);

        CASE_EXPECT_FALSE(co_task->is_completed());
    }

    ++ g_test_coroutine_task_status;
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 4);
}
Exemplo n.º 4
0
CASE_TEST(coroutine_task, custom_action)
{
    typedef std::shared_ptr< cotask::task<> > task_ptr_type;
    cotask::task<>::action_ptr_t action = cotask::task<>::action_ptr_t(new test_context_task_action());
    task_ptr_type co_task = cotask::task<>::create(action);
    task_ptr_type co_another_task = cotask::task<>::create(action); // share action

    g_test_coroutine_task_status = 0;

    CASE_EXPECT_EQ(0, co_task->start());

    CASE_EXPECT_EQ(g_test_coroutine_task_status, 1);
    CASE_EXPECT_FALSE(co_task->is_completed());

    CASE_EXPECT_EQ(0, co_another_task->start());
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 2);

    CASE_EXPECT_EQ(0, co_task->resume());
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 3);

    CASE_EXPECT_EQ(0, co_another_task->resume());
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 4);

    CASE_EXPECT_TRUE(co_task->is_completed());
    CASE_EXPECT_TRUE(co_another_task->is_completed());

    CASE_EXPECT_GT(0, co_another_task->resume());
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 4);

    ++ g_test_coroutine_task_status;
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 5);

    CASE_EXPECT_NE(co_task->get_id(), 0);
}
static void test_this_context_thread_func(copp::coroutine_context_default& co) {

    test_this_context_get_cotoutine_runner* runner = dynamic_cast<test_this_context_get_cotoutine_runner*>(co.get_runner());
    runner->set_co_obj(&co);

    CASE_EXPECT_FALSE(runner->is_run());

    co.start();

    CASE_EXPECT_TRUE(runner->is_run());
}
Exemplo n.º 6
0
CASE_TEST(coroutine_task, function_action)
{
    {
        typedef std::shared_ptr< cotask::task<> > task_ptr_type;
        task_ptr_type co_task = cotask::task<>::create(test_context_task_function_1);
        g_test_coroutine_task_status = 0;

        CASE_EXPECT_EQ(0, co_task->start());

        ++ g_test_coroutine_task_status;
        CASE_EXPECT_EQ(g_test_coroutine_task_status, 2);

        CASE_EXPECT_FALSE(co_task->is_completed());
        CASE_EXPECT_EQ(0, co_task->resume());

        CASE_EXPECT_TRUE(co_task->is_completed());

        ++ g_test_coroutine_task_status;
        CASE_EXPECT_EQ(g_test_coroutine_task_status, 4);
        CASE_EXPECT_EQ(co_task->get_coroutine_context().get_ret_code(), 100);
    }

    {
        typedef std::shared_ptr< cotask::task<> > task_ptr_type;
        task_ptr_type co_task = cotask::task<>::create(test_context_task_function_2);
        g_test_coroutine_task_status = 0;

        CASE_EXPECT_EQ(0, co_task->start());

        ++ g_test_coroutine_task_status;
        CASE_EXPECT_EQ(g_test_coroutine_task_status, 2);

        CASE_EXPECT_FALSE(co_task->is_completed());
        CASE_EXPECT_EQ(0, co_task->resume());

        CASE_EXPECT_TRUE(co_task->is_completed());

        ++ g_test_coroutine_task_status;
        CASE_EXPECT_EQ(g_test_coroutine_task_status, 4);
    }
}
CASE_TEST(atbus_endpoint, is_father)
{
    uint32_t fake_mask = 24;
    atbus::node::conf_t conf;
    atbus::node::default_conf(&conf);
    conf.children_mask = 16;

    atbus::node::ptr_t node = atbus::node::create();
    node->init(0x12345678, &conf);
    CASE_EXPECT_TRUE(node->get_self_endpoint()->is_parent_node(0x12000001, 0x12000001, fake_mask));

    CASE_EXPECT_FALSE(node->get_self_endpoint()->is_parent_node(0x12000002, 0x12000001, fake_mask));
}
CASE_TEST(this_context, yield) {
    typedef copp::coroutine_context_default co_type;

    co_type co;
    co.create(new test_this_context_yield_runner(), 32 * 1024);

    co.start();

    test_this_context_yield_runner* runner = dynamic_cast<test_this_context_yield_runner*>(co.get_runner());
    CASE_EXPECT_TRUE(runner->is_run());
    CASE_EXPECT_FALSE(runner->is_finished());

    assert(co.get_runner());
    delete runner;
}
static void recv_callback_check_fn(
    atbus::channel::io_stream_channel* channel,         // 事件触发的channel
    atbus::channel::io_stream_connection* connection,   // 事件触发的连接
    int status,                         // libuv传入的转态码
    void* input,                        // 额外参数(不同事件不同含义)
    size_t s                            // 额外参数长度
    ) {
    CASE_EXPECT_NE(NULL, channel);
    CASE_EXPECT_NE(NULL, connection);

    if (status < 0) {
        CASE_EXPECT_EQ(NULL, input);
        CASE_EXPECT_EQ(0, s);

        CASE_EXPECT_TRUE(UV_EOF == channel->error_code || UV_ECONNRESET == channel->error_code);
        return;
    }

    CASE_EXPECT_NE(NULL, input);
    CASE_EXPECT_EQ(0, status);
    CASE_EXPECT_EQ(0, channel->error_code);

    CASE_EXPECT_FALSE(g_check_buff_sequence.empty());
    if (g_check_buff_sequence.empty()) {
        return;
    }

    ++ g_recv_rec.first;
    g_recv_rec.second += s;

    CASE_EXPECT_EQ(s, g_check_buff_sequence.front().second);
    char* buff = get_test_buffer();
    char* input_buff = reinterpret_cast<char*>(input);
    for (size_t i = 0; i < g_check_buff_sequence.front().second; ++ i) {
        CASE_EXPECT_EQ(buff[i + g_check_buff_sequence.front().first], input_buff[i]);
        if (buff[i + g_check_buff_sequence.front().first] != input_buff[i]) {
            break;
        }
    }
    g_check_buff_sequence.pop_front();

    ++g_check_flag;
}
Exemplo n.º 10
0
CASE_TEST(coroutine_task, coroutine_context_yield)
{
    typedef std::shared_ptr< cotask::task<> > task_ptr_type;
    task_ptr_type co_task = cotask::task<>::create(test_context_task_function_3);
    g_test_coroutine_task_status = 0;

    CASE_EXPECT_EQ(0, co_task->start());

    ++ g_test_coroutine_task_status;
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 2);

    CASE_EXPECT_FALSE(co_task->is_completed());
    CASE_EXPECT_EQ(cotask::EN_TS_WAITING, co_task->get_status());
    CASE_EXPECT_EQ(0, co_task->resume());

    CASE_EXPECT_TRUE(co_task->is_completed());

    ++ g_test_coroutine_task_status;
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 4);

    CASE_EXPECT_NE(co_task->get_id(), 0);
}
Exemplo n.º 11
0
CASE_TEST(coroutine_task, mem_function_action)
{
    typedef std::shared_ptr< cotask::task<> > task_ptr_type;
    test_context_task_mem_function obj;
    task_ptr_type co_task = cotask::task<>::create(&test_context_task_mem_function::real_run, &obj);
    g_test_coroutine_task_status = 0;
    obj.task_id_ = co_task->get_id();

    CASE_EXPECT_EQ(0, co_task->start());

    ++ g_test_coroutine_task_status;
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 2);

    CASE_EXPECT_FALSE(co_task->is_completed());
    CASE_EXPECT_EQ(0, co_task->resume());

    CASE_EXPECT_TRUE(co_task->is_completed());

    ++ g_test_coroutine_task_status;
    CASE_EXPECT_EQ(g_test_coroutine_task_status, 4);

    CASE_EXPECT_NE(co_task->get_coroutine_context().get_ret_code(), -1);
}
Exemplo n.º 12
0
CASE_TEST(buffer, static_buffer_manager_circle_fb)
{
    atbus::detail::buffer_manager mgr;
    CASE_EXPECT_TRUE(mgr.empty());

    mgr.set_mode(1023, 10);
    CASE_EXPECT_FALSE(mgr.set_limit(2048, 10));

    CASE_EXPECT_EQ(1023, mgr.limit().limit_size_);
    CASE_EXPECT_EQ(10, mgr.limit().limit_number_);

    // size limit
    void* pointer;
    void* check_ptr[4];
    size_t s, sr;
    int res = mgr.push_front(pointer, s = 256 - atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_NE(NULL, pointer);
    memset(pointer, 0, s);

    res = mgr.push_front(pointer, s = 256 - atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_NE(NULL, pointer);
    memset(pointer, -1, s);

    res = mgr.push_front(pointer, s= 256 - atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_NE(NULL, pointer);
    memset(pointer, 0, s);

    res = mgr.push_front(pointer, 256 + 2 * atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);
    CASE_EXPECT_EQ(NULL, pointer);
    res = mgr.push_front(pointer, 257 - atbus::detail::buffer_block::head_size(257));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);
    CASE_EXPECT_EQ(NULL, pointer);
    res = mgr.push_front(pointer, s = 255 - atbus::detail::buffer_block::head_size(255) - atbus::detail::buffer_block::padding_size(1));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_NE(NULL, pointer);
    memset(pointer, -1, s);

    CASE_EXPECT_EQ(1023 - atbus::detail::buffer_block::padding_size(1), mgr.limit().cost_size_ + atbus::detail::buffer_block::head_size(255) + 3 * atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(4, mgr.limit().cost_number_);

    mgr.pop_back(256, false);
    CASE_EXPECT_EQ(4, mgr.limit().cost_number_);
    CHECK_BUFFER(mgr.back()->raw_data(), mgr.back()->raw_size(), 0x00);

    mgr.pop_back(0);
    mgr.back(check_ptr[0], s, sr);
    CASE_EXPECT_EQ(3, mgr.limit().cost_number_);
    CHECK_BUFFER(mgr.back()->raw_data(), mgr.back()->raw_size(), 0xFF);

    res = mgr.push_front(pointer, 256);
    CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);


    mgr.pop_back(255 - atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(3, mgr.limit().cost_number_);

    res = mgr.push_front(check_ptr[2], 128);
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_EQ(4, mgr.limit().cost_number_);



    res = mgr.push_front(check_ptr[3], 128);
    CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);

    mgr.pop_back(1);
    CASE_EXPECT_EQ(3, mgr.limit().cost_number_);
    CHECK_BUFFER(mgr.back()->raw_data(), mgr.back()->raw_size(), 0x00);

    res = mgr.push_front(check_ptr[3], 384 - atbus::detail::buffer_block::head_size(100) - atbus::detail::buffer_block::head_size(412) - atbus::detail::buffer_block::padding_size(1));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_EQ(4, mgr.limit().cost_number_);


    mgr.back(check_ptr[1], s, sr);
    CASE_EXPECT_TRUE(check_ptr[2] > check_ptr[0]);
    CASE_EXPECT_TRUE(check_ptr[3] >= check_ptr[0]);
    CASE_EXPECT_TRUE(check_ptr[3] < check_ptr[2]);
    CASE_EXPECT_TRUE(check_ptr[1] < check_ptr[0]);
}
Exemplo n.º 13
0
CASE_TEST(buffer, static_buffer_manager_fb)
{
    atbus::detail::buffer_manager mgr;
    CASE_EXPECT_TRUE(mgr.empty());

    mgr.set_mode(1023, 10);
    CASE_EXPECT_FALSE(mgr.set_limit(2048, 10));

    CASE_EXPECT_EQ(1023, mgr.limit().limit_size_);
    CASE_EXPECT_EQ(10, mgr.limit().limit_number_);

    // size limit
    void* pointer;
    void* check_ptr[4];
    int res = mgr.push_front(pointer, 256 - atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_NE(NULL, pointer);
    res = mgr.push_front(pointer, 256 - atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_NE(NULL, pointer);
    res = mgr.push_front(pointer, 256 - atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_NE(NULL, pointer);
    res = mgr.push_front(pointer, 256 + 2 * atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);
    CASE_EXPECT_EQ(NULL, pointer);
    res = mgr.push_front(pointer, 257 - atbus::detail::buffer_block::head_size(257));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);
    CASE_EXPECT_EQ(NULL, pointer);
    res = mgr.push_front(pointer, 255 - atbus::detail::buffer_block::head_size(255) - atbus::detail::buffer_block::padding_size(1));
    CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
    CASE_EXPECT_NE(NULL, pointer);

    CASE_EXPECT_EQ(1023 - atbus::detail::buffer_block::padding_size(1), mgr.limit().cost_size_ + atbus::detail::buffer_block::head_size(255) + 3 * atbus::detail::buffer_block::head_size(256));
    CASE_EXPECT_EQ(4, mgr.limit().cost_number_);

    mgr.reset();
    mgr.set_mode(1023, 3);
    // from empty to full to empty
    // should has the same result
    for (int i = 0; i < 3; ++ i) {
        // number limit
        res = mgr.push_front(check_ptr[0], 99);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        memset(check_ptr[0], -1, 99);

        res = mgr.push_front(check_ptr[1], 28);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        memset(check_ptr[1], 0, 28);

        res = mgr.push_front(check_ptr[2], 17);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        memset(check_ptr[2], -1, 17);

        res = mgr.push_front(check_ptr[3], 63);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);

        CASE_EXPECT_EQ(144, mgr.limit().cost_size_);
        CASE_EXPECT_EQ(3, mgr.limit().cost_number_);

        // pop and remove block
        size_t s, sr;
        res = mgr.back(pointer, sr, s);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        CASE_EXPECT_EQ(pointer, check_ptr[0]);
        CASE_EXPECT_EQ(99, s);
        CASE_EXPECT_EQ(*reinterpret_cast<char*>(pointer), -1);

        res = mgr.pop_back(128);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);

        CASE_EXPECT_EQ(45, mgr.limit().cost_size_);
        CASE_EXPECT_EQ(2, mgr.limit().cost_number_);

        res = mgr.back(pointer, sr, s);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        CASE_EXPECT_EQ(pointer, check_ptr[1]);
        CASE_EXPECT_EQ(28, s);
        CASE_EXPECT_EQ(*reinterpret_cast<char*>(pointer), 0);

        res = mgr.pop_back(100);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);

        CASE_EXPECT_EQ(17, mgr.limit().cost_size_);
        CASE_EXPECT_EQ(1, mgr.limit().cost_number_);

        res = mgr.back(pointer, sr, s);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        CASE_EXPECT_EQ(pointer, check_ptr[2]);
        CASE_EXPECT_EQ(17, s);
        CASE_EXPECT_EQ(*reinterpret_cast<char*>(pointer), -1);

        // pop but not remove block
        mgr.pop_back(10);
        CASE_EXPECT_EQ(7, mgr.limit().cost_size_);
        CASE_EXPECT_EQ(1, mgr.limit().cost_number_);

        res = mgr.back(pointer, sr, s);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        CASE_EXPECT_EQ(pointer, atbus::detail::fn::buffer_next(check_ptr[2], 10));
        CASE_EXPECT_EQ(7, s);
        CASE_EXPECT_EQ(*reinterpret_cast<char*>(pointer), -1);

        // pop all
        res = mgr.pop_back(10);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        CASE_EXPECT_TRUE(mgr.empty());
        CASE_EXPECT_EQ(0, mgr.limit().cost_size_);
        CASE_EXPECT_EQ(0, mgr.limit().cost_number_);

        // pop nothing
        res = mgr.back(pointer, sr, s);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_NO_DATA, res);
        CASE_EXPECT_EQ(NULL, pointer);
        CASE_EXPECT_EQ(0, s);

        res = mgr.pop_back(10);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_NO_DATA, res);
    }
}