<commit_before>#include <atomic>
#include <chrono>
#include <thread>

#include "gtest/gtest.h"

#include "drake/systems/lcm/lcm_receive_thread.h"
#include "drake/systems/lcm/lcm_subscriber_system.h"
#include "drake/systems/lcm/translator_between_lcmt_drake_signal.h"
#include "drake/lcmt_drake_signal.hpp"

namespace drake {
namespace systems {
namespace lcm {
namespace {

const int kDim = 10;
const int64_t kTimestamp = 123456;

/**
 * Periodically publishes an LCM message of type `drake::lcmt_drake_signal`.
 */
class MessagePublisher {
 public:
  MessagePublisher(const std::string& channel_name, ::lcm::LCM* lcm)
      : channel_name_(channel_name), lcm_(lcm) {
    message_.dim = kDim;
    message_.val.resize(kDim);
    message_.coord.resize(kDim);
    for (int ii = 0; ii < kDim; ++ii) {
      message_.val[ii] = ii;
      message_.coord[ii] = "coord_" + std::to_string(ii);
    }
    message_.timestamp = kTimestamp;
  }

  void Start() {
    thread_.reset(new std::thread(&MessagePublisher::DoPublish, this));
  }

  void Stop() {
    stop_ = true;
    thread_->join();
  }

 private:
  void DoPublish() {
    while (!stop_) {
      lcm_->publish(channel_name_, &message_);
      std::this_thread::sleep_for(std::chrono::milliseconds(200));
    }
  }

  const std::string channel_name_;

  ::lcm::LCM* lcm_;

  drake::lcmt_drake_signal message_;

  std::atomic<bool> stop_{false};

  std::unique_ptr<std::thread> thread_;
};

void TestSubscriber(::lcm::LCM* lcm, const std::string& channel_name,
                    LcmSubscriberSystem* dut) {<commit_msg>Avoid bad std::terminate error reporting<commit_after>#include <atomic>
#include <chrono>
#include <thread>

#include "gtest/gtest.h"

#include "drake/systems/lcm/lcm_receive_thread.h"
#include "drake/systems/lcm/lcm_subscriber_system.h"
#include "drake/systems/lcm/translator_between_lcmt_drake_signal.h"
#include "drake/lcmt_drake_signal.hpp"

namespace drake {
namespace systems {
namespace lcm {
namespace {

const int kDim = 10;
const int64_t kTimestamp = 123456;

/**
 * Periodically publishes an LCM message of type `drake::lcmt_drake_signal`.
 */
class MessagePublisher {
 public:
  MessagePublisher(const std::string& channel_name, ::lcm::LCM* lcm)
      : channel_name_(channel_name), lcm_(lcm) {
    message_.dim = kDim;
    message_.val.resize(kDim);
    message_.coord.resize(kDim);
    for (int ii = 0; ii < kDim; ++ii) {
      message_.val[ii] = ii;
      message_.coord[ii] = "coord_" + std::to_string(ii);
    }
    message_.timestamp = kTimestamp;
  }

  ~MessagePublisher() {
    EXPECT_TRUE(stop_);
    // Test cases are required to call Stop() before completing, but sometimes
    // fail to do so (e.g., if the test case raised an unexpected exception).
    // If that happens, we need to join the thread here, or else its destructor
    // will fail and confuse the gtest reporting of the earlier failures.
    if (!stop_) {
      Stop();
    }
  }

  void Start() {
    thread_.reset(new std::thread(&MessagePublisher::DoPublish, this));
  }

  void Stop() {
    stop_ = true;
    thread_->join();
  }

 private:
  void DoPublish() {
    while (!stop_) {
      lcm_->publish(channel_name_, &message_);
      std::this_thread::sleep_for(std::chrono::milliseconds(200));
    }
  }

  const std::string channel_name_;

  ::lcm::LCM* lcm_;

  drake::lcmt_drake_signal message_;

  std::atomic<bool> stop_{false};

  std::unique_ptr<std::thread> thread_;
};

void TestSubscriber(::lcm::LCM* lcm, const std::string& channel_name,
                    LcmSubscriberSystem* dut) {
{'fraction_non_alphanumeric': 0.13704015725919685, 'fraction_numerical': 0.007862959842740803, 'mean_word_length': 3.90633608815427, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3396389', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1230, 'n_words': 266}