Skip to content

Commit

Permalink
DataStorage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidharabagiu committed Jul 3, 2021
1 parent 10a1da3 commit 96818c4
Show file tree
Hide file tree
Showing 4 changed files with 558 additions and 5 deletions.
10 changes: 7 additions & 3 deletions sand/libsand/storage/src/filestorageimpl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "filestorageimpl.hpp"

#include <filesystem>

#include <glog/logging.h>

#include "filestoragemetadata.hpp"
Expand Down Expand Up @@ -96,14 +98,14 @@ size_t FileStorageImpl::write_file(Handle handle, size_t offset, size_t amount,

bool FileStorageImpl::close_file(Handle handle)
{
std::lock_guard lock {mutex_};

auto file = get_open_file(handle);
if (!file)
{
return false;
}

std::lock_guard lock {mutex_};

open_files_.erase(handle);

auto it = open_handles_by_file_path_.find(file->file_path());
Expand All @@ -124,7 +126,7 @@ bool FileStorageImpl::close_file(Handle handle)
bool FileStorageImpl::delete_file(const std::string &file_hash)
{
std::string file_path = file_storage_metadata_->get_file_path(file_hash);
if (file_hash.empty())
if (file_path.empty())
{
return false;
}
Expand All @@ -145,6 +147,8 @@ bool FileStorageImpl::delete_file(const std::string &file_hash)
}

file_storage_metadata_->remove(file_hash);
std::filesystem::remove(file_path);

return true;
}

Expand Down
15 changes: 13 additions & 2 deletions sand/libsand/storage/src/openfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,20 @@ bool OpenFile::map_file()

bool OpenFile::create_file(size_t size, bool truncate) const
{
std::ios::openmode mode = std::ios::out | std::ios::binary;

bool file_exists = std::filesystem::exists(file_path_);
if (truncate)
{
mode |= std::ios::trunc;
}
else if (file_exists)
{
mode |= std::ios::in;
}

{
std::ofstream fs {file_path_,
std::ios::out | std::ios::binary | std::ios::openmode(truncate ? std::ios::trunc : 0)};
std::fstream fs {file_path_, mode};
if (!fs)
{
LOG(ERROR) << "Cannot open " << file_path_ << " for writing";
Expand Down
19 changes: 19 additions & 0 deletions sand/libsand/test/inc/private/filestoragemetadata_mock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef SAND_TEST_FILESTORAGEMETADATA_MOCK_HPP_
#define SAND_TEST_FILESTORAGEMETADATA_MOCK_HPP_

#include <gmock/gmock.h>

#include "filestoragemetadata.hpp"

using namespace ::sand::storage;

class FileStorageMetadataMock : public FileStorageMetadata
{
public:
MOCK_METHOD(bool, contains, (const std::string &), (const, override));
MOCK_METHOD(std::string, get_file_path, (const std::string &), (const, override));
MOCK_METHOD(std::string, add, (const std::string &, const std::string &), (override));
MOCK_METHOD(bool, remove, (const std::string &), (override));
};

#endif // SAND_TEST_FILESTORAGEMETADATA_MOCK_HPP_
Loading

0 comments on commit 96818c4

Please sign in to comment.