17 #include "my_config.h"
18 #include <gtest/gtest.h>
19 #include "test_utils.h"
21 #include "table_cache.h"
31 namespace table_cache_unittest {
48 static const uint MAX_THREADS= 3;
52 for (uint
i= 0;
i < MAX_THREADS; ++
i)
54 initializer[
i].SetUp();
55 initializer[
i].thd()->thread_id=
i + 1;
58 ::testing::FLAGS_gtest_death_test_style =
"threadsafe";
60 virtual void TearDown()
62 for (uint
i= 0;
i < MAX_THREADS; ++
i)
63 initializer[
i].TearDown();
66 THD *get_thd(uint
index) {
return initializer[
index].thd(); }
80 virtual uint CachesNumber() {
return 1; }
83 TableCacheBasicTest::SetUp();
91 table_cache_instances= CachesNumber();
92 table_cache_size_per_instance= 100;
93 ASSERT_FALSE(table_def_init());
95 virtual void TearDown()
98 TableCacheBasicTest::TearDown();
111 virtual uint CachesNumber() {
return 2; }
133 table_cache_key.str= (
char*)key;
134 table_cache_key.length= strlen(key);
135 memset(cache_element_arr, 0,
sizeof(cache_element_arr));
138 init_alloc_root(&m_mem_root, 1024, 0);
143 version= refresh_version;
150 free_root(&m_mem_root, MYF(0));
153 TABLE *create_table(THD *thd)
157 memset(result, 0,
sizeof(
TABLE));
166 result->file=
new (&m_mem_root)
ha_example(example_hton,
this);
167 result->db_stat= HA_READ_ONLY;
190 ASSERT_FALSE(table_cache.
init());
198 ".*Assertion.*count > 0.*pthread_equal.*");
208 TEST_F(TableCacheBasicDeathTest, CacheLockAndUnlock)
212 ASSERT_FALSE(table_cache.
init());
217 ".*Assertion.*count > 0.*pthread_equal.*");
228 ".*Assertion.*count > 0.*pthread_equal.*");
243 TEST_F(TableCacheBasicDeathTest, ManagerCreateAndDestroy)
246 table_cache_instances= 2;
248 ASSERT_FALSE(table_cache_manager.
init());
255 cache_1= table_cache_manager.
get_cache(get_thd(0));
256 cache_2= table_cache_manager.
get_cache(get_thd(1));
257 cache_3= table_cache_manager.
get_cache(get_thd(2));
258 EXPECT_TRUE(cache_1 != cache_2);
260 EXPECT_TRUE(cache_3 == cache_1);
263 EXPECT_EQ(0
U, cache_1->cached_tables());
264 EXPECT_EQ(0
U, cache_2->cached_tables());
268 EXPECT_DEATH_IF_SUPPORTED(cache_1->assert_owner(),
269 ".*Assertion.*count > 0.*pthread_equal.*");
270 EXPECT_DEATH_IF_SUPPORTED(cache_2->assert_owner(),
271 ".*Assertion.*count > 0.*pthread_equal.*");
282 TEST_F(TableCacheSingleCacheTest, CacheAddAndRemove)
284 THD *thd= get_thd(0);
286 Mock_share share_1(
"share_1");
287 TABLE *table_1= share_1.create_table(thd);
298 my_hash_value_type hash_value= my_calc_hash(&table_def_cache,
299 (uchar*)share_1.table_cache_key.str,
300 share_1.table_cache_key.length);
303 table_2= table_cache->
get_table(thd, hash_value,
304 share_1.table_cache_key.str,
305 share_1.table_cache_key.length,
307 EXPECT_TRUE(table_2 == NULL);
308 EXPECT_TRUE(share_2 == &share_1);
313 EXPECT_TRUE(it++ == table_1);
314 EXPECT_TRUE(it++ == NULL);
319 table_2= table_cache->
get_table(thd, hash_value,
320 share_1.table_cache_key.str,
321 share_1.table_cache_key.length,
323 EXPECT_TRUE(table_2 == table_1);
324 EXPECT_TRUE(share_2 == &share_1);
331 table_2= table_cache->
get_table(thd, hash_value,
332 share_1.table_cache_key.str,
333 share_1.table_cache_key.length,
335 EXPECT_TRUE(table_2 == NULL);
336 EXPECT_TRUE(share_2 == NULL);
339 EXPECT_TRUE(it++ == NULL);
351 table_2= table_cache->
get_table(thd, hash_value,
352 share_1.table_cache_key.str,
353 share_1.table_cache_key.length,
355 EXPECT_TRUE(table_2 == NULL);
356 EXPECT_TRUE(share_2 == NULL);
360 share_1.destroy_table(table_1);
368 TEST_F(TableCacheSingleCacheTest, CacheOverflow)
370 THD *thd= get_thd(0);
373 table_cache_size_per_instance= 2;
375 Mock_share share_1(
"share_1");
376 Mock_share share_2(
"share_2");
377 TABLE *table_1= share_1.create_table(thd);
378 TABLE *table_2= share_1.create_table(thd);
379 TABLE *table_3= share_2.create_table(thd);
402 table_1= share_1.create_table(thd);
409 table_2= share_1.create_table(thd);
431 share_1.destroy_table(table_1);
432 share_1.destroy_table(table_3);
436 TEST_F(TableCacheSingleCacheTest, CacheGetAndRelease)
438 THD *thd= get_thd(0);
444 TABLE *table_1, *table_2, *table_3;
445 Mock_share share_1(
"share_1"), share_0(
"share_0");
449 my_hash_value_type hash_value_1= my_calc_hash(&table_def_cache,
450 (uchar*)share_1.table_cache_key.str,
451 share_1.table_cache_key.length);
452 table_1= table_cache->
get_table(thd, hash_value_1,
453 share_1.table_cache_key.str,
454 share_1.table_cache_key.length,
456 EXPECT_TRUE(table_1 == NULL);
457 EXPECT_TRUE(share_2 == NULL);
459 table_1= share_1.create_table(thd);
464 table_2= table_cache->
get_table(thd, hash_value_1,
465 share_1.table_cache_key.str,
466 share_1.table_cache_key.length,
468 EXPECT_TRUE(table_2 == NULL);
469 EXPECT_TRUE(share_2 == &share_1);
473 my_hash_value_type hash_value_0= my_calc_hash(&table_def_cache,
474 (uchar*)share_0.table_cache_key.str,
475 share_0.table_cache_key.length);
476 table_2= table_cache->
get_table(thd, hash_value_0,
477 share_0.table_cache_key.str,
478 share_0.table_cache_key.length,
480 EXPECT_TRUE(table_2 == NULL);
481 EXPECT_TRUE(share_2 == NULL);
483 table_2= share_1.create_table(thd);
488 table_3= table_cache->
get_table(thd, hash_value_1,
489 share_1.table_cache_key.str,
490 share_1.table_cache_key.length,
492 EXPECT_TRUE(table_3 == NULL);
493 EXPECT_TRUE(share_2 == &share_1);
499 table_3= table_cache->
get_table(thd, hash_value_1,
500 share_1.table_cache_key.str,
501 share_1.table_cache_key.length,
503 EXPECT_TRUE(table_3 == table_1);
504 EXPECT_TRUE(share_2 == &share_1);
507 table_3= table_cache->
get_table(thd, hash_value_1,
508 share_1.table_cache_key.str,
509 share_1.table_cache_key.length,
511 EXPECT_TRUE(table_3 == NULL);
512 EXPECT_TRUE(share_2 == &share_1);
520 table_3= table_cache->
get_table(thd, hash_value_0,
521 share_0.table_cache_key.str,
522 share_0.table_cache_key.length,
524 EXPECT_TRUE(table_3 == NULL);
525 EXPECT_TRUE(share_2 == NULL);
528 table_3= table_cache->
get_table(thd, hash_value_1,
529 share_1.table_cache_key.str,
530 share_1.table_cache_key.length,
532 EXPECT_TRUE(table_3 != NULL);
533 EXPECT_TRUE(share_2 == &share_1);
534 table_3= table_cache->
get_table(thd, hash_value_1,
535 share_1.table_cache_key.str,
536 share_1.table_cache_key.length,
538 EXPECT_TRUE(table_3 != NULL);
539 EXPECT_TRUE(share_2 == &share_1);
540 table_3= table_cache->
get_table(thd, hash_value_1,
541 share_1.table_cache_key.str,
542 share_1.table_cache_key.length,
544 EXPECT_TRUE(table_3 == NULL);
545 EXPECT_TRUE(share_2 == &share_1);
551 share_1.destroy_table(table_1);
552 share_1.destroy_table(table_2);
562 TEST_F(TableCacheDoubleCacheTest, ManagerFreeAllUnused)
564 THD *thd_1= get_thd(0);
565 THD *thd_2= get_thd(1);
573 Mock_share share_1(
"share_1");
574 Mock_share share_2(
"share_2");
575 Mock_share share_3(
"share_2");
576 TABLE *table_1= share_1.create_table(thd_1);
577 TABLE *table_2= share_1.create_table(thd_1);
578 TABLE *table_3= share_2.create_table(thd_1);
579 TABLE *table_4= share_2.create_table(thd_1);
580 TABLE *table_5= share_1.create_table(thd_2);
581 TABLE *table_6= share_3.create_table(thd_2);
650 TEST_F(TableCacheDoubleCacheTest, ManagerCachedTables)
652 THD *thd_1= get_thd(0);
653 THD *thd_2= get_thd(1);
663 Mock_share share_1(
"share_1");
664 Mock_share share_2(
"share_2");
665 TABLE *table_1= share_1.create_table(thd_1);
666 TABLE *table_2= share_1.create_table(thd_1);
667 TABLE *table_3= share_2.create_table(thd_1);
668 TABLE *table_4= share_1.create_table(thd_2);
669 TABLE *table_5= share_2.create_table(thd_2);
723 share_1.destroy_table(table_1);
724 share_1.destroy_table(table_2);
725 share_2.destroy_table(table_3);
726 share_1.destroy_table(table_4);
727 share_2.destroy_table(table_5);
735 TEST_F(TableCacheDoubleCacheDeathTest, ManagerLockAndUnlock)
740 ".*Assertion.*count > 0.*pthread_equal.*");
742 ".*Assertion.*count > 0.*pthread_equal.*");
764 ".*Assertion.*count > 0.*pthread_equal.*");
766 ".*Assertion.*count > 0.*pthread_equal.*");
775 TEST_F(TableCacheDoubleCacheDeathTest, ManagerFreeTable)
777 THD *thd_1= get_thd(0);
778 THD *thd_2= get_thd(1);
783 Mock_share share_1(
"share_1");
784 Mock_share share_2(
"share_2");
785 TABLE *table_1= share_1.create_table(thd_1);
786 TABLE *table_2= share_1.create_table(thd_1);
787 TABLE *table_3= share_2.create_table(thd_1);
788 TABLE *table_4= share_1.create_table(thd_2);
789 TABLE *table_5= share_2.create_table(thd_2);
809 EXPECT_DEATH_IF_SUPPORTED(table_cache_manager.
free_table(thd_1,
812 ".*Assertion.*is_empty.*");
820 table_cache_manager.
free_table(thd_1, TDC_RT_REMOVE_ALL, &share_1);
828 table_1= share_1.create_table(thd_1);
829 table_2= share_1.create_table(thd_1);
830 table_4= share_1.create_table(thd_2);
843 EXPECT_DEATH_IF_SUPPORTED(table_cache_manager.
free_table(thd_1,
844 TDC_RT_REMOVE_NOT_OWN,
853 table_cache_manager.
free_table(thd_1, TDC_RT_REMOVE_NOT_OWN, &share_1);
862 table_2= share_1.create_table(thd_1);
863 table_4= share_1.create_table(thd_2);
871 table_cache_manager.
free_table(thd_1, TDC_RT_REMOVE_UNUSED, &share_1);
880 table_cache_manager.
free_table(thd_1, TDC_RT_REMOVE_UNUSED, &share_1);
890 share_2.destroy_table(table_3);
891 share_2.destroy_table(table_5);
901 TEST_F(TableCacheDoubleCacheTest, Iterator)
903 THD *thd_1= get_thd(0);
904 THD *thd_2= get_thd(1);
908 Mock_share share_1(
"share_1");
909 Mock_share share_2(
"share_2");
914 EXPECT_TRUE(it++ == NULL);
916 EXPECT_TRUE(it++ == NULL);
920 TABLE *table_1= share_1.create_table(thd_1);
921 TABLE *table_2= share_1.create_table(thd_1);
922 TABLE *table_3= share_2.create_table(thd_1);
923 TABLE *table_4= share_1.create_table(thd_2);
924 TABLE *table_5= share_2.create_table(thd_2);
930 TABLE *table_r1= it++;
931 EXPECT_TRUE(table_r1 == table_4);
933 EXPECT_TRUE(it++ == NULL);
934 EXPECT_TRUE(it++ == NULL);
941 EXPECT_TRUE(table_r1 != NULL);
942 TABLE *table_r2= it++;
943 EXPECT_TRUE(table_r2 != NULL);
944 EXPECT_TRUE(table_r1 != table_r2);
945 EXPECT_TRUE(it++ == NULL);
946 EXPECT_TRUE(it++ == NULL);
953 EXPECT_TRUE(table_r1 != NULL);
955 EXPECT_TRUE(table_r2 != NULL);
956 TABLE *table_r3= it++;
957 EXPECT_TRUE(table_r3 != NULL);
958 EXPECT_TRUE(table_r1 != table_r2 && table_r1 != table_r3 && table_r2 != table_r3);
959 EXPECT_TRUE(it++ == NULL);
960 EXPECT_TRUE(it++ == NULL);
967 EXPECT_TRUE(table_r1 != NULL);
969 EXPECT_TRUE(table_r2 != NULL);
970 EXPECT_TRUE(table_r1 != table_r2);
971 EXPECT_TRUE(it++ == NULL);
972 EXPECT_TRUE(it++ == NULL);
980 EXPECT_TRUE(table_r1 != NULL);
982 EXPECT_TRUE(table_r2 != NULL);
983 EXPECT_TRUE(table_r1 != table_r2);
984 EXPECT_TRUE(it++ == NULL);
985 EXPECT_TRUE(it++ == NULL);
992 EXPECT_TRUE(table_r1 == table_4);
993 EXPECT_TRUE(it++ == NULL);
994 EXPECT_TRUE(it++ == NULL);
1000 EXPECT_TRUE(it++ == NULL);
1001 EXPECT_TRUE(it++ == NULL);
1007 EXPECT_TRUE(it++ == NULL);
1008 EXPECT_TRUE(it++ == NULL);
1015 EXPECT_TRUE(it++ == NULL);
1016 EXPECT_TRUE(it++ == NULL);
1020 share_1.destroy_table(table_1);
1021 share_1.destroy_table(table_2);
1022 share_2.destroy_table(table_3);
1023 share_1.destroy_table(table_4);
1024 share_2.destroy_table(table_5);