/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

/**
* @file
* @copyright 2022 MPLv2
*/

/**
* @defgroup tests
*/


#include "WHISPER/InternalUDPSender.hpp"
#include "WHISPER/Messages/stringData.hpp"
#include <string>
#define CATCH_CONFIG_MAIN

#include <catch2/catch.hpp>

#include "WHISPER/InternalUDPListener.hpp"
#include "WHISPER/Messages/Message.hpp"
#include "zmq.hpp"
    #include "google/protobuf/message.h"


#include <memory>

/**
* @brief brief test description
* @ingroup group
*/
SCENARIO("A test scenario","[keywords]")
{
  GIVEN("Preliminaries")
  {
    auto *listener = new WHISPER::InternalUDPListener(8000);
    auto sender = std::make_shared<WHISPER::InternalUDPSender>("127.0.0.1",8000);

    WHEN("doing something")
    { 
    auto receiver = std::make_shared<WHISPER::threadSafeQueue<WHISPER::Message>>();
    listener->connect(receiver) ;
    listener->subscribe(WHISPER::DATA);
    // listener->start();
    // listener->stop();
    auto stringMsg = WHISPER::StringData(1,1,WHISPER::SourceType::SENSOR,"hello world");
    sender->send(stringMsg,WHISPER::DATA);
    LOG_S(INFO)<<"wating";
    std::this_thread::sleep_for(std::chrono::milliseconds(200));
      
      
    LOG_S(INFO)<<"closing";

      THEN("expecting something to happen")
      {
        REQUIRE(receiver->size() > 0);
        WHISPER::Message msg;
        receiver->get(msg);
        LOG_S(INFO)<< msg.msgType_;
        WHISPER::StringData data(msg.serialize());      
        REQUIRE(data.data_ == "hello world");

        delete listener;
        REQUIRE(receiver.unique() == true );
      } // THEN
    } // WHEN
  } // GIVEN
} // SCENARIO
