Move more unit tests into the ep_unit_tests binary 12/68212/7
authorDave Rigby <daver@couchbase.com>
Fri, 12 Aug 2016 14:53:16 +0000 (15:53 +0100)
committerDave Rigby <daver@couchbase.com>
Tue, 11 Oct 2016 08:02:45 +0000 (08:02 +0000)
For GTest-style tests we have created a single test binary
(ep-engine_ep_unit_tests) to link all the tests into. This has the
advantage of not having to compile different variants of our source
files for multiple different test binaries (which is partly a
limitation / 'feature' of CMake's dependancy calculation).

However not all tests are in this binary. This patch moves an
additional 2 test suites - checkpoint & defragmenter - into the single
binary. This speeds up our build time, and also removes a bunch of
duplicated boilerplate test setup code.

Change-Id: I7a9b6f8166fe2dcb739bdf124b43d1de6abc1e42
Reviewed-on: http://review.couchbase.org/68212
Tested-by: buildbot <build@couchbase.com>
Reviewed-by: Manu Dhundi <manu@couchbase.com>
CMakeLists.txt
src/memory_tracker.cc
src/memory_tracker.h
tests/module_tests/checkpoint_test.cc
tests/module_tests/defragmenter_test.cc
tests/module_tests/ep_unit_tests_main.cc

index 191e345..639efc9 100644 (file)
@@ -183,6 +183,8 @@ TARGET_LINK_LIBRARIES(ep cJSON JSON_checker couchstore forestdb
 # so simpler / quicker just to link them into a single executable).
 ADD_EXECUTABLE(ep-engine_ep_unit_tests
   tests/mock/mock_dcp.cc
+  tests/module_tests/checkpoint_test.cc
+  tests/module_tests/defragmenter_test.cc
   tests/module_tests/ep_unit_tests_main.cc
   tests/module_tests/dcp_test.cc
   tests/module_tests/evp_engine_test.cc
@@ -256,21 +258,6 @@ ADD_EXECUTABLE(ep-engine_atomic_test
   src/testlogger.cc)
 TARGET_LINK_LIBRARIES(ep-engine_atomic_test platform)
 
-ADD_EXECUTABLE(ep-engine_checkpoint_test
-  tests/module_tests/checkpoint_test.cc
-  src/atomic.cc
-  src/bloomfilter.cc
-  src/checkpoint.cc
-  src/compress.cc
-  src/failover-table.cc
-  src/item.cc
-  src/murmurhash3.cc
-  src/stored-value.cc
-  src/testlogger.cc
-  src/vbucket.cc
-  ${OBJECTREGISTRY_SOURCE} ${CONFIG_SOURCE})
-TARGET_LINK_LIBRARIES(ep-engine_checkpoint_test gtest ${SNAPPY_LIBRARIES} cJSON platform)
-
 ADD_EXECUTABLE(ep-engine_chunk_creation_test
   tests/module_tests/chunk_creation_test.cc)
 TARGET_LINK_LIBRARIES(ep-engine_chunk_creation_test platform)
@@ -342,7 +329,6 @@ TARGET_LINK_LIBRARIES(ep-engine_kvstore_test
 
 ADD_TEST(ep-engine_atomic_ptr_test ep-engine_atomic_ptr_test)
 ADD_TEST(ep-engine_atomic_test ep-engine_atomic_test)
-ADD_TEST(ep-engine_checkpoint_test ep-engine_checkpoint_test)
 ADD_TEST(ep-engine_chunk_creation_test ep-engine_chunk_creation_test)
 ADD_TEST(ep-engine_compress_test ep-engine_compress_test)
 ADD_TEST(ep-engine_configuration_test ep-engine_configuration_test)
@@ -354,7 +340,6 @@ ADD_TEST(ep-engine_misc_test ep-engine_misc_test)
 ADD_TEST(ep-engine_mutex_test ep-engine_mutex_test)
 ADD_TEST(ep-engine_ringbuffer_test ep-engine_ringbuffer_test)
 ADD_TEST(ep-engine_kvstore_test ep-engine_kvstore_test)
-ADD_TEST(ep-engine_defragmenter_test ep-engine_defragmenter_test)
 ADD_TEST(ep-engine_memory_tracker_test ep-engine_memory_tracker_test)
 
 ADD_LIBRARY(timing_tests SHARED tests/module_tests/timing_tests.cc)
@@ -365,30 +350,6 @@ ADD_EXECUTABLE(ep-engine_sizes src/sizes.cc src/mutex.h src/testlogger.cc
               ${OBJECTREGISTRY_SOURCE} ${CONFIG_SOURCE})
 TARGET_LINK_LIBRARIES(ep-engine_sizes platform)
 
-ADD_EXECUTABLE(ep-engine_defragmenter_test
-               tests/module_tests/defragmenter_test.cc
-               src/bloomfilter.cc
-               src/checkpoint.cc
-               src/compress.cc
-               src/configuration.cc
-               src/defragmenter_visitor.cc
-               src/ep_time.c
-               src/generated_configuration.cc
-               src/failover-table.cc
-               src/item.cc
-               src/memory_tracker.cc
-               src/murmurhash3.cc
-               src/stored-value.cc
-               src/testlogger.cc
-               src/vbucket.cc
-               ${OBJECTREGISTRY_SOURCE}
-               ${Memcached_SOURCE_DIR}/utilities/extension_loggers.c
-               ${Memcached_SOURCE_DIR}/programs/engine_testapp/mock_server.cc
-               $<TARGET_OBJECTS:memory_tracking>)
-TARGET_LINK_LIBRARIES(ep-engine_defragmenter_test
-                      cJSON gtest mcd_util platform ${MALLOC_LIBRARIES}
-                      ${SNAPPY_LIBRARIES})
-
 ADD_LIBRARY(ep_testsuite SHARED
    tests/ep_testsuite.cc
    src/atomic.cc
index 4dfb7db..2e3f64d 100644 (file)
@@ -25,7 +25,7 @@
 #include "memory_tracker.h"
 #include "objectregistry.h"
 
-bool MemoryTracker::tracking = false;
+std::atomic<bool> MemoryTracker::tracking{false};
 std::atomic<MemoryTracker*> MemoryTracker::instance;
 std::mutex MemoryTracker::instance_mutex;
 
index 5c0c364..d5e9706 100644 (file)
@@ -75,7 +75,7 @@ private:
     static void DeleteHook(const void* ptr);
 
     // Wheter or not we have the ability to accurately track memory allocations
-    static bool tracking;
+    static std::atomic<bool> tracking;
     // Singleton memory tracker and mutex guarding it's creation.
     static std::atomic<MemoryTracker*> instance;
     static std::mutex instance_mutex;
index a72071f..c797d45 100644 (file)
@@ -17,8 +17,6 @@
 
 #include "config.h"
 
-#include <signal.h>
-
 #include <algorithm>
 #include <set>
 #include <vector>
 
 #include <gtest/gtest.h>
 
-#ifdef _MSC_VER
-#define alarm(a)
-#endif
-
 #define NUM_TAP_THREADS 3
 #define NUM_SET_THREADS 4
 
@@ -51,15 +45,6 @@ struct thread_args {
 };
 
 extern "C" {
-static rel_time_t basic_current_time(void) {
-    return 0;
-}
-
-rel_time_t (*ep_current_time)() = basic_current_time;
-
-time_t ep_real_time() {
-    return time(NULL);
-}
 
 /**
  * Dummy callback to replace the flusher callback.
@@ -243,10 +228,6 @@ TEST_F(CheckpointTest, basic_chk_test) {
                                            MustSendCheckpointEnd::YES);
     }
 
-    // Start a timer so that the test can be killed if it doesn't finish in a
-    // reasonable amount of time
-    alarm(60);
-
     rc = cb_create_thread(&persistence_thread, launch_persistence_thread, &t_args, 0);
     EXPECT_EQ(0, rc);
 
@@ -759,15 +740,3 @@ TEST_F(CheckpointTest, CursorMovement) {
 
 }
 
-/* static storage for environment variable set by putenv(). */
-static char allow_no_stats_env[] = "ALLOW_NO_STATS_UPDATE=yeah";
-
-int main(int argc, char **argv) {
-    ::testing::InitGoogleTest(&argc, argv);
-    putenv(allow_no_stats_env);
-
-    HashTable::setDefaultNumBuckets(5);
-    HashTable::setDefaultNumLocks(1);
-
-    return RUN_ALL_TESTS();
-}
index 99492f7..cb040b4 100644 (file)
 #include <platform/cb_malloc.h>
 
 
-static time_t start_time;
-
-static time_t mock_abstime(const rel_time_t exptime) {
-    return start_time + exptime;
-}
-
-static rel_time_t mock_current_time(void) {
-    rel_time_t result = (rel_time_t)(time(NULL) - start_time);
-    return result;
-}
-
 /**
  * Dummy callback to replace the flusher callback.
  */
@@ -106,9 +95,38 @@ static size_t benchmarkDefragment(VBucket& vbucket, size_t passes,
     return size_t(visited / duration_s);
 }
 
-class DefragmenterBenchmarkTest : public ::testing::Test {
+class DefragmenterTest : public ::testing::Test {
 protected:
     static void SetUpTestCase() {
+
+        // Setup the MemoryTracker.
+        MemoryTracker::getInstance(*get_mock_server_api()->alloc_hooks);
+    }
+
+    static void TearDownTestCase() {
+        MemoryTracker::destroyInstance();
+    }
+
+    void SetUp() override {
+        // Setup object registry. As we do not create a full ep-engine, we
+        // use the "initial_tracking" for all memory tracking".
+        ObjectRegistry::setStats(&mem_used);
+    }
+
+    void TearDown() override {
+        ObjectRegistry::setStats(nullptr);
+    }
+
+    // Track of memory used (from ObjectRegistry).
+    std::atomic<size_t> mem_used{0};
+};
+
+
+class DefragmenterBenchmarkTest : public DefragmenterTest {
+protected:
+    static void SetUpTestCase() {
+        DefragmenterTest::SetUpTestCase();
+
         /* Create the vbucket */
         std::shared_ptr<Callback<uint16_t> > cb(new DummyCB());
         vbucket.reset(new VBucket(0, vbucket_state_active, stats, config,
@@ -117,6 +135,8 @@ protected:
 
     static void TearDownTestCase() {
         vbucket.reset();
+
+        DefragmenterTest::TearDownTestCase();
     }
 
     static EPStats stats;
@@ -200,18 +220,6 @@ static bool wait_for_mapped_below(size_t mapped_threshold,
     return true;
 }
 
-ALLOCATOR_HOOKS_API* getHooksApi(void) {
-    static ALLOCATOR_HOOKS_API hooksApi;
-    hooksApi.add_new_hook = mc_add_new_hook;
-    hooksApi.remove_new_hook = mc_remove_new_hook;
-    hooksApi.add_delete_hook = mc_add_delete_hook;
-    hooksApi.remove_delete_hook = mc_remove_delete_hook;
-    hooksApi.get_extra_stats_size = mc_get_extra_stats_size;
-    hooksApi.get_allocator_stats = mc_get_allocator_stats;
-    hooksApi.get_allocation_size = mc_get_allocation_size;
-    return &hooksApi;
-}
-
 // Create a number of documents, spanning at least two or more pages, then
 // delete most (but not all) of them - crucially ensure that one document from
 // each page is still present. This will result in the rest of that page
@@ -222,19 +230,11 @@ ALLOCATOR_HOOKS_API* getHooksApi(void) {
  * memory allocator.
  */
 #if defined(HAVE_JEMALLOC)
-TEST(DefragmenterTest, MappedMemory) {
+TEST_F(DefragmenterTest, MappedMemory) {
 #else
-TEST(DefragmenterTest, DISABLED_MappedMemory) {
+TEST_F(DefragmenterTest, DISABLED_MappedMemory) {
 #endif
 
-    // Instantiate memory tracker (singleton created on-demand).
-    (void)MemoryTracker::getInstance(*get_mock_server_api()->alloc_hooks);
-
-    // Setup object registry. As we do not create a full ep-engine, we
-    // use the "initial_tracking" for all memory tracking".
-    std::atomic<size_t> mem_used(0);
-    ObjectRegistry::setStats(&mem_used);
-
     // Sanity check - need memory tracker to be able to check our memory usage.
     ASSERT_TRUE(MemoryTracker::trackingMemoryAllocations())
         << "Memory tracker not enabled - cannot continue";
@@ -368,26 +368,4 @@ TEST(DefragmenterTest, DISABLED_MappedMemory) {
         << "estimate (" <<  expected_mapped << ") after the defragmentater "
         << "visited " << visitor.getVisitedCount() << " items "
         << "and moved " << visitor.getDefragCount() << " items!";
-
-    MemoryTracker::destroyInstance();
-}
-
-
-static char allow_no_stats_env[] = "ALLOW_NO_STATS_UPDATE=1";
-
-int main(int argc, char** argv) {
-    /* Setup mock time functions */
-    start_time = time(0);
-    ep_abs_time = mock_abstime;
-    ep_current_time = mock_current_time;
-
-    putenv(allow_no_stats_env);
-
-    // Set number of hashtable locks equal to current JSON config default.
-    HashTable::setDefaultNumLocks(47);
-
-    init_alloc_hooks();
-
-    ::testing::InitGoogleTest(&argc, argv);
-    return RUN_ALL_TESTS();
 }
index fd7abc5..3467782 100644 (file)
@@ -71,6 +71,9 @@ int main(int argc, char **argv) {
     // writing).
     HashTable::setDefaultNumLocks(Configuration().getHtLocks());
 
+    // Need to initialize ep_real_time and friends.
+    initialize_time_functions(get_mock_server_api()->core);
+
     ::testing::InitGoogleTest(&argc, argv);
     return RUN_ALL_TESTS();
 }