Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
103b7c01
Commit
103b7c01
authored
Dec 25, 2016
by
Tim Hockin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a ForEach() to port allocator
parent
0777ecd0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
0 deletions
+46
-0
allocator.go
pkg/registry/core/service/portallocator/allocator.go
+8
-0
allocator_test.go
pkg/registry/core/service/portallocator/allocator_test.go
+38
-0
No files found.
pkg/registry/core/service/portallocator/allocator.go
View file @
103b7c01
...
@@ -33,6 +33,7 @@ type Interface interface {
...
@@ -33,6 +33,7 @@ type Interface interface {
Allocate
(
int
)
error
Allocate
(
int
)
error
AllocateNext
()
(
int
,
error
)
AllocateNext
()
(
int
,
error
)
Release
(
int
)
error
Release
(
int
)
error
ForEach
(
func
(
int
))
}
}
var
(
var
(
...
@@ -117,6 +118,13 @@ func (r *PortAllocator) AllocateNext() (int, error) {
...
@@ -117,6 +118,13 @@ func (r *PortAllocator) AllocateNext() (int, error) {
return
r
.
portRange
.
Base
+
offset
,
nil
return
r
.
portRange
.
Base
+
offset
,
nil
}
}
// ForEach calls the provided function for each allocated port.
func
(
r
*
PortAllocator
)
ForEach
(
fn
func
(
int
))
{
r
.
alloc
.
ForEach
(
func
(
offset
int
)
{
fn
(
r
.
portRange
.
Base
+
offset
)
})
}
// Release releases the port back to the pool. Releasing an
// Release releases the port back to the pool. Releasing an
// unallocated port or a port out of the range is a no-op and
// unallocated port or a port out of the range is a no-op and
// returns no error.
// returns no error.
...
...
pkg/registry/core/service/portallocator/allocator_test.go
View file @
103b7c01
...
@@ -103,6 +103,44 @@ func TestAllocate(t *testing.T) {
...
@@ -103,6 +103,44 @@ func TestAllocate(t *testing.T) {
}
}
}
}
func
TestForEach
(
t
*
testing
.
T
)
{
pr
,
err
:=
net
.
ParsePortRange
(
"10000-10200"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
testCases
:=
[]
sets
.
Int
{
sets
.
NewInt
(),
sets
.
NewInt
(
10000
),
sets
.
NewInt
(
10000
,
10200
),
sets
.
NewInt
(
10000
,
10099
,
10200
),
}
for
i
,
tc
:=
range
testCases
{
r
:=
NewPortAllocator
(
*
pr
)
for
port
:=
range
tc
{
if
err
:=
r
.
Allocate
(
port
);
err
!=
nil
{
t
.
Errorf
(
"[%d] error allocating port %v: %v"
,
i
,
port
,
err
)
}
if
!
r
.
Has
(
port
)
{
t
.
Errorf
(
"[%d] expected port %v allocated"
,
i
,
port
)
}
}
calls
:=
sets
.
NewInt
()
r
.
ForEach
(
func
(
port
int
)
{
calls
.
Insert
(
port
)
})
if
len
(
calls
)
!=
len
(
tc
)
{
t
.
Errorf
(
"[%d] expected %d calls, got %d"
,
i
,
len
(
tc
),
len
(
calls
))
}
if
!
calls
.
Equal
(
tc
)
{
t
.
Errorf
(
"[%d] expected calls to equal testcase: %v vs %v"
,
i
,
calls
.
List
(),
tc
.
List
())
}
}
}
func
TestSnapshot
(
t
*
testing
.
T
)
{
func
TestSnapshot
(
t
*
testing
.
T
)
{
pr
,
err
:=
net
.
ParsePortRange
(
"10000-10200"
)
pr
,
err
:=
net
.
ParsePortRange
(
"10000-10200"
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment