Repository: pion/ice
Pull request #596 (2023-07-03 07:34:03)

Add `GetRemoteCandidates()`
This is analogue to `GetLocalCandidates()` and provides the user with the ability to get a slice of previously added remote candidates without keeping track of them in the application code.



codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pion/ice/pull/596?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) Report
Patch coverage has no change and project coverage change: **`-56.09`** :warning:
> Comparison is base [(`794e03f`)](https://app.codecov.io/gh/pion/ice/commit/794e03f62ccfec7fe12df2873ec89d617bf2e780?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) 78.41% compared to head [(`bcc9f48`)](https://app.codecov.io/gh/pion/ice/pull/596?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) 22.32%.

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           master     #596       +/-   ##
===========================================
- Coverage   78.41%   22.32%   -56.09%     
===========================================
  Files          42       37        -5     
  Lines        4374     4277       -97     
===========================================
- Hits         3430      955     -2475     
- Misses        729     3282     +2553     
+ Partials      215       40      -175     
```

| Flag | Coverage Δ | |
|---|---|---|
| go | `?` | |
| wasm | `22.32% <0.00%> (-0.07%)` | :arrow_down: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://app.codecov.io/gh/pion/ice/pull/596?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion) | Coverage Δ | |
|---|---|---|
| [agent.go](https://app.codecov.io/gh/pion/ice/pull/596?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion#diff-YWdlbnQuZ28=) | `0.00% <0.00%> (-82.47%)` | :arrow_down: |

... and [30 files with indirect coverage changes](https://app.codecov.io/gh/pion/ice/pull/596/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion)


</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pion/ice/pull/596?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).   
:loudspeaker: Do you have feedback about the report comment? [Let us know in this issue](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pion).

stv0g: Hi @at-wat,

sure. I also added one for `GetLocalCandidates()`.

Patch:
diff --git a/agent.go b/agent.go
index 91eb784..3f06857 100644
--- a/agent.go
+++ b/agent.go
@@ -838,6 +838,24 @@ func (a *Agent) addCandidate(ctx context.Context, c Candidate, candidateConn net
 	})
 }
 
+// GetRemoteCandidates returns the remote candidates
+func (a *Agent) GetRemoteCandidates() ([]Candidate, error) {
+	var res []Candidate
+
+	err := a.run(a.context(), func(ctx context.Context, agent *Agent) {
+		var candidates []Candidate
+		for _, set := range agent.remoteCandidates {
+			candidates = append(candidates, set...)
+		}
+		res = candidates
+	})
+	if err != nil {
+		return nil, err
+	}
+
+	return res, nil
+}
+
 // GetLocalCandidates returns the local candidates
 func (a *Agent) GetLocalCandidates() ([]Candidate, error) {
 	var res []Candidate


Test patch:
diff --git a/agent_test.go b/agent_test.go
index 6374cae..d41dc36 100644
--- a/agent_test.go
+++ b/agent_test.go
@@ -1322,6 +1322,74 @@ func TestGetRemoteCredentials(t *testing.T) {
 	assert.NoError(t, a.Close())
 }
 
+func TestGetRemoteCandidates(t *testing.T) {
+	var config AgentConfig
+
+	a, err := NewAgent(&config)
+	if err != nil {
+		t.Fatalf("Error constructing ice.Agent: %v", err)
+	}
+
+	expectedCandidates := []Candidate{}
+
+	for i := 0; i < 5; i++ {
+		cfg := CandidateHostConfig{
+			Network:   "udp",
+			Address:   "192.168.0.2",
+			Port:      1000 + i,
+			Component: 1,
+		}
+
+		cand, errCand := NewCandidateHost(&cfg)
+		assert.NoError(t, errCand)
+
+		expectedCandidates = append(expectedCandidates, cand)
+
+		a.addRemoteCandidate(cand)
+	}
+
+	actualCandidates, err := a.GetRemoteCandidates()
+	assert.NoError(t, err)
+	assert.ElementsMatch(t, expectedCandidates, actualCandidates)
+
+	assert.NoError(t, a.Close())
+}
+
+func TestGetLocalCandidates(t *testing.T) {
+	var config AgentConfig
+
+	a, err := NewAgent(&config)
+	if err != nil {
+		t.Fatalf("Error constructing ice.Agent: %v", err)
+	}
+
+	dummyConn := &net.UDPConn{}
+	expectedCandidates := []Candidate{}
+
+	for i := 0; i < 5; i++ {
+		cfg := CandidateHostConfig{
+			Network:   "udp",
+			Address:   "192.168.0.2",
+			Port:      1000 + i,
+			Component: 1,
+		}
+
+		cand, errCand := NewCandidateHost(&cfg)
+		assert.NoError(t, errCand)
+
+		expectedCandidates = append(expectedCandidates, cand)
+
+		err = a.addCandidate(context.Background(), cand, dummyConn)
+		assert.NoError(t, err)
+	}
+
+	actualCandidates, err := a.GetLocalCandidates()
+	assert.NoError(t, err)
+	assert.ElementsMatch(t, expectedCandidates, actualCandidates)
+
+	assert.NoError(t, a.Close())
+}
+
 func TestCloseInConnectionStateCallback(t *testing.T) {
 	report := test.CheckRoutines(t)
 	defer report()