00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef TEST_FUTURE_H
00024 #define TEST_FUTURE_H
00025
00026 #include <cppunit/extensions/HelperMacros.h>
00027 #include <cppunit/TestAssert.h>
00028 #include "baseTest.h"
00029
00030 #include <amino/cstdatomic>
00031 #include <amino/thread.h>
00032 #include <amino/future.h>
00033 #include <amino/ftask.h>
00034
00035 #include <iostream>
00036 #include <assert.h>
00037 #include <unistd.h>
00038
00039 namespace test{
00040 using namespace amino;
00041
00042 const int ACCU_COUNT=1000;
00043
00044 class Accumulate:public Runnable{
00045 private:
00046 atomic<int> * count;
00047 public:
00048 Accumulate(atomic<int> * arg){
00049 count = arg;
00050 }
00051
00052 void* run(){
00053 for(int i=0;i<ACCU_COUNT;i++)
00054 (*count)++;
00055 return NULL;
00056 }
00057 };
00058
00059 template<typename Executor>
00060 class FutureTest :
00061 public CppUnit::TestFixture, public BaseTest<int> {
00062 CPPUNIT_TEST_SUITE(FutureTest);
00063 CPPUNIT_TEST(testAccumulateMassive);
00064 CPPUNIT_TEST(testAccumulateMassive_another);
00065 CPPUNIT_TEST_SUITE_END();
00066
00067 public:
00068 FutureTest() {
00069 }
00070
00071 void setUp() {
00072 }
00073
00074 void reset() {
00075 }
00076
00077 void tearDown() {
00078 }
00079
00080 void testAccumulateMassive(){
00081 const int TASK_COUNT=200;
00082 FutureTask ** futures= new FutureTask*[TASK_COUNT];
00083 Executor executor;
00084 atomic<int> sum;
00085 sum = 0;
00086
00087 Accumulate * ppAcc[TASK_COUNT];
00088 for(int i=0;i<TASK_COUNT;i++){
00089 ppAcc[i] = new Accumulate(&sum);
00090 futures[i] = new FutureTask(ppAcc[i]);
00091 }
00092
00093 for(int i=0;i<TASK_COUNT;i++){
00094 executor.execute(futures[i]);
00095 }
00096
00097 for(int i=0;i<TASK_COUNT;i++){
00098 futures[i]->get();
00099 }
00100
00101
00102 for(int i=0;i<TASK_COUNT;i++){
00103 executor.execute(futures[i]);
00104 }
00105
00106 executor.shutdown();
00107 executor.waitTermination();
00108
00109 CPPUNIT_ASSERT(sum.load()==2*ACCU_COUNT*TASK_COUNT);
00110 for(int i=0;i<TASK_COUNT;i++){
00111 delete ppAcc[i];
00112 delete futures[i];
00113 }
00114 delete [] futures;
00115 }
00116
00117 void testAccumulateMassive_another(){
00118 const int TASK_COUNT=200;
00119 FutureTask ** futures= new FutureTask*[TASK_COUNT];
00120 Executor executor;
00121 atomic<int> sum;
00122 sum = 0;
00123
00124 Accumulate * ppAcc[TASK_COUNT];
00125 for(int i=0;i<TASK_COUNT;i++){
00126 ppAcc[i] = new Accumulate(&sum);
00127 futures[i] = new FutureTask(ppAcc[i]);
00128 }
00129
00130 for(int i=0;i<TASK_COUNT;i++){
00131 executor.execute(futures[i]);
00132 }
00133
00134 for(int i=0;i<TASK_COUNT;i++){
00135 futures[i]->get(2000);
00136 }
00137
00138
00139 for(int i=0;i<TASK_COUNT;i++){
00140 executor.execute(futures[i]);
00141 }
00142
00143 executor.shutdown();
00144 executor.waitTermination();
00145
00146 CPPUNIT_ASSERT(sum.load()==2*ACCU_COUNT*TASK_COUNT);
00147 for(int i=0;i<TASK_COUNT;i++){
00148 delete ppAcc[i];
00149 delete futures[i];
00150 }
00151 delete [] futures;
00152 }
00153 };
00154 }
00155 #endif