X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=folly%2Ftest%2FSingletonTest.cpp;h=46463554e5ca49be56aa66b0c832d58cc179fc4f;hb=321542683a01c3f334047531e9b487f047129775;hp=55152751567bcc7d00a7849aabc1f8afb943ea2a;hpb=8f1e662a59dec3476bf626b283effaa8a88acdd5;p=folly.git diff --git a/folly/test/SingletonTest.cpp b/folly/test/SingletonTest.cpp index 55152751..46463554 100644 --- a/folly/test/SingletonTest.cpp +++ b/folly/test/SingletonTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2015 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. @@ -19,7 +19,6 @@ #include #include #include -#include #include #include @@ -60,6 +59,7 @@ TEST(Singleton, BasicUsage) { EXPECT_NE(s2, nullptr); EXPECT_EQ(s1, s2); + EXPECT_EQ(s1.get(), SingletonBasicUsage::try_get_fast().get()); std::shared_ptr s3 = SingletonBasicUsage::try_get(); @@ -387,14 +387,14 @@ template using SingletonCreationError = Singleton; TEST(Singleton, SingletonCreationError) { - auto& vault = *SingletonVault::singleton(); + SingletonVault::singleton(); SingletonCreationError error_once_singleton; // first time should error out - EXPECT_THROW(error_once_singleton.get_weak().lock(), std::runtime_error); + EXPECT_THROW(error_once_singleton.try_get(), std::runtime_error); // second time it'll work fine - error_once_singleton.get_weak().lock(); + error_once_singleton.try_get(); SUCCEED(); } @@ -409,7 +409,7 @@ TEST(Singleton, SingletonConcurrencyStress) { std::vector ts; for (size_t i = 0; i < 100; ++i) { ts.emplace_back([&]() { - slowpoke_singleton.get_weak().lock(); + slowpoke_singleton.try_get(); }); } @@ -457,11 +457,12 @@ TEST(Singleton, SingletonEagerInitAsync) { [&] {didEagerInit = true; return new std::string("foo"); }) .shouldEagerInit(); folly::EventBase eb; + folly::Baton<> done; vault.registrationComplete(); EXPECT_FALSE(didEagerInit); - auto result = vault.doEagerInitVia(&eb); // a Future is returned + vault.doEagerInitVia(eb, &done); eb.loop(); - result.get(); // ensure this completed successfully and didn't hang forever + done.wait(); EXPECT_TRUE(didEagerInit); sing.get_weak(); // (avoid compile error complaining about unused var 'sing') } @@ -538,7 +539,7 @@ TEST(Singleton, SingletonEagerInitParallel) { for (size_t j = 0; j < kThreads; j++) { threads.push_back(std::make_shared([&] { barrier.wait(); - vault.doEagerInitVia(&exe).get(); + vault.doEagerInitVia(exe); })); } @@ -553,20 +554,6 @@ TEST(Singleton, SingletonEagerInitParallel) { } } -// Benchmarking a normal singleton vs a Meyers singleton vs a Folly -// singleton. Meyers are insanely fast, but (hopefully) Folly -// singletons are fast "enough." -int* getMeyersSingleton() { - static auto ret = new int(0); - return ret; -} - -int normal_singleton_value = 0; -int* getNormalSingleton() { - doNotOptimizeAway(&normal_singleton_value); - return &normal_singleton_value; -} - struct MockTag {}; template using SingletonMock = Singleton ; @@ -594,44 +581,6 @@ TEST(Singleton, MockTest) { EXPECT_NE(serial_count_first, serial_count_mock); } -struct BenchmarkSingleton { - int val = 0; -}; - -BENCHMARK(NormalSingleton, n) { - for (size_t i = 0; i < n; ++i) { - doNotOptimizeAway(getNormalSingleton()); - } -} - -BENCHMARK_RELATIVE(MeyersSingleton, n) { - for (size_t i = 0; i < n; ++i) { - doNotOptimizeAway(getMeyersSingleton()); - } -} - -struct BenchmarkTag {}; -template -using SingletonBenchmark = Singleton ; - -struct GetTag{}; -struct GetWeakTag{}; - -SingletonBenchmark benchmark_singleton_get; -SingletonBenchmark benchmark_singleton_get_weak; - -BENCHMARK_RELATIVE(FollySingleton, n) { - for (size_t i = 0; i < n; ++i) { - SingletonBenchmark::try_get(); - } -} - -BENCHMARK_RELATIVE(FollySingletonWeak, n) { - for (size_t i = 0; i < n; ++i) { - SingletonBenchmark::get_weak(); - } -} - int main(int argc, char* argv[]) { testing::InitGoogleTest(&argc, argv); google::InitGoogleLogging(argv[0]); @@ -640,8 +589,6 @@ int main(int argc, char* argv[]) { SingletonVault::singleton()->registrationComplete(); auto ret = RUN_ALL_TESTS(); - if (!ret) { - folly::runBenchmarksOnFlag(); - } + return ret; }