<commit_before>-module(afunix_tests).

-include_lib("eunit/include/eunit.hrl").

-import(afunix,[ socket/0
               , bind/2
               , listen/1
               , listen/2
               , connect/2
               , accept/1
               , send/2
               , send/3
               , recv/2
               ]).


send_receive_test() ->
    Path = socket_path(),

    S = socket(),
    ok = bind(S, Path),
    ok = listen(S),

    C = socket(),
    ok = connect(C, Path),

    {ok, C1} = accept(S),

    ok = send(C, <<"data">>),

    ?assertMatch({ok, <<"data">>}, recv(C1, 100)).


send_receive_fd_test() ->
    Path = socket_path(),

    S = socket(),
    ok = bind(S, Path),
    ok = listen(S),

    C = socket(),
    ok = connect(C, Path),

    Fd = memfd:create(),
    ok = memfd:pwrite(Fd, bof, <<1,2,3,4>>),
    FdBin = memfd:fd_to_binary(Fd),
    ok = send(C, [afunix:fd_from_binary(FdBin)], <<"data">>),

    {ok, C1} = accept(S),
    {ok, [Fd1], <<"data">>} = recv(C1, 100),
    Fd1Bin = afunix:fd_to_binary(Fd1),
    MemFd = memfd:fd_from_binary(Fd1Bin),
    ?assertMatch({ok, <<1,2,3,4>>}, memfd:pread(MemFd, 0, 4)).


accept_monitor_test() ->
    Path = socket_path(),

    S = socket(),
    ok = bind(S, Path),
    ok = listen(S),

    {error, eagain} = accept(S),
    Ref = afunix:monitor(S, read),

    C = socket(),
    ok = connect(C, Path),

    receive {afunix, Ref} -> {ok, _} = accept(S) end.


connect_monitor_test() ->
    Path = socket_path(),

    S = socket(),
    ok = bind(S, Path),
    ok = listen(S, 0),

    C1 = socket(),
    ok = connect(C1, Path),
<commit_msg>Use new memfd API<commit_after>-module(afunix_tests).

-include_lib("eunit/include/eunit.hrl").

-import(afunix,[ socket/0
               , bind/2
               , listen/1
               , listen/2
               , connect/2
               , accept/1
               , send/2
               , send/3
               , recv/2
               ]).


send_receive_test() ->
    Path = socket_path(),

    S = socket(),
    ok = bind(S, Path),
    ok = listen(S),

    C = socket(),
    ok = connect(C, Path),

    {ok, C1} = accept(S),

    ok = send(C, <<"data">>),

    ?assertMatch({ok, <<"data">>}, recv(C1, 100)).


send_receive_fd_test() ->
    Path = socket_path(),

    S = socket(),
    ok = bind(S, Path),
    ok = listen(S),

    C = socket(),
    ok = connect(C, Path),

    Fd = memfd:new(),
    ok = memfd:pwrite(Fd, bof, <<1,2,3,4>>),
    ok = send(C, [afunix:fd_from_binary(memfd:fd(Fd))], <<"data">>),

    {ok, C1} = accept(S),
    {ok, [Fd1], <<"data">>} = recv(C1, 100),
    MemFd = memfd:new(afunix:fd_to_binary(Fd1)),
    ?assertMatch({ok, <<1,2,3,4>>}, memfd:pread(MemFd, 0, 4)).


accept_monitor_test() ->
    Path = socket_path(),

    S = socket(),
    ok = bind(S, Path),
    ok = listen(S),

    {error, eagain} = accept(S),
    Ref = afunix:monitor(S, read),

    C = socket(),
    ok = connect(C, Path),

    receive {afunix, Ref} -> {ok, _} = accept(S) end.


connect_monitor_test() ->
    Path = socket_path(),

    S = socket(),
    ok = bind(S, Path),
    ok = listen(S, 0),

    C1 = socket(),
    ok = connect(C1, Path),

{'fraction_non_alphanumeric': 0.19860847564832385, 'fraction_numerical': 0.022137887413029727, 'mean_word_length': 2.0828460038986356, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 37, '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': '7210393', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1225, 'n_words': 247}