ResourceTest.cpp

Go to the documentation of this file.
00001 /*
00002 // $Id: //open/dev/fennel/test/ResourceTest.cpp#10 $
00003 // Fennel is a library of data storage and processing components.
00004 // Copyright (C) 2005-2009 The Eigenbase Project
00005 // Copyright (C) 2005-2009 SQLstream, Inc.
00006 // Copyright (C) 2005-2009 LucidEra, Inc.
00007 // Portions Copyright (C) 1999-2009 John V. Sichi
00008 //
00009 // This program is free software; you can redistribute it and/or modify it
00010 // under the terms of the GNU General Public License as published by the Free
00011 // Software Foundation; either version 2 of the License, or (at your option)
00012 // any later version approved by The Eigenbase Project.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 */
00023 
00024 #include "fennel/common/CommonPreamble.h"
00025 #include "fennel/test/TestBase.h"
00026 #include "fennel/common/FennelResource.h"
00027 #include "fennel/synch/Thread.h"
00028 
00029 #include <boost/test/test_tools.hpp>
00030 #include <boost/thread/barrier.hpp>
00031 
00032 #include <vector>
00033 
00034 using namespace fennel;
00035 
00036 class ResourceTest : virtual public TestBase
00037 {
00038 public:
00039 
00040     explicit ResourceTest()
00041     {
00042         FENNEL_UNIT_TEST_CASE(ResourceTest, testEnUsLocale);
00043         FENNEL_UNIT_TEST_CASE(ResourceTest, testConcurrency);
00044     }
00045 
00046     void testEnUsLocale();
00047     void testConcurrency();
00048 };
00049 
00050 void ResourceTest::testEnUsLocale()
00051 {
00052     Locale locale("en","US");
00053     std::string actual =
00054         FennelResource::instance(locale).sysCallFailed("swizzle");
00055     std::string expected = "System call failed:  swizzle";
00056     BOOST_CHECK_EQUAL(expected,actual);
00057 }
00058 
00059 class ResourceThread : public Thread
00060 {
00061 private:
00062     boost::barrier &barrier;
00063 
00064     int count;
00065     int completed;
00066 
00067     std::vector<std::string> variants;
00068 
00069 public:
00070     explicit ResourceThread(
00071         std::string desc, boost::barrier &barrier, int count)
00072         : Thread(desc), barrier(barrier), count(count), completed(0)
00073     {
00074         for (int i = 0; i < count; i++) {
00075             std::stringstream ss;
00076             ss << "var_" << (i + 1);
00077             variants.push_back(ss.str());
00078         }
00079     }
00080 
00081     virtual ~ResourceThread()
00082     {
00083     }
00084 
00085     int getCompleted()
00086     {
00087         return completed;
00088     }
00089 
00090     virtual void run()
00091     {
00092         try {
00093             barrier.wait();
00094 
00095             for (int i = 0; i < count; i++) {
00096                 std::string &variant = variants[i];
00097 
00098                 Locale locale("en", "US", variant);
00099 
00100                 FennelResource::instance(locale).sysCallFailed(variant);
00101 
00102                 completed++;
00103             }
00104         } catch (...) {
00105             completed = -1;
00106         }
00107     }
00108 };
00109 
00110 #define CONC_ITER (1000)
00111 
00112 // Test thread sync bug fix (change 3930).  Note that the bug only seems
00113 // to occur if two threads are attempting to create a ResourceBundle for
00114 // the same Locale at the same time.
00115 void ResourceTest::testConcurrency()
00116 {
00117     boost::barrier barrier(2);
00118 
00119     ResourceThread thread1("resThread1", barrier, CONC_ITER);
00120     ResourceThread thread2("resThread2", barrier, CONC_ITER);
00121 
00122     thread1.start();
00123     thread2.start();
00124 
00125     thread1.join();
00126     thread2.join();
00127 
00128     BOOST_CHECK_EQUAL(thread1.getCompleted(), CONC_ITER);
00129     BOOST_CHECK_EQUAL(thread2.getCompleted(), CONC_ITER);
00130 }
00131 
00132 FENNEL_UNIT_TEST_SUITE(ResourceTest);
00133 
00134 // End ResourceTest.cpp
00135 

Generated on Mon Jun 22 04:00:21 2009 for Fennel by  doxygen 1.5.1