From 4bc18189b14d43a48d4cb6eff80bc8e9d3a59e8f Mon Sep 17 00:00:00 2001 From: Loic Dachary Date: Tue, 24 Sep 2013 15:51:08 +0200 Subject: [PATCH] ErasureCode: fix plugin loading threaded test The TEST_F(ErasureCodePluginRegistryTest, factory_mutex) was bugous and only succeeded by chance. The sleep was on the factory constructor which was never called. An erasure code plugin that hangs forever on load is created instead. The sleep_forever.detach is replaced by pthread_cancel to interrupt the thread that hangs forever. If not, gtest will try to join the thread and never exit. Signed-off-by: Loic Dachary --- src/test/Makefile.am | 7 +++++++ src/test/osd/ErasureCodeExample.h | 11 ----------- src/test/osd/ErasureCodePluginExample.cc | 4 +++- src/test/osd/ErasureCodePluginHangs.cc | 24 ++++++++++++++++++++++++ src/test/osd/TestErasureCodeExample.cc | 23 +++-------------------- src/test/osd/TestErasureCodePlugin.cc | 14 ++++---------- 6 files changed, 41 insertions(+), 42 deletions(-) create mode 100644 src/test/osd/ErasureCodePluginHangs.cc diff --git a/src/test/Makefile.am b/src/test/Makefile.am index 5b709d248a81..a3127be9455c 100644 --- a/src/test/Makefile.am +++ b/src/test/Makefile.am @@ -323,6 +323,13 @@ libec_missing_entry_point_la_LIBADD = $(PTHREAD_LIBS) $(EXTRALIBS) libec_missing_entry_point_la_LDFLAGS = ${AM_LDFLAGS} -export-symbols-regex '.*__erasure_code_.*' erasure_codelib_LTLIBRARIES += libec_missing_entry_point.la +libec_hangs_la_SOURCES = test/osd/ErasureCodePluginHangs.cc +libec_hangs_la_CFLAGS = ${AM_CFLAGS} +libec_hangs_la_CXXFLAGS= ${AM_CXXFLAGS} +libec_hangs_la_LIBADD = $(PTHREAD_LIBS) $(EXTRALIBS) +libec_hangs_la_LDFLAGS = ${AM_LDFLAGS} -export-symbols-regex '.*__erasure_code_.*' +erasure_codelib_LTLIBRARIES += libec_hangs.la + libec_fail_to_initialize_la_SOURCES = test/osd/ErasureCodePluginFailToInitialize.cc libec_fail_to_initialize_la_CFLAGS = ${AM_CFLAGS} libec_fail_to_initialize_la_CXXFLAGS= ${AM_CXXFLAGS} diff --git a/src/test/osd/ErasureCodeExample.h b/src/test/osd/ErasureCodeExample.h index 95d79feb923f..0fd55187559f 100644 --- a/src/test/osd/ErasureCodeExample.h +++ b/src/test/osd/ErasureCodeExample.h @@ -34,17 +34,6 @@ class ErasureCodeExample : public ErasureCodeInterface { public: - useconds_t delay; - ErasureCodeExample(const map ¶meters) : - delay(0) - { - if (parameters.find("usleep") != parameters.end()) { - std::istringstream ss(parameters.find("usleep")->second); - ss >> delay; - usleep(delay); - } - } - virtual ~ErasureCodeExample() {} virtual int minimum_to_decode(const set &want_to_read, diff --git a/src/test/osd/ErasureCodePluginExample.cc b/src/test/osd/ErasureCodePluginExample.cc index 1543b1cdaede..6ae61c0a18de 100644 --- a/src/test/osd/ErasureCodePluginExample.cc +++ b/src/test/osd/ErasureCodePluginExample.cc @@ -14,6 +14,8 @@ * */ +#include + #include "osd/ErasureCodePlugin.h" #include "ErasureCodeExample.h" @@ -22,7 +24,7 @@ public: virtual int factory(const map ¶meters, ErasureCodeInterfaceRef *erasure_code) { - *erasure_code = ErasureCodeInterfaceRef(new ErasureCodeExample(parameters)); + *erasure_code = ErasureCodeInterfaceRef(new ErasureCodeExample()); return 0; } }; diff --git a/src/test/osd/ErasureCodePluginHangs.cc b/src/test/osd/ErasureCodePluginHangs.cc new file mode 100644 index 000000000000..ea73786b5267 --- /dev/null +++ b/src/test/osd/ErasureCodePluginHangs.cc @@ -0,0 +1,24 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +// vim: ts=8 sw=2 smarttab +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2013 Cloudwatt + * + * Author: Loic Dachary + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + */ + +#include +#include "osd/ErasureCodePlugin.h" + +int __erasure_code_init(char *plugin_name) +{ + sleep(1000); + return 0; +} diff --git a/src/test/osd/TestErasureCodeExample.cc b/src/test/osd/TestErasureCodeExample.cc index 6866dfdbb9ff..f12e80c8cd04 100644 --- a/src/test/osd/TestErasureCodeExample.cc +++ b/src/test/osd/TestErasureCodeExample.cc @@ -20,24 +20,9 @@ #include "global/global_context.h" #include "gtest/gtest.h" -TEST(ErasureCodeExample, constructor) -{ - map parameters; - { - ErasureCodeExample example(parameters); - EXPECT_EQ(0u, example.delay); - } - parameters["usleep"] = "10"; - { - ErasureCodeExample example(parameters); - EXPECT_EQ(10u, example.delay); - } -} - TEST(ErasureCodeExample, minimum_to_decode) { - map parameters; - ErasureCodeExample example(parameters); + ErasureCodeExample example; set available_chunks; set want_to_read; want_to_read.insert(1); @@ -72,8 +57,7 @@ TEST(ErasureCodeExample, minimum_to_decode) TEST(ErasureCodeExample, minimum_to_decode_with_cost) { - map parameters; - ErasureCodeExample example(parameters); + ErasureCodeExample example; map available; set want_to_read; want_to_read.insert(1); @@ -117,8 +101,7 @@ TEST(ErasureCodeExample, minimum_to_decode_with_cost) TEST(ErasureCodeExample, encode_decode) { - map parameters; - ErasureCodeExample example(parameters); + ErasureCodeExample example; bufferlist in; in.append("ABCDE"); diff --git a/src/test/osd/TestErasureCodePlugin.cc b/src/test/osd/TestErasureCodePlugin.cc index ba7d13fbd2d6..46ed4b1730dc 100644 --- a/src/test/osd/TestErasureCodePlugin.cc +++ b/src/test/osd/TestErasureCodePlugin.cc @@ -28,19 +28,12 @@ protected: class Thread_factory : public Thread { public: - useconds_t delay; - - Thread_factory(useconds_t _delay) : - delay(_delay) - {} - virtual void *entry() { map parameters; parameters["erasure-code-directory"] = ".libs"; - parameters["usleep"] = delay; ErasureCodePluginRegistry &instance = ErasureCodePluginRegistry::instance(); ErasureCodeInterfaceRef erasure_code; - instance.factory("example", parameters, &erasure_code); + instance.factory("hangs", parameters, &erasure_code); return NULL; } }; @@ -58,7 +51,7 @@ TEST_F(ErasureCodePluginRegistryTest, factory_mutex) { // useconds_t delay = 0; const useconds_t DELAY_MAX = 20 * 1000 * 1000; - Thread_factory sleep_forever(1024 * 1024 * 1024); + Thread_factory sleep_forever; sleep_forever.create(); do { cout << "Trying (1) with delay " << delay << "us\n"; @@ -71,7 +64,8 @@ TEST_F(ErasureCodePluginRegistryTest, factory_mutex) { EXPECT_FALSE(instance.lock.TryLock()); - EXPECT_EQ(0, sleep_forever.detach()); + EXPECT_EQ(0, pthread_cancel(sleep_forever.get_thread_id())); + EXPECT_EQ(0, sleep_forever.join()); } TEST_F(ErasureCodePluginRegistryTest, all) -- 2.47.3