00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
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
00113
00114
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
00135