<commit_before>#include "tests.h"

using namespace swoole;

TEST(coroutine, create)
{
    long cid = Coroutine::create([](void *arg)
    {
        long cid = Coroutine::get_current_cid();
        Coroutine *co = Coroutine::get_by_cid(cid);
        co->yield();
    });
    ASSERT_GT(cid, 0);
    Coroutine::get_by_cid(cid)->resume();
}

TEST(coroutine, socket_connect_refused)
{
    coro_test([](void *arg)
    {
        Socket sock(SW_SOCK_TCP);
        bool retval = sock.connect("127.0.0.1", 9801, 0.5);
        ASSERT_EQ(retval, false);
        ASSERT_EQ(sock.errCode, ECONNREFUSED);
    });
}

TEST(coroutine, socket_connect_timeout)
{
    coro_test([](void *arg)
    {
        Socket sock(SW_SOCK_TCP);
        sock.set_timeout(0.5);
        bool retval = sock.connect("192.0.0.1", 9801);
        ASSERT_EQ(retval, false);
        ASSERT_EQ(sock.errCode, ETIMEDOUT);
    });
}

TEST(coroutine, socket_connect_with_dns)
{
    coro_test([](void *arg)
    {
        Socket sock(SW_SOCK_TCP);
        bool retval = sock.connect("www.baidu.com", 80, 0.5);
        ASSERT_EQ(retval, true);
        ASSERT_EQ(sock.errCode, 0);
    });
}

TEST(coroutine, socket_resolve_with_cache)
{
    coro_test([](void *arg)
    {
        Coroutine::set_dns_cache_capacity(10);

        std::string addr1 = Coroutine::gethostbyname("www.baidu.com", AF_INET);
        std::string addr2 = Coroutine::gethostbyname("www.baidu.com", AF_INET);

        ASSERT_NE(addr1, "");
        ASSERT_NE(addr2, "");
        ASSERT_EQ(addr1, addr2);
    });
}

TEST(coroutine, socket_resolve_without_cache)
{
    coro_test([](void *arg)
    {
        Coroutine::set_dns_cache_capacity(0);

        std::string addr1 = Coroutine::gethostbyname("www.baidu.com", AF_INET);
        std::string addr2 = Coroutine::gethostbyname("www.baidu.com", AF_INET);

        ASSERT_NE(addr1, "");
        ASSERT_NE(addr2, "");
        ASSERT_NE(addr1, addr2);
    });
}

TEST(coroutine, socket_resolve_cache_inet4_and_inet6)
{
    coro_test([](void *arg)
    {
        Coroutine::set_dns_cache_capacity(10);

        std::string addr1 = Coroutine::gethostbyname("ipv6.sjtu.edu.cn", AF_INET);
        std::string addr2 = Coroutine::gethostbyname("ipv6.sjtu.edu.cn", AF_INET6);

        ASSERT_NE(addr1, "");<commit_msg>fix core-testst<commit_after>#include "tests.h"

using namespace swoole;

TEST(coroutine, create)
{
    long cid = Coroutine::create([](void *arg)
    {
        long cid = Coroutine::get_current_cid();
        Coroutine *co = Coroutine::get_by_cid(cid);
        co->yield();
    });
    ASSERT_GT(cid, 0);
    Coroutine::get_by_cid(cid)->resume();
}

TEST(coroutine, socket_connect_refused)
{
    coro_test([](void *arg)
    {
        Socket sock(SW_SOCK_TCP);
        bool retval = sock.connect("127.0.0.1", 9801, 0.5);
        ASSERT_EQ(retval, false);
        ASSERT_EQ(sock.errCode, ECONNREFUSED);
    });
}

TEST(coroutine, socket_connect_timeout)
{
    coro_test([](void *arg)
    {
        Socket sock(SW_SOCK_TCP);
        sock.set_timeout(0.5);
        bool retval = sock.connect("192.0.0.1", 9801);
        ASSERT_EQ(retval, false);
        ASSERT_EQ(sock.errCode, ETIMEDOUT);
    });
}

TEST(coroutine, socket_connect_with_dns)
{
    coro_test([](void *arg)
    {
        Socket sock(SW_SOCK_TCP);
        bool retval = sock.connect("www.baidu.com", 80, 0.5);
        ASSERT_EQ(retval, true);
        ASSERT_EQ(sock.errCode, 0);
    });
}

TEST(coroutine, socket_resolve_with_cache)
{
    coro_test([](void *arg)
    {
        set_dns_cache_capacity(10);

        std::string addr1 = Coroutine::gethostbyname("www.baidu.com", AF_INET);
        std::string addr2 = Coroutine::gethostbyname("www.baidu.com", AF_INET);

        ASSERT_NE(addr1, "");
        ASSERT_NE(addr2, "");
        ASSERT_EQ(addr1, addr2);
    });
}

TEST(coroutine, socket_resolve_without_cache)
{
    coro_test([](void *arg)
    {
        set_dns_cache_capacity(0);

        std::string addr1 = Coroutine::gethostbyname("www.baidu.com", AF_INET);
        std::string addr2 = Coroutine::gethostbyname("www.baidu.com", AF_INET);

        ASSERT_NE(addr1, "");
        ASSERT_NE(addr2, "");
        ASSERT_NE(addr1, addr2);
    });
}

TEST(coroutine, socket_resolve_cache_inet4_and_inet6)
{
    coro_test([](void *arg)
    {
        set_dns_cache_capacity(10);

        std::string addr1 = Coroutine::gethostbyname("ipv6.sjtu.edu.cn", AF_INET);
        std::string addr2 = Coroutine::gethostbyname("ipv6.sjtu.edu.cn", AF_INET6);

        ASSERT_NE(addr1, "");
{'fraction_non_alphanumeric': 0.15546684408959857, 'fraction_numerical': 0.02439565313816811, 'mean_word_length': 3.0962761126248863, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 10, 'xml': 0}, 'pii_count': 4, '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': '5795557', 'n_tokens_mistral': 1724, 'n_tokens_neox': 1758, 'n_words': 229}