-
Notifications
You must be signed in to change notification settings - Fork 142
fix: autorelay reservation for private nodes #584
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| package tunnel | ||
|
|
||
| import ( | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestNormalizeAutoRelayConfig(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| cfgMaxCandidates int | ||
| relayNums int | ||
| wantMinCandidates int | ||
| wantMaxCandidates int | ||
| wantNumRelays int | ||
| }{ | ||
| { | ||
| // root cause scenario from issue #583: single relay with default MaxCandidates; | ||
| // after the fix minCandidates must be >= 1 and numRelays must be 1 | ||
| name: "single relay – min candidates fixed to 1, numRelays set to 1", | ||
| cfgMaxCandidates: 4, | ||
| relayNums: 1, | ||
| wantMinCandidates: 1, | ||
| wantMaxCandidates: 4, | ||
| wantNumRelays: 1, | ||
| }, | ||
| { | ||
| // multiple relays, maxCandidates is large enough | ||
| name: "multi relay – maxCandidates sufficient, numRelays stays 0", | ||
| cfgMaxCandidates: 4, | ||
| relayNums: 3, | ||
| wantMinCandidates: 1, | ||
| wantMaxCandidates: 4, | ||
| wantNumRelays: 0, | ||
| }, | ||
| { | ||
| // multiple relays, maxCandidates is too small and must be boosted to relayNums | ||
| name: "multi relay – maxCandidates boosted to relayNums", | ||
| cfgMaxCandidates: 2, | ||
| relayNums: 5, | ||
| wantMinCandidates: 1, | ||
| wantMaxCandidates: 5, | ||
| wantNumRelays: 0, | ||
| }, | ||
| { | ||
| // cfgMaxCandidates=0 with single relay: | ||
| // maxCandidates should be boosted to relayNums=1, minCandidates=1 | ||
| name: "zero maxCandidates with single relay – both boosted to 1", | ||
| cfgMaxCandidates: 0, | ||
| relayNums: 1, | ||
| wantMinCandidates: 1, | ||
| wantMaxCandidates: 1, | ||
| wantNumRelays: 1, | ||
| }, | ||
| { | ||
| // no relay nodes configured; minCandidates=1 must still hold | ||
| name: "no relay nodes", | ||
| cfgMaxCandidates: 4, | ||
| relayNums: 0, | ||
| wantMinCandidates: 1, | ||
| wantMaxCandidates: 4, | ||
| wantNumRelays: 0, | ||
| }, | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test suite for Also, some comments in this file are in Chinese (e.g., lines 84 and 92). It would be great to translate them to English for consistency. },
{
// cfgMaxCandidates=0, no relay nodes
// minCandidates should be 0 if maxCandidates is 0
name: "zero maxCandidates with no relays",
cfgMaxCandidates: 0,
relayNums: 0,
wantMinCandidates: 0,
wantMaxCandidates: 0,
wantNumRelays: 0,
}, |
||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| gotMin, gotMax, gotNum := normalizeAutoRelayConfig(tt.cfgMaxCandidates, tt.relayNums) | ||
| if gotMin != tt.wantMinCandidates { | ||
| t.Errorf("minCandidates: got %d, want %d", gotMin, tt.wantMinCandidates) | ||
| } | ||
| if gotMax != tt.wantMaxCandidates { | ||
| t.Errorf("maxCandidates: got %d, want %d", gotMax, tt.wantMaxCandidates) | ||
| } | ||
| if gotNum != tt.wantNumRelays { | ||
| t.Errorf("numRelays: got %d, want %d", gotNum, tt.wantNumRelays) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestBuildAutoRelayOpts(t *testing.T) { | ||
| t.Run("without numRelays", func(t *testing.T) { | ||
| opts := buildAutoRelayOpts(1, 4, 0) | ||
| // should contain 3 options: WithMinCandidates, WithMaxCandidates, WithBackoff | ||
| if len(opts) != 3 { | ||
| t.Errorf("expected 3 options, got %d", len(opts)) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("with numRelays", func(t *testing.T) { | ||
| opts := buildAutoRelayOpts(1, 1, 1) | ||
| // should contain 4 options: WithMinCandidates, WithMaxCandidates, WithBackoff, WithNumRelays | ||
| if len(opts) != 4 { | ||
| t.Errorf("expected 4 options, got %d", len(opts)) | ||
| } | ||
| }) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current implementation for handling
maxCandidatesis a bit convoluted. It calculates a newmaxCandidates, logs an update, modifies the input configurationc.MaxCandidates, and then uses this modified value inbuildAutoRelayOpts.For better clarity and to avoid side effects on the input configuration, it's preferable to use the local
maxCandidatesvariable directly.I suggest removing the update to
c.MaxCandidateshere, and then passing the localmaxCandidatesvariable tobuildAutoRelayOptson line 174 like so:buildAutoRelayOpts(minCandidates, maxCandidates, numRelays)...,.