1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph distributed storage system
6 * Copyright (C) 2013,2014 Cloudwatt <libre.licensing@cloudwatt.com>
7 * Copyright (C) 2014 Red Hat <contact@redhat.com>
9 * Author: Loic Dachary <loic@dachary.org>
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
21 #include "common/Thread.h"
22 #include "erasure-code/ErasureCodePlugin.h"
23 #include "global/global_context.h"
24 #include "common/config.h"
25 #include "gtest/gtest.h"
28 class ErasureCodePluginRegistryTest : public ::testing::Test {
31 class Thread_factory : public Thread {
33 static void cleanup(void *arg) {
34 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
35 if (instance.lock.is_locked())
36 instance.lock.Unlock();
39 void *entry() override {
40 ErasureCodeProfile profile;
41 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
42 ErasureCodeInterfaceRef erasure_code;
43 pthread_cleanup_push(cleanup, NULL);
44 instance.factory("hangs",
45 g_conf->get_val<std::string>("erasure_code_dir"),
46 profile, &erasure_code, &cerr);
47 pthread_cleanup_pop(0);
54 TEST_F(ErasureCodePluginRegistryTest, factory_mutex) {
55 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
57 EXPECT_TRUE(instance.lock.TryLock());
58 instance.lock.Unlock();
61 // Test that the loading of a plugin is protected by a mutex.
64 const useconds_t DELAY_MAX = 20 * 1000 * 1000;
65 Thread_factory sleep_forever;
66 sleep_forever.create("sleep_forever");
68 cout << "Trying (1) with delay " << delay << "us\n";
71 if (!instance.loading)
72 delay = ( delay + 1 ) * 2;
73 } while(!instance.loading && delay < DELAY_MAX);
74 ASSERT_TRUE(delay < DELAY_MAX);
76 EXPECT_FALSE(instance.lock.TryLock());
78 EXPECT_EQ(0, pthread_cancel(sleep_forever.get_thread_id()));
79 EXPECT_EQ(0, sleep_forever.join());
82 TEST_F(ErasureCodePluginRegistryTest, all)
84 ErasureCodeProfile profile;
85 const char* env = getenv("CEPH_LIB");
86 string directory(env ? env : ".libs");
87 ErasureCodeInterfaceRef erasure_code;
88 ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance();
89 EXPECT_FALSE(erasure_code);
90 EXPECT_EQ(-EIO, instance.factory("invalid",
91 g_conf->get_val<std::string>("erasure_code_dir"),
92 profile, &erasure_code, &cerr));
93 EXPECT_FALSE(erasure_code);
94 EXPECT_EQ(-EXDEV, instance.factory("missing_version",
95 g_conf->get_val<std::string>("erasure_code_dir"),
97 &erasure_code, &cerr));
98 EXPECT_FALSE(erasure_code);
99 EXPECT_EQ(-ENOENT, instance.factory("missing_entry_point",
100 g_conf->get_val<std::string>("erasure_code_dir"),
102 &erasure_code, &cerr));
103 EXPECT_FALSE(erasure_code);
104 EXPECT_EQ(-ESRCH, instance.factory("fail_to_initialize",
105 g_conf->get_val<std::string>("erasure_code_dir"),
107 &erasure_code, &cerr));
108 EXPECT_FALSE(erasure_code);
109 EXPECT_EQ(-EBADF, instance.factory("fail_to_register",
110 g_conf->get_val<std::string>("erasure_code_dir"),
112 &erasure_code, &cerr));
113 EXPECT_FALSE(erasure_code);
114 EXPECT_EQ(0, instance.factory("example",
115 g_conf->get_val<std::string>("erasure_code_dir"),
116 profile, &erasure_code, &cerr));
117 EXPECT_TRUE(erasure_code.get());
118 ErasureCodePlugin *plugin = 0;
120 Mutex::Locker l(instance.lock);
121 EXPECT_EQ(-EEXIST, instance.load("example", directory, &plugin, &cerr));
122 EXPECT_EQ(-ENOENT, instance.remove("does not exist"));
123 EXPECT_EQ(0, instance.remove("example"));
124 EXPECT_EQ(0, instance.load("example", directory, &plugin, &cerr));
130 * compile-command: "cd ../.. ; make -j4 &&
131 * make unittest_erasure_code_plugin &&
132 * valgrind --tool=memcheck \
133 * ./unittest_erasure_code_plugin \
134 * --gtest_filter=*.* --log-to-stderr=true --debug-osd=20"