/*************************************************************************** * * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved * $测试phashmap在多线程环境下的安全性问题$ * **************************************************************************/ /** * @file bsl_test_phashmap.h * @author xiaowei(com@baidu.com) * @date 2008/08/18 12:37:07 * @version $Revision: 1.1 $ * @brief * **/ #ifndef __BSL_TEST_PHASHMAP_H_ #define __BSL_TEST_PHASHMAP_H_ #include static const int DATANUM = 100000; ///////////// /** * 测试多线程的读写安全情况 * 这个case测试两个hash表, * 一个是phash,一个是rwhash */ template void * __test_phashmap_fun_query(void *param) { auto_timer t("query time at one thread"); HASH * test = (HASH *)param; __XASSERT2(test!=NULL); int qloop = DATANUM / 20; for (int i=0; iget(str, &val); if (ret == bsl::HASH_EXIST) { __XASSERT2_R(str == val); } } return NULL; } template void * __test_phashmap_fun_update(void *param) { //auto_timer t("update time at one thread"); HASH *test = (HASH *)param; __XASSERT2(test!=NULL); int uloop = DATANUM; for (int i=0; iset(s1, s1) != -1); test->erase(randstr()); } return NULL; } template bool test_phashmap() { return test_phashmap_temp(20, __test_phashmap_fun_query, 1, __test_phashmap_fun_update); } //测试在写情况下dump数据问题 template void * __test_cp_fun1(void *param) { HASH *test = (HASH *)param; __XASSERT2(test != NULL); int loop = 1000; for (int i=0; imake_checkpoint(); } return NULL; } template void * __test_cp_fun2(void *param) { HASH *test = (HASH *)param; __XASSERT2(test != NULL); int loop = 1000; for (int i=0; iend_checkpoint(); } return NULL; } template bool test_phashmap_cp1() { return test_phashmap_temp(10, __test_cp_fun1, 10, __test_cp_fun2); } //测试在写情况下dump数据问题 template void * __test_cp_fun3(void *param) { HASH *test = (HASH *)param; __XASSERT2_R(test != NULL); int num = DATANUM; for (int i=0; iset(int2str(i), int2str(i)) == bsl::HASH_INSERT_SEC); } { auto_timer t("make checkpoint"); __XASSERT2_R(test->make_checkpoint() == 0); } for (int i=0; iset(int2str(i), int2str(num/2 - i), 1) == bsl::HASH_OVERWRITE); } #if 0 //check for (int i=0; iget(int2str(i), &val) == bsl::HASH_EXIST); __XASSERT2_R(val == int2str(num/2 - i)); } __BSL_ERROR("start to erase"); #endif for (int i=0; ierase(int2str(i)) == bsl::HASH_EXIST, "erase %d %d", i, test->dumpstatus()); } for (int i=0; iset(int2str(i), int2str(num - i)) == bsl::HASH_INSERT_SEC); } return NULL; } template void * __test_cp_fun4(void *param) { HASH *test = (HASH *)param; __XASSERT2_R(test != NULL); while (test->dumpstatus() == bsl::PHASH_NORMAL) { struct timespec tv; tv.tv_sec = 0; tv.tv_nsec = 10 * 1000; nanosleep(&tv, NULL); } bsl::filestream fd; __XASSERT2_R(fd.open("phash.dat.1", "w") == 0); bsl::binarchive ar(fd); __XASSERT2_R(ar.write(*test) == 0); fd.close(); __XASSERT2_R(fd.open("phash.dat.1", "r") == 0); bsl::binarchive ar2(fd); HASH test2; __XASSERT2_R(ar.read(&test2) == 0); fd.close(); int num = DATANUM; __BSL_DEBUG("---------start to check size %d", (int)test2.size()); for (int i=0; i void * __test_cp_fun5(void *param) { HASH *test = (HASH *)param; __XASSERT2_R(test != NULL); int loop = DATANUM / 20; for (int i=0; iget(int2str(i)); } return NULL; } template bool test_phashmap_cp2() { return test_phashmap_temp(1, __test_cp_fun3, 1, __test_cp_fun4, 20, __test_cp_fun5); } class bsl_test_phashmap : public CxxTest::TestSuite { public: typedef bsl::phashmap type1_t; void test_phashmap_(void) { TSM_ASSERT ("", test_phashmap()); } void test_phashmap_cp1_() { TSM_ASSERT ("", test_phashmap_cp1()); } void test_phashmap_cp2_() { TSM_ASSERT ("", test_phashmap_cp2()); } typedef bsl::phashmap, bsl::bsl_alloc > type2_t; void test_phashmap_2(void) { TSM_ASSERT ("", test_phashmap()); } void test_phashmap_cp1_2() { TSM_ASSERT ("", test_phashmap_cp1()); } void test_phashmap_cp2_2() { TSM_ASSERT ("", test_phashmap_cp2()); } /// typedef bsl::phashmap, bsl::bsl_cpsalloc > > type3_t; void test_phashmap_3(void) { TSM_ASSERT ("", test_phashmap()); } void test_phashmap_cp1_3() { TSM_ASSERT ("", test_phashmap_cp1()); } void test_phashmap_cp2_3() { TSM_ASSERT ("", test_phashmap_cp2()); } }; #endif //__BSL_TEST_PHASHMAP_H_ /* vim: set ts=4 sw=4 sts=4 tw=100 noet: */