Commit f923e648 authored by Pierre Aubert's avatar Pierre Aubert
Browse files

Add test

parent 8fb84760
Pipeline #98192 passed with stages
in 1 minute and 19 seconds
project(Phoenix) project(Phoenix)
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
add_subdirectory(TEST_BASE_NOTIFY)
add_subdirectory(TEST_BASE_NOTIFY_2)
project(Phoenix)
cmake_minimum_required(VERSION 2.8)
add_definitions(-DCURRENT_TEST_DIR="${CMAKE_CURRENT_BINARY_DIR}")
add_executable(test_base_notify main.cpp)
target_link_libraries(test_base_notify phoenix_notify pthread)
add_test(NAME TestBaseNotify
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_base_notify)
/***************************************
Auteur : Pierre Aubert
Mail : aubertp7@gmail.com
Licence : CeCILL-C
****************************************/
#include <unistd.h>
#include <sys/stat.h>
#include <thread>
#include <fstream>
#include <sstream>
#include <iostream>
#include "PNotify.h"
///Test the PNotify
/** @param[out] b : true on success, false otherwise
*/
void testPNotify(bool & b, const std::string & inputDir){
b = true;
PNotify nodify;
nodify.resize(100lu); //Let's have 100 files in buffer
nodify.setWatch(inputDir, IN_NONBLOCK, IN_CLOSE_WRITE);
size_t nbCheck(5lu);
for(size_t i(0lu); i < nbCheck; ++i){
std::vector<std::string> vecNewFile(nodify.scanFileName());
std::cout << "testPNotify check "<<i<<" : find " << vecNewFile.size() << " new files" << std::endl;
if(vecNewFile.size() != 0lu){
for(std::vector<std::string>::iterator it(vecNewFile.begin()); it != vecNewFile.end(); ++it){
std::cout << "\t" << (*it) << std::endl;
}
}
usleep(800000);
}
}
///Create test file in the input directory
/** @param inputDir : create files in the given directory
*/
void testCreateFile(const std::string & inputDir){
size_t nbFile(10lu);
for(size_t i(0lu); i < nbFile; ++i){
std::stringstream fileName;
fileName << inputDir << "/file_" << i << ".txt";
std::ofstream fs;
fs.open(fileName.str());
if(fs.is_open()){
fs << "Some text for file " << i << std::endl;
}
usleep(500000);
}
}
int main(int argc, char** argv){
std::string inputDir(CURRENT_TEST_DIR);
bool okThreadPNotify(true);
std::thread thrRecv0(testPNotify, std::ref(okThreadPNotify), inputDir);
std::thread thrSend(testCreateFile, inputDir);
thrRecv0.join();
thrSend.join();
std::cout << "main : okThreadPNotify : " << okThreadPNotify << std::endl;
bool b(okThreadPNotify);
return b - 1;
}
project(Phoenix)
cmake_minimum_required(VERSION 2.8)
add_definitions(-DCURRENT_TEST_DIR="${CMAKE_CURRENT_BINARY_DIR}")
add_executable(test_base_notify2 main.cpp)
target_link_libraries(test_base_notify2 phoenix_notify pthread)
add_test(NAME TestBaseNotify2
COMMAND ${CMAKE_CURRENT_BINARY_DIR}/test_base_notify2)
/***************************************
Auteur : Pierre Aubert
Mail : aubertp7@gmail.com
Licence : CeCILL-C
****************************************/
#include <unistd.h>
#include <sys/stat.h>
#include <thread>
#include <fstream>
#include <sstream>
#include <iostream>
#include "PNotify.h"
///Test the PNotify
/** @param[out] b : true on success, false otherwise
*/
void testPNotify(bool & b, const std::string & inputDir){
b = true;
PNotify nodify;
nodify.setFlags(IN_NONBLOCK);
nodify.resize(100lu); //Let's have 100 files in buffer
nodify.addWatch(inputDir, IN_CLOSE_WRITE);
size_t nbCheck(5lu);
for(size_t i(0lu); i < nbCheck; ++i){
std::vector<std::string> vecNewFile(nodify.scanFileName());
std::cout << "testPNotify check "<<i<<" : find " << vecNewFile.size() << " new files" << std::endl;
if(vecNewFile.size() != 0lu){
for(std::vector<std::string>::iterator it(vecNewFile.begin()); it != vecNewFile.end(); ++it){
std::cout << "\t" << (*it) << std::endl;
}
}
usleep(800000);
}
}
///Create test file in the input directory
/** @param inputDir : create files in the given directory
*/
void testCreateFile(const std::string & inputDir){
size_t nbFile(10lu);
for(size_t i(0lu); i < nbFile; ++i){
std::stringstream fileName;
fileName << inputDir << "/file_" << i << ".txt";
std::ofstream fs;
fs.open(fileName.str());
if(fs.is_open()){
fs << "Some text for file " << i << std::endl;
}
usleep(500000);
}
}
int main(int argc, char** argv){
std::string inputDir(CURRENT_TEST_DIR);
bool okThreadPNotify(true);
std::thread thrRecv0(testPNotify, std::ref(okThreadPNotify), inputDir);
std::thread thrSend(testCreateFile, inputDir);
thrRecv0.join();
thrSend.join();
std::cout << "main : okThreadPNotify : " << okThreadPNotify << std::endl;
bool b(okThreadPNotify);
return b - 1;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment