Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get valid pair by priority #46

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/ex_ice/priv/checklist.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ defmodule ExICE.Priv.Checklist do
@spec get_valid_pair(t()) :: CandidatePair.t() | nil
def get_valid_pair(checklist) do
checklist
|> Enum.find({nil, nil}, fn {_id, pair} -> pair.valid? end)
|> elem(1)
|> Stream.map(fn {_id, pair} -> pair end)
|> Stream.filter(fn pair -> pair.valid? end)
|> Enum.sort_by(fn pair -> pair.priority end, :desc)
|> Enum.at(0)
end

@spec find_pair(t(), CandidatePair.t()) :: CandidatePair.t() | nil
Expand Down
1 change: 0 additions & 1 deletion test/candidate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ defmodule ExICE.CandidateTest do
alias ExICE.Candidate

test "candidate's foundation" do
# FIXME socket shouldn't be nil
ip = {192, 168, 1, 1}
port = 12_345

Expand Down
52 changes: 52 additions & 0 deletions test/priv/checklist_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
defmodule ExICE.Priv.ChecklistTest do
use ExUnit.Case, async: true

alias ExICE.Priv.{Candidate, CandidatePair, Checklist}

test "get_valid_pair/1" do
local_addr = {192, 168, 0, 1}
local_port = 8445
remote_addr = {192, 168, 0, 2}
remote_port = 8445
remote_srflx_addr = {192, 168, 0, 3}
remote_srflx_port = 8445

local_host_cand =
Candidate.Host.new(
address: local_addr,
port: local_port,
base_address: local_addr,
base_port: local_port,
socket: nil
)

remote_host_cand =
ExICE.Candidate.new(:host,
address: local_addr,
port: local_port,
base_address: local_addr,
base_port: local_port
)

remote_srflx_cand =
ExICE.Candidate.new(:srflx,
address: remote_srflx_addr,
port: remote_srflx_port,
base_address: remote_addr,
base_port: remote_port
)

host_pair =
CandidatePair.new(local_host_cand, remote_host_cand, :controlling, :succeeded, valid?: true)

srflx_pair =
CandidatePair.new(local_host_cand, remote_srflx_cand, :controlling, :succeeded,
valid?: true
)

checklist = %{host_pair.id => host_pair, srflx_pair.id => srflx_pair}

assert host_pair.priority > srflx_pair.priority
assert Checklist.get_valid_pair(checklist) == host_pair
end
end
Loading