<commit_before>%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2001-2016. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%

%%
-module(ei_accept_SUITE).

-include_lib("common_test/include/ct.hrl").
-include("ei_accept_SUITE_data/ei_accept_test_cases.hrl").

-export([all/0, suite/0,
         ei_accept/1, ei_threaded_accept/1]).

-import(runner, [get_term/1,send_term/2]).

suite() ->
    [{ct_hooks,[ts_install_cth]},
     {timetrap, {seconds, 30}}].

all() -> 
    [ei_accept, ei_threaded_accept].

ei_accept(Config) when is_list(Config) ->
    P = runner:start(?interpret),
    0 = ei_connect_init(P, 42, erlang:get_cookie(), 0),

    Myname = hd(tl(string:tokens(atom_to_list(node()), "@"))),
    io:format("Myname ~p ~n",  [Myname]),
    EINode = list_to_atom("c42@"++Myname),
    io:format("EINode ~p ~n",  [EINode]),
    Self = self(),
    TermToSend= {call, Self, "Test"},
    F= fun() ->
               case waitfornode("c42",20) of
                   true ->
                       {any, EINode} ! TermToSend,
                       Self ! sent_ok;
                   false ->
                       Self ! never_published
               end,
               ok
       end,

    spawn(F),
    Port = 6543,
    {ok, ListenFd} = ei_publish(P, Port),
    {ok, Fd, _Node} = ei_accept(P, ListenFd),
    TermReceived= ei_receive(P, Fd),
    io:format("Sent ~p received ~p ~n", [TermToSend, TermReceived]),
    TermToSend= TermReceived,
    receive
        sent_ok ->
            ok;
        Unknown ->
            io:format("~p ~n", [Unknown])
    after 1000 ->
              io:format("timeout ~n")
    end,
    runner:finish(P),
    ok.

ei_threaded_accept(Config) when is_list(Config) ->
    Einode = filename:join(proplists:get_value(data_dir, Config), "eiaccnode"),
    N = 1, % 3,
    Host = atom_to_list(node()),
    Port = 6767,
    start_einode(Einode, N, Host, Port),
    io:format("started eiaccnode"),
    %%spawn_link(fun() -> start_einode(Einode, N, Host, Port) end),
    TestServerPid = self(),
    [spawn_link(fun() -> send_rec_einode(I, TestServerPid) end) || I <- lists:seq(0, N-1)],
    [receive I -> ok end || I <- lists:seq(0, N-1) ],
    ok.<commit_msg>erl_interface: Add tuple fallback tests<commit_after>%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 2001-2016. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%

%%
-module(ei_accept_SUITE).

-include_lib("common_test/include/ct.hrl").
-include("ei_accept_SUITE_data/ei_accept_test_cases.hrl").

-export([all/0, suite/0,
         ei_accept/1, ei_threaded_accept/1]).

-import(runner, [get_term/1,send_term/2]).

suite() ->
    [{ct_hooks,[ts_install_cth]},
     {timetrap, {seconds, 30}}].

all() -> 
    [ei_accept, ei_threaded_accept].

ei_accept(Config) when is_list(Config) ->
    P = runner:start(?interpret),
    0 = ei_connect_init(P, 42, erlang:get_cookie(), 0),

    Myname = hd(tl(string:tokens(atom_to_list(node()), "@"))),
    io:format("Myname ~p ~n",  [Myname]),
    EINode = list_to_atom("c42@"++Myname),
    io:format("EINode ~p ~n",  [EINode]),

    %% We take this opportunity to also test export-funs and bit-strings
    %% with (ugly) tuple fallbacks.
    %% Test both toward pending connection and established connection.
    RealTerms = [<<1:1>>,     fun lists:map/2],
    Fallbacks = [{<<128>>,1}, {lists,map}],

    Self = self(),
    Funny = fun() -> hello end,
    TermToSend = {call, Self, "Test", Funny, RealTerms},
    TermToGet  = {call, Self, "Test", Funny, Fallbacks},
    Port = 6543,
    {ok, ListenFd} = ei_publish(P, Port),
    {any, EINode} ! TermToSend,
    {ok, Fd, _Node} = ei_accept(P, ListenFd),
    Got1 = ei_receive(P, Fd),

    %% Send again, now without auto-connect
    {any, EINode} ! TermToSend,
    Got2 = ei_receive(P, Fd),

    io:format("Sent ~p~nExp. ~p~nGot1 ~p~nGot2 ~p~n", [TermToSend, TermToGet, Got1, Got2]),
    TermToGet = Got1,
    TermToGet = Got2,

    runner:finish(P),
    ok.

ei_threaded_accept(Config) when is_list(Config) ->
    Einode = filename:join(proplists:get_value(data_dir, Config), "eiaccnode"),
    N = 1, % 3,
    Host = atom_to_list(node()),
    Port = 6767,
    start_einode(Einode, N, Host, Port),
    io:format("started eiaccnode"),
    %%spawn_link(fun() -> start_einode(Einode, N, Host, Port) end),
    TestServerPid = self(),
    [spawn_link(fun() -> send_rec_einode(I, TestServerPid) end) || I <- lists:seq(0, N-1)],
    [receive I -> ok end || I <- lists:seq(0, N-1) ],
    ok.
{'fraction_non_alphanumeric': 0.15669362084456423, 'fraction_numerical': 0.018508535489667566, 'mean_word_length': 3.226271829916477, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3301720', 'n_tokens_mistral': 2037, 'n_tokens_neox': 1932, 'n_words': 494}