<commit_before>/*
 * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
 */

#include "bgp/bgp_server.h"

#include "base/logging.h"
#include "base/task.h"
#include "base/test/task_test_util.h"
#include "bgp/bgp_config.h"
#include "bgp/bgp_config_parser.h"
#include "bgp/bgp_factory.h"
#include "bgp/bgp_peer.h"
#include "bgp/bgp_sandesh.h"
#include "bgp/bgp_session_manager.h"
#include "bgp/inet/inet_table.h"
#include "bgp/test/bgp_server_test_util.h"
#include "control-node/control_node.h"
#include "io/test/event_manager_test.h"
#include "testing/gunit.h"

using namespace boost::asio;
using namespace std;

//
// Fire state machine timers faster and reduce possible delay in this test
//
class StateMachineTest : public StateMachine {
    public:
        static bool hold_time_msec_;
        explicit StateMachineTest(BgpPeer *peer) : StateMachine(peer) { }
        ~StateMachineTest() { }

        void StartConnectTimer(int seconds) {
            connect_timer_->Start(10,
                boost::bind(&StateMachine::ConnectTimerExpired, this),
                boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
        }

        void StartOpenTimer(int seconds) {
            open_timer_->Start(10,
                boost::bind(&StateMachine::OpenTimerExpired, this),
                boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
        }

        void StartIdleHoldTimer() {
            if (idle_hold_time_ <= 0)
                return;

            idle_hold_timer_->Start(10,
                boost::bind(&StateMachine::IdleHoldTimerExpired, this),
                boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
        }

        virtual int hold_time_msecs() const { return hold_time_msec_ /* msec */; }
};

bool StateMachineTest::hold_time_msec_ = StateMachine::GetDefaultHoldTime() * 1000;

class BgpServerUnitTest : public ::testing::Test {
protected:
    virtual void SetUp() {
        evm_.reset(new EventManager());
        a_.reset(new BgpServerTest(evm_.get(), "A"));
        b_.reset(new BgpServerTest(evm_.get(), "B"));
        thread_.reset(new ServerThread(evm_.get()));

        a_->session_manager()->Initialize(0);
        BGP_DEBUG_UT("Created server at port: " <<
            a_->session_manager()->GetPort());
        b_->session_manager()->Initialize(0);
        BGP_DEBUG_UT("Created server at port: " <<
            b_->session_manager()->GetPort());
        thread_->Start();<commit_msg>Set test mode to get ClearNeighbor tests to pass.<commit_after>/*
 * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
 */

#include "bgp/bgp_server.h"

#include "base/logging.h"
#include "base/task.h"
#include "base/test/task_test_util.h"
#include "bgp/bgp_config.h"
#include "bgp/bgp_config_parser.h"
#include "bgp/bgp_factory.h"
#include "bgp/bgp_peer.h"
#include "bgp/bgp_sandesh.h"
#include "bgp/bgp_session_manager.h"
#include "bgp/inet/inet_table.h"
#include "bgp/test/bgp_server_test_util.h"
#include "control-node/control_node.h"
#include "io/test/event_manager_test.h"
#include "testing/gunit.h"

using namespace boost::asio;
using namespace std;

//
// Fire state machine timers faster and reduce possible delay in this test
//
class StateMachineTest : public StateMachine {
    public:
        static bool hold_time_msec_;
        explicit StateMachineTest(BgpPeer *peer) : StateMachine(peer) { }
        ~StateMachineTest() { }

        void StartConnectTimer(int seconds) {
            connect_timer_->Start(10,
                boost::bind(&StateMachine::ConnectTimerExpired, this),
                boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
        }

        void StartOpenTimer(int seconds) {
            open_timer_->Start(10,
                boost::bind(&StateMachine::OpenTimerExpired, this),
                boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
        }

        void StartIdleHoldTimer() {
            if (idle_hold_time_ <= 0)
                return;

            idle_hold_timer_->Start(10,
                boost::bind(&StateMachine::IdleHoldTimerExpired, this),
                boost::bind(&StateMachine::TimerErrorHanlder, this, _1, _2));
        }

        virtual int hold_time_msecs() const { return hold_time_msec_ /* msec */; }
};

bool StateMachineTest::hold_time_msec_ = StateMachine::GetDefaultHoldTime() * 1000;

class BgpServerUnitTest : public ::testing::Test {
protected:
    BgpServerUnitTest() {
        ControlNode::SetTestMode(true);
    }

    virtual void SetUp() {
        evm_.reset(new EventManager());
        a_.reset(new BgpServerTest(evm_.get(), "A"));
        b_.reset(new BgpServerTest(evm_.get(), "B"));
        thread_.reset(new ServerThread(evm_.get()));

        a_->session_manager()->Initialize(0);
        BGP_DEBUG_UT("Created server at port: " <<
            a_->session_manager()->GetPort());
        b_->session_manager()->Initialize(0);
        BGP_DEBUG_UT("Created server at port: " <<
            b_->session_manager()->GetPort());
        thread_->Start();
{'fraction_non_alphanumeric': 0.13486256673917343, 'fraction_numerical': 0.009096302155428119, 'mean_word_length': 3.0955465587044535, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 27, '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': '5301748', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1585, 'n_words': 301}