shared_memory.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 /*****************************************************************************
00014 ** Includes
00015 *****************************************************************************/
00016 
00017 #include <iostream>
00018 #include <cstdlib>
00019 #include <gtest/gtest.h>
00020 #include <ecl/exceptions/standard_exception.hpp>
00021 #include <ecl/time/sleep.hpp>
00022 #include "../../include/ecl/ipc/shared_memory.hpp"
00023 
00024 #ifdef ECL_HAS_SHARED_MEMORY
00025 // Should check for posix support too because we're using posix fork, but
00026 // posix is all we got for now, so don't care too much.
00027 
00028 /*****************************************************************************
00029 ** Using
00030 *****************************************************************************/
00031 
00032 using std::cout;
00033 using std::endl;
00034 using std::cerr;
00035 using std::string;
00036 using ecl::SharedMemory;
00037 using ecl::Sleep;
00038 
00039 /*****************************************************************************
00040 ** Doxygen
00041 *****************************************************************************/
00042 
00047 /*****************************************************************************
00048 ** Namespaces
00049 *****************************************************************************/
00050 
00051 namespace ecl {
00052 namespace ipc {
00053 namespace tests {
00054 
00055 /*****************************************************************************
00056 ** Data Storage Class
00057 *****************************************************************************/
00058 
00059 class Data {
00060     public:
00061         Data(double d1 = 0.0, double d2 = 0.0) {value[0] = d1; value[1] = d2;}
00062         double value[2];
00063 };
00064 
00065 } // namespace tests
00066 } // namespace ipc
00067 } // namespace ecl
00068 
00069 /*****************************************************************************
00070 ** Using
00071 *****************************************************************************/
00072 
00073 using ecl::ipc::tests::Data;
00074 
00075 /*****************************************************************************
00076 ** Doxygen
00077 *****************************************************************************/
00078 
00083 /*****************************************************************************
00084 ** Tests
00085 *****************************************************************************/
00086 
00087 TEST(SharedMemoryTests,access) {
00088 
00089         string name("shared_memory");
00090     Sleep sleep;
00091 
00092     pid_t pID = fork();
00093     if (pID == 0)
00094     {
00095         /*********************************************************************
00096         ** Child
00097         *********************************************************************/
00098         sleep(1);
00099         try {
00100             SharedMemory<Data> sm(name.c_str());
00101            Data *data = sm.data();
00102             EXPECT_EQ(1.3,data->value[0]);
00103             EXPECT_EQ(2.3,data->value[1]);
00104 //        cout << "Child read: " << data->value[0] << " " << data->value[1] << endl;
00105         } catch ( ecl::StandardException &e) {
00106             // Don't fail the test, hudson doesn't let us have permissions for instance.
00107             std::cout << e.what() << std::endl;
00108         }
00109     }
00110     else if (pID < 0)            // failed to fork
00111     {
00112         cerr << "Failed to fork" << endl;
00113         exit(1);
00114     }
00115     else
00116     {
00117         /*********************************************************************
00118         ** Parent
00119         *********************************************************************/
00120         try {
00121             SharedMemory<Data> sm(name.c_str());
00122             Data *data = sm.data();
00123             data->value[0] = 1.3;
00124             data->value[1] = 2.3;
00125             EXPECT_EQ(1.3,data->value[0]);
00126             EXPECT_EQ(2.3,data->value[1]);
00127 //        cout << "Parent wrote: " << data->value[0] << " " << data->value[1] << endl;
00128             sleep(4);
00129         } catch ( ecl::StandardException &e) {
00130             // Don't fail the test, hudson doesn't let us have permissions for instance.
00131             std::cout << e.what() << std::endl;
00132         }
00133     }
00134 }
00135 
00136 /*****************************************************************************
00137 ** Main program
00138 *****************************************************************************/
00139 
00140 int main(int argc, char **argv) {
00141 
00142     testing::InitGoogleTest(&argc,argv);
00143     return RUN_ALL_TESTS();
00144 }
00145 
00146 #else
00147 
00148 /*****************************************************************************
00149 ** Alternative Main
00150 *****************************************************************************/
00151 
00152 int main(int argc, char **argv) {
00153     std::cout << std::endl;
00154     std::cout << "Shared memory is not supported on this platform (or ecl is just lacking)." << std::endl;
00155     std::cout << std::endl;
00156     return 0;
00157 }
00158 
00159 #endif /* ECL_HAS_SHARED_MEMORY */
00160 
00161 
00162 


ecl_ipc
Author(s): Daniel Stonier
autogenerated on Mon Jul 3 2017 02:21:22