<commit_before>/*
** Copyright 2011 Merethis
** This file is part of Centreon Broker.
**
** Centreon Broker is free software: you can redistribute it and/or
** modify it under the terms of the GNU General Public License version 2
** as published by the Free Software Foundation.
**
** Centreon Broker is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Centreon Broker. If not, see
** <http://www.gnu.org/licenses/>.
*/

#include <QDir>
#include <QRegExp>
#include <QString>
#include <QThreadPool>
#include <QtCore>
#include "com/centreon/broker/logging/file.hh"
#include "com/centreon/broker/logging/logging.hh"

using namespace com::centreon::broker;

#define MESSAGE "0123456789abcdefghijklmnopqrstuvwxyz"
#define TEMP_FILE "centreon_broker_unit_test.tmp"
#define THREAD_COUNT 50
#define WRITE_COUNT 200

/**
 *  Log some messages.
 */
static void log_messages() {
  for (unsigned int i = 0; i < WRITE_COUNT; ++i)
    logging::error << logging::HIGH << MESSAGE;
  return ;
}

/**
 *  Check that logging is thread-safe.
 *
 *  @return 0 on success.
 */
int main() {
  // Build filename.
  QString filename(QDir::tempPath());
  filename.append("/" TEMP_FILE);

  // Remove old file.
  QFile::remove(filename);

  // Initialize file backend.
  logging::file::with_timestamp(true);
  QSharedPointer<logging::file> backend(new logging::file(filename));

  // Add backend to logging list.
  logging::log_on(backend);

  // Reserve threads.
  for (unsigned int i = 0; i < THREAD_COUNT; ++i)
    QThreadPool::globalInstance()->reserveThread();

  // Run tasks.
  for (unsigned int i = 0; i < THREAD_COUNT; ++i)
    QtConcurrent::run(log_messages);

  // Wait for tasks to finish.
  QThreadPool::globalInstance()->waitForDone();

  // Remove backend from logging.
  logging::log_on(backend, 0, logging::NONE);
  backend.clear();

  // Release threads.
  QThreadPool::globalInstance()->releaseThread();

  // Return value.
  int retval(0);

  // Message regexp.
  QRegExp regex(".*" MESSAGE ".*");

  // Read file.
  QFile f(filename);
  retval |= !f.open(QIODevice::ReadOnly);
  if (!retval) {
    f.readLine();
    for (unsigned int i = 0;
         !retval && (i < THREAD_COUNT * WRITE_COUNT);
         ++i)
      retval |= (regex.indexIn(f.readLine()) == -1);
    f.readLine();
    retval |= !f.readLine().isEmpty();
  }

  return (retval);<commit_msg>Fix in one unit test (missing cleanup).<commit_after>/*
** Copyright 2011 Merethis
** This file is part of Centreon Broker.
**
** Centreon Broker is free software: you can redistribute it and/or
** modify it under the terms of the GNU General Public License version 2
** as published by the Free Software Foundation.
**
** Centreon Broker is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Centreon Broker. If not, see
** <http://www.gnu.org/licenses/>.
*/

#include <QDir>
#include <QRegExp>
#include <QString>
#include <QThreadPool>
#include <QtCore>
#include "com/centreon/broker/logging/file.hh"
#include "com/centreon/broker/logging/logging.hh"

using namespace com::centreon::broker;

#define MESSAGE "0123456789abcdefghijklmnopqrstuvwxyz"
#define TEMP_FILE "centreon_broker_unit_test.tmp"
#define THREAD_COUNT 50
#define WRITE_COUNT 200

/**
 *  Log some messages.
 */
static void log_messages() {
  for (unsigned int i = 0; i < WRITE_COUNT; ++i)
    logging::error << logging::HIGH << MESSAGE;
  return ;
}

/**
 *  Check that logging is thread-safe.
 *
 *  @return 0 on success.
 */
int main() {
  // Build filename.
  QString filename(QDir::tempPath());
  filename.append("/" TEMP_FILE);

  // Remove old file.
  QFile::remove(filename);

  // Initialize file backend.
  logging::file::with_timestamp(true);
  QSharedPointer<logging::file> backend(new logging::file(filename));

  // Add backend to logging list.
  logging::log_on(backend);

  // Reserve threads.
  for (unsigned int i = 0; i < THREAD_COUNT; ++i)
    QThreadPool::globalInstance()->reserveThread();

  // Run tasks.
  for (unsigned int i = 0; i < THREAD_COUNT; ++i)
    QtConcurrent::run(log_messages);

  // Wait for tasks to finish.
  QThreadPool::globalInstance()->waitForDone();

  // Remove backend from logging.
  logging::log_on(backend, 0, logging::NONE);
  backend.clear();

  // Release threads.
  QThreadPool::globalInstance()->releaseThread();

  // Return value.
  int retval(0);

  // Message regexp.
  QRegExp regex(".*" MESSAGE ".*");

  // Read file.
  QFile f(filename);
  retval |= !f.open(QIODevice::ReadOnly);
  if (!retval) {
    f.readLine();
    for (unsigned int i = 0;
         !retval && (i < THREAD_COUNT * WRITE_COUNT);
         ++i)
      retval |= (regex.indexIn(f.readLine()) == -1);
    f.readLine();
    retval |= !f.readLine().isEmpty();
  }

  // Remove temp file.
  QFile::remove(filename);

  return (retval);
{'fraction_non_alphanumeric': 0.1398377052274014, 'fraction_numerical': 0.010568031704095112, 'mean_word_length': 3.787714543812105, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4877968', 'n_tokens_mistral': 1744, 'n_tokens_neox': 1664, 'n_words': 519}