MB-20623: re-enable and fix MutationLog unit test 39/68039/10
authorDave Rigby <daver@couchbase.com>
Mon, 22 Aug 2016 13:46:01 +0000 (14:46 +0100)
committerDave Rigby <daver@couchbase.com>
Fri, 30 Sep 2016 13:29:26 +0000 (13:29 +0000)
This test appears to have been not built/run a while back. Resurrect
it.

Change-Id: I4b35291239882e5e58bc2c7d435e3c793a7ae6ba
Reviewed-on: http://review.couchbase.org/68039
Reviewed-by: Daniel Owen <owend@couchbase.com>
Tested-by: buildbot <build@couchbase.com>
CMakeLists.txt
src/mutation_log.cc
src/mutation_log.h
tests/module_tests/mutation_log_test.cc

index 322e76f..b993bf0 100644 (file)
@@ -351,6 +351,7 @@ ADD_TEST(ep-engine_hash_table_test ep-engine_hash_table_test)
 ADD_TEST(ep-engine_hrtime_test ep-engine_hrtime_test)
 ADD_TEST(ep-engine_misc_test ep-engine_misc_test)
 ADD_TEST(ep-engine_mutex_test ep-engine_mutex_test)
+ADD_TEST(ep-engine_mutation_log_test ep-engine_mutation_log_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)
@@ -387,6 +388,63 @@ TARGET_LINK_LIBRARIES(ep-engine_defragmenter_test
                       cJSON gtest platform ${MALLOC_LIBRARIES}
                       ${SNAPPY_LIBRARIES})
 
+ADD_EXECUTABLE(ep-engine_mutation_log_test
+               tests/module_tests/mutation_log_test.cc
+               src/access_scanner.cc
+               src/atomic.cc
+               src/backfill.cc
+               src/bgfetcher.cc
+               src/bloomfilter.cc
+               src/checkpoint.cc
+               src/checkpoint_remover.cc
+               src/conflict_resolution.cc
+               src/compress.cc
+               src/connmap.cc
+               src/dcp/backfill.cc
+               src/dcp/backfill-manager.cc
+               src/dcp/consumer.cc
+               src/dcp/flow-control.cc
+               src/dcp/flow-control-manager.cc
+               src/dcp/producer.cc
+               src/dcp/response.cc
+               src/dcp/stream.cc
+               src/defragmenter.cc
+               src/defragmenter_visitor.cc
+               src/ep.cc
+               src/ep_engine.cc
+               src/ep_time.c
+               src/executorpool.cc
+               src/executorthread.cc
+               src/ext_meta_parser.cc
+               src/failover-table.cc
+               src/flusher.cc
+               src/htresizer.cc
+               src/item.cc
+               src/item_pager.cc
+               src/kvshard.cc
+               src/logger.cc
+               src/memory_tracker.cc
+               src/murmurhash3.cc
+               src/mutation_log.cc
+               src/objectregistry.cc
+               src/tapconnection.cc
+               src/replicationthrottle.cc
+               src/stored-value.cc
+               src/string_utils.cc
+               src/tasks.cc
+               src/taskqueue.cc
+               src/vbucket.cc
+               src/vbucketmap.cc
+               src/warmup.cc
+               ${CMAKE_CURRENT_BINARY_DIR}/src/stats-info.c
+               ${OBJECTREGISTRY_SOURCE}
+               ${KVSTORE_SOURCE}
+               ${COUCH_KVSTORE_SOURCE}
+               ${FOREST_KVSTORE_SOURCE}
+               ${CONFIG_SOURCE})
+TARGET_LINK_LIBRARIES(ep-engine_mutation_log_test
+                      couchstore cJSON dirutils forestdb JSON_checker platform ${SNAPPY_LIBRARIES})
+
 ADD_LIBRARY(ep_testsuite SHARED
    tests/ep_testsuite.cc
    src/atomic.cc
index 4a573b6..348c7bc 100644 (file)
 
 #include "config.h"
 
-#include <sys/stat.h>
-
 #include <algorithm>
+#include <fcntl.h>
+#include <platform/strerror.h>
 #include <string>
+#include <sys/stat.h>
 #include <system_error>
 #include <utility>
 
index b2c6388..e7d442a 100644 (file)
  *   limitations under the License.
  */
 
-#ifndef SRC_MUTATION_LOG_H_
-#define SRC_MUTATION_LOG_H_ 1
+#pragma once
 
 #include "config.h"
 
-#include <fcntl.h>
-
 #include <array>
-#include <algorithm>
-#include <cstdio>
-#include <exception>
-#include <functional>
-#include <iterator>
-#include <limits>
-#include <numeric>
-#include <set>
+#include <cstring>
+#include <memory>
 #include <string>
-#include <utility>
+#include <unordered_map>
 #include <vector>
 
 #include "atomic.h"
 #include <platform/histogram.h>
-#include <platform/strerror.h>
 #include "utility.h"
-#include "common.h"
 
 #define ML_BUFLEN (128 * 1024 * 1024)
 
@@ -633,5 +622,3 @@ private:
                        std::unordered_map<std::string, mutation_log_event_t> > loading;
     size_t itemsSeen[MUTATION_LOG_TYPES];
 };
-
-#endif  // SRC_MUTATION_LOG_H_
index 6283813..7266d90 100644 (file)
 
 #include "config.h"
 
-#include <signal.h>
-
 #include <algorithm>
+#include <fcntl.h>
 #include <map>
+#include <platform/strerror.h>
 #include <set>
 #include <stdexcept>
+#include <sys/stat.h>
 #include <vector>
 
 #include "assert.h"
 #include "mutation_log.h"
 
-#define TMP_LOG_FILE "/tmp/mlt_test.log"
+#define TMP_LOG_FILE "mlt_test.log"
+
+// Windows doesn't have a truncate() function, implement one.
+#if defined(WIN32)
+int truncate(const char *path, off_t length) {
+    LARGE_INTEGER distance;
+    distance.u.HighPart = 0;
+    distance.u.LowPart = length;
+
+    HANDLE fh = CreateFile(path, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
+    if (fh == INVALID_HANDLE_VALUE) {
+        fprintf(stderr, "truncate: CreateFile failed with error: %s\n",
+                cb_strerror().c_str());
+        abort();
+    }
+
+    cb_assert(SetFilePointerEx(fh, distance, NULL, FILE_BEGIN) != 0);
+    cb_assert(SetEndOfFile(fh) != 0);
+
+    CloseHandle(fh);
+    return 0;
+}
+#endif
+
+// Bitfield of available file permissions.
+namespace FilePerms {
+    const int None = 0;
+#if defined(WIN32)
+    const int Read = _S_IREAD;
+    const int Write = _S_IWRITE;
+#else
+    const int Read = S_IRUSR;
+    const int Write = S_IWUSR;
+#endif
+}
+
+/*
+ * Sets the read/write permissions on the given file (in a cross-platform way).
+ */
+static void set_file_perms(const char* path, /*FilePerms*/int perms) {
+#if defined(WIN32)
+    if (_chmod(path, perms) != 0) {
+#else
+    if (chmod(path, perms) != 0) {
+#endif
+        std::cerr << "set_file_perms: chmod failed: " << cb_strerror() << std::endl;
+        abort();
+    }
+}
+
 
 static void testUnconfigured() {
     MutationLog ml("");
@@ -113,10 +163,11 @@ static void testSyncSet() {
     cb_assert(ml.getFlushConfig() == FLUSH_COMMIT_1);
 }
 
-static void loaderFun(void *arg, uint16_t vb,
-                      const std::string &k, uint64_t rowid) {
-    std::map<std::string, uint64_t> *maps = reinterpret_cast<std::map<std::string, uint64_t> *>(arg);
-    maps[vb][k] = rowid;
+static bool loaderFun(void *arg, uint16_t vb,
+                      const std::string &k) {
+    std::set<std::string>* sets = reinterpret_cast<std::set<std::string> *>(arg);
+    sets[vb].insert(k);
+    return true;
 }
 
 static void testLogging() {
@@ -360,13 +411,14 @@ static void testLoggingBadCRC() {
     }
 
     // Break the log
-    int file = open(TMP_LOG_FILE, O_RDWR, 0666);
+    int file = open(TMP_LOG_FILE, O_RDWR, FilePerms::Read | FilePerms::Write);
     cb_assert(lseek(file, 5000, SEEK_SET) == 5000);
     uint8_t b;
     cb_assert(read(file, &b, sizeof(b)) == 1);
     cb_assert(lseek(file, 5000, SEEK_SET) == 5000);
     b = ~b;
     cb_assert(write(file, &b, sizeof(b)) == 1);
+    close(file);
 
     {
         MutationLog ml(TMP_LOG_FILE);
@@ -455,16 +507,13 @@ static void testLoggingShortRead() {
     }
 
     // Break the log harder (can't read even the initial block)
+    // This should succeed as open() will call reset() to give us a usable
+    // mutation log.
     cb_assert(truncate(TMP_LOG_FILE, 4000) == 0);
 
     {
         MutationLog ml(TMP_LOG_FILE);
-        try {
-            ml.open();
-            abort();
-        } catch(MutationLog::ShortReadException &e) {
-            // expected
-        }
+        ml.open();
     }
 
     remove(TMP_LOG_FILE);
@@ -472,6 +521,7 @@ static void testLoggingShortRead() {
 
 static void testYUNOOPEN() {
     int file = open(TMP_LOG_FILE, O_CREAT|O_RDWR, 0);
+    set_file_perms(TMP_LOG_FILE, FilePerms::None);
     cb_assert(file >= 0);
     close(file);
     MutationLog ml(TMP_LOG_FILE);
@@ -479,13 +529,19 @@ static void testYUNOOPEN() {
         ml.open();
         abort();
     } catch(MutationLog::ReadException &e) {
-        std::string exp("Unable to open log file: Permission denied");
+        const std::string exp("Unable to open log file:");
         // This is kind of a soft assertion.  The actual text may vary.
-        if (e.what() != exp) {
+        if (exp.compare(0, exp.size(), e.what(), exp.size()) != 0) {
             std::cerr << "Expected ``" << exp << "'', got: " << e.what() << std::endl;
         }
     }
-    cb_assert(remove(TMP_LOG_FILE) == 0);
+
+    // Restore permissions to be able to delete file.
+    set_file_perms(TMP_LOG_FILE, FilePerms::Read | FilePerms::Write);
+    if (remove(TMP_LOG_FILE) != 0)
+    {
+        std::cerr << "testYUNOOPEN: remove failed: " << cb_strerror() << std::endl;
+    }
 }
 
 // @todo
@@ -519,6 +575,7 @@ static void testReadOnly() {
 }
 
 int main(int, char **) {
+    remove(TMP_LOG_FILE);
     testReadOnly();
     testUnconfigured();
     testSyncSet();