<commit_before>%% -------------------------------------------------------------------
%%
%% invite_test: Invite Suite Test
%%
%% Copyright (c) 2013 <NAME>.  All Rights Reserved.
%%
%% This file is provided to you 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.
%%
%% -------------------------------------------------------------------

-module(invite_test).

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

-compile([export_all]).

% invite_test_() ->
%     {setup, spawn, 
%         fun() -> start() end,
%         fun(_) -> stop() end,
%         {inparallel, [
%             {timeout, 60, fun cancel/0},
%             {timeout, 60, fun dialog/0},
%             {timeout, 60, fun rr_contact/0},
%             {timeout, 60, fun multiple_uac/0},
%             {timeout, 60, fun multiple_uas/0}
%         ]}
%     }.


start() ->
    tests_util:start_nksip(),

    ok = sipapp_endpoint:start({invite, client1}, [
        {from, "sip:client1@nksip"},
        {local_host, "localhost"},
        {supported, []},
        {transports, [{udp, all, 5060}, {tls, all, 5061}]}
    ]),
    
    ok = sipapp_endpoint:start({invite, client2}, [
        {from, "sip:client2@nksip"},
        no_100,
        {local_host, "127.0.0.1"},
        {supported, []},
        {transports, [{udp, all, 5070}, {tls, all, 5071}]}
    ]),

    tests_util:log(),
    ?debugFmt("Starting ~p", [?MODULE]).


stop() ->
    ok = sipapp_endpoint:stop({invite, client1}),
    ok = sipapp_endpoint:stop({invite, client2}).



cancel() ->
    C1 = {invite, client1},
    Ref = make_ref(),
    Self = self(),
    RepHd = {add, "x-nk-reply", base64:encode(erlang:term_to_binary({Ref, Self}))},
    Fun = fun({ok, Code, _}) -> Self ! {Ref, Code} end,

    % % Receive generated busy
    % Hds1 = [{add, "x-nk-sleep", 300}, {add, "x-nk-op", busy}, RepHd],
    % {ok, 486, _} = nksip_uac:invite(C1, "sip:any@127.0.0.1:5070", 
    %                                 [{callback, Fun} | Hds1]),

    Hds2 = [{add, "x-nk-sleep", 3000}, {add, "x-nk-op", ok}, {add, "x-nk-prov", "true"}, RepHd],
    Remote = "sip:any@127.0.0.1:5070",

    % Test manual CANCEL
    {async, Req3Id} = nksip_uac:invite(C1, "sip:any@127.0.0.1:5070", 
                                        [{callback, Fun}, async | Hds2]),
    timer:sleep(100),

    lager:notice("REQ: ~p", [Req3Id]),

    ok = nksip_uac:cancel(C1, Req3Id),
    
    % Test invite expire, UAC must send CANCEL
    {ok, 487, _} = nksip_uac:invite(C1, Remote, 
                              [{callback, Fun}, {expires, 1} | Hds2]),

    % Test invite expire, UAC will ignore and UAS must CANCEL
    {ok, 487, _} = nksip_uac:invite(C1, "sip:any@127.0.0.1:5070", 
                                        [{callback, Fun}, {expires, 1}, no_auto_expire
                                          | Hds2]),
    
    ok = tests_util:wait(Ref, [180, 487, 180, 180,
                               {client2, {dialog_stop, cancelled}},<commit_msg>Test update<commit_after>%% -------------------------------------------------------------------
%%
%% invite_test: Invite Suite Test
%%
%% Copyright (c) 2013 <NAME>.  All Rights Reserved.
%%
%% This file is provided to you 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.
%%
%% -------------------------------------------------------------------

-module(invite_test).

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

-compile([export_all]).

invite_test_() ->
    {setup, spawn, 
        fun() -> start() end,
        fun(_) -> stop() end,
        {inparallel, [
            {timeout, 60, fun cancel/0},
            {timeout, 60, fun dialog/0},
            {timeout, 60, fun rr_contact/0},
            {timeout, 60, fun multiple_uac/0},
            {timeout, 60, fun multiple_uas/0}
        ]}
    }.


start() ->
    tests_util:start_nksip(),

    ok = sipapp_endpoint:start({invite, client1}, [
        {from, "sip:client1@nksip"},
        {local_host, "localhost"},
        {supported, []},
        {transports, [{udp, all, 5060}, {tls, all, 5061}]}
    ]),
    
    ok = sipapp_endpoint:start({invite, client2}, [
        {from, "sip:client2@nksip"},
        no_100,
        {local_host, "127.0.0.1"},
        {supported, []},
        {transports, [{udp, all, 5070}, {tls, all, 5071}]}
    ]),

    tests_util:log(),
    ?debugFmt("Starting ~p", [?MODULE]).


stop() ->
    ok = sipapp_endpoint:stop({invite, client1}),
    ok = sipapp_endpoint:stop({invite, client2}).



cancel() ->
    C1 = {invite, client1},
    Ref = make_ref(),
    Self = self(),
    RepHd = {add, "x-nk-reply", base64:encode(erlang:term_to_binary({Ref, Self}))},
    Fun = fun({ok, Code, _}) -> Self ! {Ref, Code} end,

    % Receive generated busy
    Hds1 = [{add, "x-nk-sleep", 300}, {add, "x-nk-op", busy}, RepHd],
    {ok, 486, _} = nksip_uac:invite(C1, "sip:any@127.0.0.1:5070", 
                                    [{callback, Fun} | Hds1]),

    Hds2 = [{add, "x-nk-sleep", 3000}, {add, "x-nk-op", ok}, {add, "x-nk-prov", "true"}, RepHd],
    Remote = "sip:any@127.0.0.1:5070",

    % Test manual CANCEL
    {async, Req3Id} = nksip_uac:invite(C1, "sip:any@127.0.0.1:5070", 
                                        [{callback, Fun}, async | Hds2]),
    timer:sleep(100),
    ok = nksip_uac:cancel(C1, Req3Id),
    
    % Test invite expire, UAC must send CANCEL
    {ok, 487, _} = nksip_uac:invite(C1, Remote, 
                              [{callback, Fun}, {expires, 1} | Hds2]),

    % Test invite expire, UAC will ignore and UAS must CANCEL
    {ok, 487, _} = nksip_uac:invite(C1, "sip:any@127.0.0.1:5070", 
                                        [{callback, Fun}, {expires, 1}, no_auto_expire
                                          | Hds2]),
    
    ok = tests_util:wait(Ref, [180, 487, 180, 180,
                               {client2, {dialog_stop, cancelled}},
{'fraction_non_alphanumeric': 0.20297679848241645, 'fraction_numerical': 0.042463154822705386, 'mean_word_length': 2.0694133452754144, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 10, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5791392', 'n_tokens_mistral': 2496, 'n_tokens_neox': 2279, 'n_words': 571}