patch 4.0
This commit is contained in:
119
deps/oblib/unittest/lib/list/test_atomic_list.cpp
vendored
Normal file
119
deps/oblib/unittest/lib/list/test_atomic_list.cpp
vendored
Normal file
@ -0,0 +1,119 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OceanBase
|
||||
* OceanBase CE is licensed under Mulan PubL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PubL v2.
|
||||
* You may obtain a copy of Mulan PubL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPubL-2.0
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PubL v2 for more details.
|
||||
*/
|
||||
|
||||
#include <pthread.h>
|
||||
#include "lib/allocator/ob_malloc.h"
|
||||
#include "lib/list/ob_atomic_list.h"
|
||||
|
||||
using namespace oceanbase;
|
||||
using namespace common;
|
||||
|
||||
ObAtomicList atomic_list;
|
||||
|
||||
void test_simple()
|
||||
{
|
||||
char mem[32];
|
||||
char *next = mem + 16;
|
||||
|
||||
atomic_list.init("test", 0);
|
||||
OB_ASSERT(atomic_list.empty());
|
||||
OB_ASSERT(NULL == atomic_list.head());
|
||||
OB_ASSERT(TO_PTR(atomic_list.head()) == atomic_list.push(mem));
|
||||
OB_ASSERT(!atomic_list.empty());
|
||||
OB_ASSERT(mem == atomic_list.head());
|
||||
OB_ASSERT(mem == atomic_list.pop());
|
||||
OB_ASSERT(atomic_list.empty());
|
||||
|
||||
OB_ASSERT(TO_PTR(atomic_list.head()) == atomic_list.push(mem));
|
||||
OB_ASSERT(TO_PTR(atomic_list.head()) == atomic_list.push(next));
|
||||
OB_ASSERT(!atomic_list.empty());
|
||||
OB_ASSERT(next == atomic_list.head());
|
||||
OB_ASSERT(mem == atomic_list.next(next));
|
||||
OB_ASSERT(next == atomic_list.pop());
|
||||
OB_ASSERT(mem == atomic_list.pop());
|
||||
}
|
||||
|
||||
void *thread_func(void *arg)
|
||||
{
|
||||
char *mem = (char *)ob_malloc(64 * 1024, 0);
|
||||
OB_ASSERT(NULL != mem);
|
||||
int64_t index = *(int64_t *)arg;
|
||||
|
||||
void *freelist = NULL;
|
||||
void *ptr = NULL;
|
||||
int64_t i = 0;
|
||||
|
||||
for (i = 0; i < 64 * 1024 / 16; i++) {
|
||||
atomic_list.push(mem + i * 16);
|
||||
}
|
||||
|
||||
for (int64_t j = 0; j < 100000; j++) {
|
||||
for (i = 0; i < 100; i++) {
|
||||
ptr = atomic_list.pop();
|
||||
OB_ASSERT(NULL != ptr);
|
||||
*(reinterpret_cast<void**>(ptr)) = freelist;
|
||||
freelist = ptr;
|
||||
}
|
||||
|
||||
for (i = 0; i < 100; i++) {
|
||||
ptr = freelist;
|
||||
freelist = *(void **)ptr;
|
||||
OB_ASSERT(NULL != atomic_list.push(ptr));
|
||||
}
|
||||
if (0 == (j + 1) % 100000 && j > 0) {
|
||||
printf("thread %ld pop %ld times and push %ld times\n",
|
||||
index, 1000000L, 1000000L);
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void thread_run()
|
||||
{
|
||||
pthread_t id[15];
|
||||
int64_t index[15];
|
||||
|
||||
printf("start multi-thread concurrency test\n");
|
||||
for (int64_t i = 0; i < 15; i++) {
|
||||
index[i] = i;
|
||||
if (0 != pthread_create(&id[i], NULL, thread_func, &index[i])) {
|
||||
printf("create thread error\n");
|
||||
}
|
||||
}
|
||||
|
||||
void *ret = NULL;
|
||||
for (int64_t i = 0; i < 15; i++) {
|
||||
pthread_join(id[i], &ret);
|
||||
}
|
||||
|
||||
int64_t count = 0;
|
||||
for (int64_t i = 0; i < 15 * 64 * 1024 / 16; i++) {
|
||||
OB_ASSERT(NULL != atomic_list.pop());
|
||||
count++;
|
||||
}
|
||||
OB_ASSERT(count == 15 * 64 * 1024 / 16);
|
||||
|
||||
printf("end test\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
UNUSED(argc);
|
||||
UNUSED(argv);
|
||||
|
||||
test_simple();
|
||||
|
||||
thread_run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
38
deps/oblib/unittest/lib/list/test_dlist.cpp
vendored
38
deps/oblib/unittest/lib/list/test_dlist.cpp
vendored
@ -19,25 +19,26 @@
|
||||
#include "lib/allocator/page_arena.h"
|
||||
|
||||
using ::testing::_;
|
||||
using ::testing::Invoke;
|
||||
using ::testing::Return;
|
||||
using ::testing::SetArgReferee;
|
||||
using ::testing::Invoke;
|
||||
|
||||
namespace oceanbase {
|
||||
namespace common {
|
||||
namespace oceanbase
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
|
||||
class TestObDList : public ::testing::Test {
|
||||
class TestObDList : public ::testing::Test
|
||||
{
|
||||
public:
|
||||
TestObDList()
|
||||
{}
|
||||
TestObDList() {}
|
||||
|
||||
virtual void SetUp()
|
||||
{}
|
||||
virtual void TearDown()
|
||||
{}
|
||||
virtual void SetUp() {}
|
||||
virtual void TearDown(){}
|
||||
};
|
||||
|
||||
struct TestNode : public common::ObDLinkBase<TestNode> {
|
||||
struct TestNode: public common::ObDLinkBase<TestNode>
|
||||
{
|
||||
int64_t value_;
|
||||
OB_UNIS_VERSION(1);
|
||||
};
|
||||
@ -56,30 +57,33 @@ TEST_F(TestObDList, encode_decode)
|
||||
ASSERT_TRUE(list.add_last(&node2));
|
||||
|
||||
int64_t buf_size = get_dlist_serialize_size(list);
|
||||
char* buf = static_cast<char*>(ob_malloc(buf_size));
|
||||
char *buf = static_cast<char*>(ob_malloc(buf_size));
|
||||
int64_t buf_len = buf_size;
|
||||
int64_t pos = 0;
|
||||
ASSERT_EQ(OB_SUCCESS, serialize_dlist(list, buf, buf_len, pos));
|
||||
ASSERT_EQ(buf_size, pos);
|
||||
LOG_INFO("print", K(buf_size));
|
||||
|
||||
|
||||
common::ObArenaAllocator allocator;
|
||||
common::ObDList<TestNode> new_list;
|
||||
pos = 0;
|
||||
ASSERT_EQ(OB_SUCCESS, deserialize_dlist(new_list, allocator, buf, buf_len, pos));
|
||||
|
||||
TestNode* first_node = new_list.get_first();
|
||||
TestNode* second_node = first_node->get_next();
|
||||
TestNode *first_node = new_list.get_first();
|
||||
TestNode *second_node = first_node->get_next();
|
||||
ASSERT_TRUE(NULL != first_node);
|
||||
ASSERT_TRUE(NULL != second_node);
|
||||
ASSERT_EQ(1, first_node->value_);
|
||||
ASSERT_EQ(2, second_node->value_);
|
||||
}
|
||||
|
||||
} // namespace common
|
||||
} // end namespace oceanbase
|
||||
|
||||
int main(int argc, char** argv)
|
||||
|
||||
} // end namespace share
|
||||
} // end namespace oceanbase
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
oceanbase::common::ObLogger::get_logger().set_log_level("INFO");
|
||||
testing::InitGoogleTest(&argc, argv);
|
||||
|
||||
Reference in New Issue
Block a user