CASE_TEST(this_context, get_coroutine) {
    typedef copp::coroutine_context_default co_type;

    std::vector<std::thread> th_pool;

    co_type co_arr[5];
    for(co_type& co: co_arr) {
        co.create(new test_this_context_get_cotoutine_runner(), 32 * 1024);

        th_pool.push_back(std::thread(std::bind(
            test_this_context_thread_func,
            std::ref(co)
        )));
    }

    for(std::thread& th: th_pool) {
        th.join();
    }

    CASE_EXPECT_LT(1, test_this_context_get_cotoutine_runner::get_max_thd_count());

    for(co_type& co: co_arr) {
        assert(co.get_runner());
        delete dynamic_cast<test_this_context_get_cotoutine_runner*>(co.get_runner());
    }
}
Beispiel #2
0
CASE_TEST(buffer, static_buffer_manager_merge_front)
{
    // merge front : NN tail ... head NN ... => NN tail ... head NN NN ...
    {
        atbus::detail::buffer_manager mgr;
        CASE_EXPECT_TRUE(mgr.empty());

        mgr.set_mode(255, 3);

        CASE_EXPECT_EQ(255, mgr.limit().limit_size_);
        CASE_EXPECT_EQ(3, mgr.limit().limit_number_);

        // init
        void* pointer;
        void* check_ptr[3] = { &pointer, &pointer, &pointer };
        size_t s, sr;
        int res = mgr.push_front(pointer, s = 128 - atbus::detail::buffer_block::head_size(128));
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        CASE_EXPECT_NE(NULL, pointer);
        memset(pointer, 0xec, s);
        CASE_EXPECT_EQ(1, mgr.limit().cost_number_);

        res = mgr.merge_front(check_ptr[0], 125);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);
        CASE_EXPECT_EQ(s, mgr.front()->raw_size());
        CASE_EXPECT_EQ(NULL, check_ptr[0]);
        CASE_EXPECT_EQ(1, mgr.limit().cost_number_);

        res = mgr.merge_front(check_ptr[1], 0);
        CASE_EXPECT_EQ(0, res);
        CASE_EXPECT_EQ(NULL, check_ptr[1]);
        CASE_EXPECT_EQ(1, mgr.limit().cost_number_);
        CASE_EXPECT_EQ(pointer, mgr.front()->raw_data());

        res = mgr.merge_front(check_ptr[2], sr = 120);
        CASE_EXPECT_EQ(0, res);
        CASE_EXPECT_EQ(s + sr, mgr.front()->raw_size());
        CASE_EXPECT_NE(pointer, mgr.front()->raw_data());
        CASE_EXPECT_EQ(check_ptr[2], atbus::detail::fn::buffer_next(mgr.front()->raw_data(), s));
        CASE_EXPECT_EQ(1, mgr.limit().cost_number_);

        CASE_EXPECT_EQ(pointer, atbus::detail::fn::buffer_next(mgr.front()->raw_data(), 120));
        CHECK_BUFFER(mgr.front()->raw_data(), s, 0xec);
    }

    // merge front : .... head NNNNNN tail .... => .... head NN NNNNNN tail ....
    {
        atbus::detail::buffer_manager mgr;
        CASE_EXPECT_TRUE(mgr.empty());

        mgr.set_mode(255, 3);

        CASE_EXPECT_EQ(255, mgr.limit().limit_size_);
        CASE_EXPECT_EQ(3, mgr.limit().limit_number_);

        // init
        void* pointer;
        void* check_ptr[2] = { &pointer, &pointer };
        size_t s, sr;
        mgr.push_back(pointer, s = 136 - atbus::detail::buffer_block::head_size(136));
        int res = mgr.push_back(pointer, sr = 64 - atbus::detail::buffer_block::head_size(64));
        mgr.pop_front(s);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        CASE_EXPECT_NE(NULL, pointer);

        memset(pointer, 0xeb, sr);
        CASE_EXPECT_EQ(1, mgr.limit().cost_number_);

        res = mgr.merge_front(check_ptr[0], 137);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);
        CASE_EXPECT_EQ(sr, mgr.front()->raw_size());
        CASE_EXPECT_EQ(NULL, check_ptr[0]);
        CASE_EXPECT_EQ(1, mgr.limit().cost_number_);

        res = mgr.merge_front(check_ptr[1], 136);
        CASE_EXPECT_EQ(0, res);
        CASE_EXPECT_EQ(136 + sr, mgr.front()->raw_size());
        CASE_EXPECT_NE(pointer, mgr.front()->raw_data());
        CASE_EXPECT_EQ(check_ptr[1], atbus::detail::fn::buffer_next(mgr.front()->raw_data(), sr));
        CASE_EXPECT_EQ(1, mgr.limit().cost_number_);

        CASE_EXPECT_EQ(pointer, atbus::detail::fn::buffer_next(mgr.front()->raw_data(), 136));
        CHECK_BUFFER(mgr.front()->raw_data(), sr, 0xeb);
    }

    // merge back : ... head NNNNNN tail ... => ... NNNNNN tail ... new_head NNN
    {
        atbus::detail::buffer_manager mgr;
        CASE_EXPECT_TRUE(mgr.empty());

        mgr.set_mode(255, 3);

        CASE_EXPECT_EQ(255, mgr.limit().limit_size_);
        CASE_EXPECT_EQ(3, mgr.limit().limit_number_);

        // init
        void* pointer;
        void* check_ptr[2] = { &pointer, &pointer };
        size_t s, sr;
        mgr.push_back(pointer, s = 64 - atbus::detail::buffer_block::head_size(64));
        mgr.push_back(pointer, 32 - atbus::detail::buffer_block::head_size(32));
        int res = mgr.push_back(pointer, sr = 32 - atbus::detail::buffer_block::head_size(32));
        mgr.pop_front(s);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_SUCCESS, res);
        pointer = mgr.front()->data();
        CASE_EXPECT_NE(NULL, pointer);

        memset(pointer, 0xea, sr);
        CASE_EXPECT_EQ(2, mgr.limit().cost_number_);

        res = mgr.merge_front(check_ptr[0], 96);
        CASE_EXPECT_EQ(EN_ATBUS_ERR_BUFF_LIMIT, res);
        CASE_EXPECT_EQ(sr, mgr.front()->raw_size());
        CASE_EXPECT_EQ(NULL, check_ptr[0]);
        CASE_EXPECT_EQ(2, mgr.limit().cost_number_);

        res = mgr.merge_front(check_ptr[1], 65);
        CASE_EXPECT_EQ(0, res);
        CASE_EXPECT_EQ(65 + sr, mgr.front()->raw_size());
        CASE_EXPECT_NE(pointer, mgr.front()->raw_data());
        CASE_EXPECT_EQ(check_ptr[1], atbus::detail::fn::buffer_next(mgr.front()->raw_data(), sr));
        CASE_EXPECT_EQ(2, mgr.limit().cost_number_);

        CASE_EXPECT_LT(pointer, mgr.front()->raw_data());
        CHECK_BUFFER(mgr.front()->raw_data(), sr, 0xea);
    }
}