/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include "folly/File.h"
+#include <folly/File.h>
-#include <mutex>
-
-#include <boost/thread/locks.hpp>
-#include <glog/logging.h>
-#include <gtest/gtest.h>
-
-#include "folly/Benchmark.h"
-#include "folly/String.h"
-#include "folly/Subprocess.h"
-#include "folly/experimental/io/FsUtil.h"
-#include "folly/experimental/TestUtil.h"
+#include <folly/String.h>
+#include <folly/portability/Fcntl.h>
+#include <folly/portability/GTest.h>
using namespace folly;
-using namespace folly::test;
namespace {
void expectWouldBlock(ssize_t r) {
int savedErrno = errno;
EXPECT_EQ(-1, r);
- EXPECT_EQ(EAGAIN, savedErrno) << errnoStr(errno);
+ EXPECT_EQ(EAGAIN, savedErrno) << errnoStr(savedErrno);
}
void expectOK(ssize_t r) {
int savedErrno = errno;
- EXPECT_LE(0, r) << ": errno=" << errnoStr(errno);
+ EXPECT_LE(0, r) << ": errno=" << errnoStr(savedErrno);
}
} // namespace
}
}
+TEST(File, SimpleStringPiece) {
+ char buf = 'x';
+ File f(StringPiece("/etc/hosts"));
+ EXPECT_NE(-1, f.fd());
+ EXPECT_EQ(1, ::read(f.fd(), &buf, 1));
+ f.close();
+ EXPECT_EQ(-1, f.fd());
+}
+
TEST(File, OwnsFd) {
// Wrap a file descriptor, make sure that ownsFd works
// We'll test that the file descriptor is closed by closing the writing
EXPECT_TRUE(false);
}
}
-
-TEST(File, Locks) {
- typedef std::unique_lock<File> Lock;
- typedef boost::shared_lock<File> SharedLock;
-
- // Find out where we are.
- static constexpr size_t pathLength = 2048;
- char buf[pathLength + 1];
- int r = readlink("/proc/self/exe", buf, pathLength);
- CHECK_ERR(r);
- buf[r] = '\0';
-
- fs::path helper(buf);
- helper.remove_filename();
- helper /= "file_test_lock_helper";
-
- TemporaryFile tempFile;
- File f(tempFile.fd());
-
- enum LockMode { EXCLUSIVE, SHARED };
- auto testLock = [&] (LockMode mode, bool expectedSuccess) {
- auto ret =
- Subprocess({helper.native(),
- mode == SHARED ? "-s" : "-x",
- tempFile.path().native()}).wait();
- EXPECT_TRUE(ret.exited());
- if (ret.exited()) {
- EXPECT_EQ(expectedSuccess ? 0 : 42, ret.exitStatus());
- }
- };
-
- // Make sure nothing breaks and things compile.
- {
- Lock lock(f);
- }
-
- {
- SharedLock lock(f);
- }
-
- {
- Lock lock(f, std::defer_lock);
- EXPECT_TRUE(lock.try_lock());
- }
-
- {
- SharedLock lock(f, boost::defer_lock);
- EXPECT_TRUE(lock.try_lock());
- }
-
- // X blocks X
- {
- Lock lock(f);
- testLock(EXCLUSIVE, false);
- }
-
- // X blocks S
- {
- Lock lock(f);
- testLock(SHARED, false);
- }
-
- // S blocks X
- {
- SharedLock lock(f);
- testLock(EXCLUSIVE, false);
- }
-
- // S does not block S
- {
- SharedLock lock(f);
- testLock(SHARED, true);
- }
-}
-