<commit_before>/*
 * Copyright (c) 2017 <NAME>
 *
 * wampcc is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include "test_common.h"

#include "wampcc/tcp_socket.h"
#include "wampcc/io_loop.h"

#include <stdexcept>

using namespace wampcc;
using namespace std;


void test_unused_socket()
{
  unique_ptr<kernel> the_kernel(new kernel({}, logger::nolog()));
  tcp_socket sock( the_kernel.get() );
}

void test_canonical_connect(int port)
{
  cout << "---------- test_canonical_connect ----------\n";
  unique_ptr<kernel> the_kernel(new kernel({}, logger::nolog()));

  {
    tcp_socket sock( the_kernel.get() );

    auto fut = sock.connect("127.0.0.1", port);

    std::future_status status = fut.wait_for(std::chrono::milliseconds(100));

    if (status != std::future_status::ready)
    {
      cout << "result not ready ... waiting\n";
      fut.wait();
    }

    cout << "got connect result\n";
    fut.get();

    if (sock.is_connected() == false)
      throw runtime_error("expected to be connected");
  }

  the_kernel.reset();
}


void test_future_and_socket_discarded(int port)
{
  cout << "---------- test_future_and_socket_discarded ----------\n";
  unique_ptr<kernel> the_kernel(new kernel({}, logger::nolog()));

  {
    tcp_socket sock( the_kernel.get() );
    auto fut = sock.connect("127.0.0.1", port);
  }

  {
    tcp_socket sock( the_kernel.get() );
    sock.connect("127.0.0.1", port);
  }

  {
    std::shared_ptr<tcp_socket> sp(new tcp_socket(the_kernel.get()));
    auto fut = sp->connect("127.0.0.1", port);
  }

  {
    std::shared_ptr<tcp_socket> sp(new tcp_socket(the_kernel.get()));
    sp->connect("127.0.0.1", port);
  }


  the_kernel.reset();
}



void test_future_and_socket_discarded_v2(int port)
{
  cout << "---------- test_future_and_socket_discarded_v2 ----------\n";
  unique_ptr<kernel> the_kernel( new kernel({}, logger::nolog() ) );
  {
    std::shared_ptr<tcp_socket> sp(new tcp_socket(the_kernel.get()));
    sp->connect("127.0.0.1", port);
  }
}


void test_connect_and_delete(int port)
{
  cout << "---------- test_connect_and_delete(1)----------\n";

  unique_ptr<kernel> the_kernel( new kernel({}, logger::nolog() ) );
  {
    tcp_socket my_socket_1(the_kernel.get());
    my_socket_1.connect("127.0.0.1", port);
    tcp_socket my_socket_2(the_kernel.get());
    my_socket_2.connect("127.0.0.1", port);
    tcp_socket my_socket_3(the_kernel.get());
    my_socket_3.connect("127.0.0.1", port);
  }

  cout << "---------- test_connect_and_delete(2) ----------\n";
  {
    std::shared_ptr<tcp_socket> sp_1(new tcp_socket(the_kernel.get()));
    sp_1->connect("127.0.0.1", port);
    std::shared_ptr<tcp_socket> sp_2(new tcp_socket(the_kernel.get()));
    sp_2->connect("127.0.0.1", port);
    std::shared_ptr<tcp_socket> sp_3(new tcp_socket(the_kernel.get()));
    sp_3->connect("127.0.0.1", port);
  }

}





void test_connect_read_close(int port)
{
  cout << "---------- test_connect_read_close ----------\n";

  unique_ptr<kernel> the_kernel( new kernel({}, logger::nolog() ) );

  socket_listener my_listener;

  {
    std::shared_ptr<tcp_socket> sp_1(new tcp_socket(the_kernel.get()));
    sp_1->connect("127.0.0.1", port);

    my_listener.start_listening( sp_1 );
    sp_1->close();
  }

}


void test_connect_then_io_stop(int port)
{
  cout << "---------- " << __FUNCTION__ << " ----------\n";

  unique_ptr<kernel> the_kernel( new kernel({}, logger::nolog() ) );

  socket_listener my_listener;

  {
    std::shared_ptr<tcp_socket> sp_1(new tcp_socket(the_kernel.get()));
    auto fut = sp_1->connect("127.0.0.1", port);
    my_listener.start_listening( sp_1 );
    fut.wait();
    the_kernel->get_io()->sync_stop();

    // deletion of the socket will proceed fine, because completion of the IO
    // loop implies all sockets have been closed.
    sp_1.reset();
  }

}

int main(int argc, char** argv)
{
  int starting_port_number = 23100;
  int port;

  if (argc>1)
    starting_port_number = atoi(argv[1]);

  auto all_tests = [](int port)
  {
    test_unused_socket();<commit_msg>fix socket usage<commit_after>/*
 * Copyright (c) 2017 <NAME>
 *
 * wampcc is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

#include "test_common.h"

#include "wampcc/tcp_socket.h"
#include "wampcc/io_loop.h"

#include <stdexcept>

using namespace wampcc;
using namespace std;


void test_unused_socket()
{
  unique_ptr<kernel> the_kernel(new kernel({}, logger::nolog()));
  tcp_socket sock( the_kernel.get() );
}

void test_canonical_connect(int port)
{
  cout << "---------- test_canonical_connect ----------\n";
  unique_ptr<kernel> the_kernel(new kernel({}, logger::nolog()));

  {
    tcp_socket sock( the_kernel.get() );

    auto fut = sock.connect("127.0.0.1", port);

    std::future_status status = fut.wait_for(std::chrono::milliseconds(100));

    if (status != std::future_status::ready)
    {
      cout << "result not ready ... waiting\n";
      fut.wait();
    }

    cout << "got connect result\n";
    fut.get();

    if (sock.is_connected() == false)
      throw runtime_error("expected to be connected");
  }

  the_kernel.reset();
}


void test_future_and_socket_discarded(int port)
{
  cout << "---------- test_future_and_socket_discarded ----------\n";
  unique_ptr<kernel> the_kernel(new kernel({}, logger::nolog()));

  {
    tcp_socket sock( the_kernel.get() );
    auto fut = sock.connect("127.0.0.1", port);
  }

  {
    tcp_socket sock( the_kernel.get() );
    sock.connect("127.0.0.1", port);
  }

  {
    std::shared_ptr<tcp_socket> sp(new tcp_socket(the_kernel.get()));
    auto fut = sp->connect("127.0.0.1", port);
  }

  {
    std::shared_ptr<tcp_socket> sp(new tcp_socket(the_kernel.get()));
    sp->connect("127.0.0.1", port);
  }


  the_kernel.reset();
}



void test_future_and_socket_discarded_v2(int port)
{
  cout << "---------- test_future_and_socket_discarded_v2 ----------\n";
  unique_ptr<kernel> the_kernel( new kernel({}, logger::nolog() ) );
  {
    std::shared_ptr<tcp_socket> sp(new tcp_socket(the_kernel.get()));
    sp->connect("127.0.0.1", port);
  }
}


void test_connect_and_delete(int port)
{
  cout << "---------- test_connect_and_delete(1)----------\n";

  unique_ptr<kernel> the_kernel( new kernel({}, logger::nolog() ) );
  {
    tcp_socket my_socket_1(the_kernel.get());
    my_socket_1.connect("127.0.0.1", port);
    tcp_socket my_socket_2(the_kernel.get());
    my_socket_2.connect("127.0.0.1", port);
    tcp_socket my_socket_3(the_kernel.get());
    my_socket_3.connect("127.0.0.1", port);
  }

  cout << "---------- test_connect_and_delete(2) ----------\n";
  {
    std::shared_ptr<tcp_socket> sp_1(new tcp_socket(the_kernel.get()));
    sp_1->connect("127.0.0.1", port);
    std::shared_ptr<tcp_socket> sp_2(new tcp_socket(the_kernel.get()));
    sp_2->connect("127.0.0.1", port);
    std::shared_ptr<tcp_socket> sp_3(new tcp_socket(the_kernel.get()));
    sp_3->connect("127.0.0.1", port);
  }

}





void test_connect_read_close(int port)
{
  cout << "---------- test_connect_read_close ----------\n";

  unique_ptr<kernel> the_kernel( new kernel({}, logger::nolog() ) );

  socket_listener my_listener;

  {
    std::shared_ptr<tcp_socket> sp_1(new tcp_socket(the_kernel.get()));
    sp_1->connect("127.0.0.1", port).wait();
    my_listener.start_listening( sp_1 );
    sp_1->close();
  }

}


void test_connect_then_io_stop(int port)
{
  cout << "---------- " << __FUNCTION__ << " ----------\n";

  unique_ptr<kernel> the_kernel( new kernel({}, logger::nolog() ) );

  socket_listener my_listener;

  {
    std::shared_ptr<tcp_socket> sp_1(new tcp_socket(the_kernel.get()));
    auto fut = sp_1->connect("127.0.0.1", port);
    fut.wait();
    my_listener.start_listening( sp_1 );
    the_kernel->get_io()->sync_stop();

    // deletion of the socket will proceed fine, because completion of the IO
    // loop implies all sockets have been closed.
    sp_1.reset();
  }

}

int main(int argc, char** argv)
{
  int starting_port_number = 23100;
  int port;

  if (argc>1)
    starting_port_number = atoi(argv[1]);

  auto all_tests = [](int port)
  {
    test_unused_socket();
{'fraction_non_alphanumeric': 0.19355229669131013, 'fraction_numerical': 0.02957217306993092, 'mean_word_length': 3.3408732246186217, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 28, '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': '7297443', 'n_tokens_mistral': 3102, 'n_tokens_neox': 2952, 'n_words': 537}