timers.connect.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <felspar/io.hpp>
#include <felspar/test.hpp>

#include <sys/types.h>
#if defined(FELSPAR_POSIX_SOCKETS)
#include <sys/socket.h>
#include <netdb.h>
#endif


using namespace std::literals;


namespace {


    auto const suite = felspar::testsuite("connect");


    felspar::io::warden::task<void> timed_connect(felspar::io::warden &ward) {
        felspar::test::injected check;

        struct addrinfo hints = {};
        hints.ai_socktype = SOCK_STREAM;
        struct addrinfo *addresses = nullptr;
        check(getaddrinfo("www.felspar.com", nullptr, &hints, &addresses)) == 0;
        check(addresses) != nullptr;

        sockaddr_in address =
                *reinterpret_cast<sockaddr_in *>(addresses->ai_addr);
        freeaddrinfo(addresses);

        try {
            address.sin_port = htons(80);
            auto fd1 = ward.create_socket(AF_INET, SOCK_STREAM, 0);
            co_await ward.connect(
                    fd1, reinterpret_cast<sockaddr const *>(&address),
                    sizeof(address), 5s);

            address.sin_port = htons(808);
            auto fd2 = ward.create_socket(AF_INET, SOCK_STREAM, 0);
            co_await ward.connect(
                    fd2, reinterpret_cast<sockaddr const *>(&address),
                    sizeof(address), 10ms);

            check(false) == true;
        } catch (felspar::io::timeout const &) {
            check(true) == true;
        } catch (std::exception const &e) {
            check(e.what()) == ""; /// Print out the exception message
        } catch (...) { check(false) == true; }
    }
    auto const p = suite.test("poll", []() {
        felspar::io::poll_warden ward;
        ward.run(timed_connect);
    });
#ifdef FELSPAR_ENABLE_IO_URING
    auto const u = suite.test("uring", []() {
        felspar::io::uring_warden ward{5};
        ward.run(timed_connect);
    });
#endif


}