Added support to run unit tests in a multithreaded context

- This is controlled by specifying the 'mtstress' argument when running
  `unit_test`.
- The goal is to detect if the operator/transformation  fails in this
  context.
- In this mode, the test will be executed 5'000 times in 50 threads
  concurrently.
- Allocation & initialization of the operator/transformation is
  performed once in the main thread, while the evaluation is executed in
  the threads.
  - This is consistent with the library's support for multithreading,
    where initialization and loading of rules is expected to run once.
    See issue #3215.
This commit is contained in:
Eduardo Arias
2024-08-09 06:54:35 -07:00
parent 7bdc3c825c
commit ee5f95eb04
5 changed files with 165 additions and 69 deletions

View File

@@ -93,13 +93,13 @@ bool ModSecurityTest<T>::load_test_json(const std::string &file) {
template <class T>
std::pair<std::string, std::vector<T *>>*
void
ModSecurityTest<T>::load_tests(const std::string &path) {
DIR *dir;
struct dirent *ent;
struct stat buffer;
if ((dir = opendir(path.c_str())) == NULL) {
if ((dir = opendir(path.c_str())) == nullptr) {
/* if target is a file, use it as a single test. */
if (stat(path.c_str(), &buffer) == 0) {
if (load_test_json(path) == false) {
@@ -107,10 +107,10 @@ ModSecurityTest<T>::load_tests(const std::string &path) {
std::cout << std::endl;
}
}
return NULL;
return;
}
while ((ent = readdir(dir)) != NULL) {
while ((ent = readdir(dir)) != nullptr) {
std::string filename = ent->d_name;
std::string json = ".json";
if (filename.size() < json.size()
@@ -123,16 +123,15 @@ ModSecurityTest<T>::load_tests(const std::string &path) {
}
}
closedir(dir);
return NULL;
}
template <class T>
std::pair<std::string, std::vector<T *>>* ModSecurityTest<T>::load_tests() {
return load_tests(this->target);
void ModSecurityTest<T>::load_tests() {
load_tests(this->target);
}
template <class T>
void ModSecurityTest<T>::cmd_options(int argc, char **argv) {
int i = 1;
@@ -144,6 +143,10 @@ void ModSecurityTest<T>::cmd_options(int argc, char **argv) {
i++;
m_count_all = true;
}
if (argc > i && strcmp(argv[i], "mtstress") == 0) {
i++;
m_test_multithreaded = true;
}
if (std::getenv("AUTOMAKE_TESTS")) {
m_automake_output = true;
}