aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/util/mmap_file_allocator_factory/mmap_file_allocator_factory_test.cpp
blob: 346c415ff9bf1cc92c035093dff93d744c2b1477 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/mmap_file_allocator_factory.h>
#include <vespa/vespalib/util/mmap_file_allocator.h>
#include <vespa/vespalib/util/memory_allocator.h>
#include <vespa/vespalib/gtest/gtest.h>
#include <filesystem>

using vespalib::alloc::MemoryAllocator;
using vespalib::alloc::MmapFileAllocator;
using vespalib::alloc::MmapFileAllocatorFactory;

namespace {

vespalib::string basedir("mmap-file-allocator-factory-dir");

bool is_mmap_file_allocator(const MemoryAllocator *allocator)
{
    return dynamic_cast<const MmapFileAllocator *>(allocator) != nullptr;
}

}

TEST(MmapFileAllocatorFactoryTest, empty_dir_gives_no_allocator)
{
    MmapFileAllocatorFactory::instance().setup("");
    auto allocator = MmapFileAllocatorFactory::instance().make_memory_allocator("foo");
    EXPECT_FALSE(allocator);
}

TEST(MmapFileAllocatorFactoryTest, nonempty_dir_gives_allocator)
{
    MmapFileAllocatorFactory::instance().setup(basedir);
    auto allocator0 = MmapFileAllocatorFactory::instance().make_memory_allocator("foo");
    auto allocator1 = MmapFileAllocatorFactory::instance().make_memory_allocator("bar");
    EXPECT_TRUE(is_mmap_file_allocator(allocator0.get()));
    EXPECT_TRUE(is_mmap_file_allocator(allocator1.get()));
    vespalib::string allocator0_dir(basedir + "/0.foo");
    vespalib::string allocator1_dir(basedir + "/1.bar");
    EXPECT_TRUE(std::filesystem::is_directory(std::filesystem::path(allocator0_dir)));
    EXPECT_TRUE(std::filesystem::is_directory(std::filesystem::path(allocator1_dir)));
    allocator0.reset();
    EXPECT_FALSE(std::filesystem::is_directory(std::filesystem::path(allocator0_dir)));
    allocator1.reset();
    EXPECT_FALSE(std::filesystem::is_directory(std::filesystem::path(allocator1_dir)));
    MmapFileAllocatorFactory::instance().setup("");
    std::filesystem::remove_all(std::filesystem::path(basedir));
}

GTEST_MAIN_RUN_ALL_TESTS()