-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use random port checks instead of incremental port checks
- Loading branch information
Showing
3 changed files
with
30 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,39 @@ | ||
package warp | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func listenLocalPort(t *testing.T) (net.Listener, int) { | ||
ln, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
func TestTakeOut(t *testing.T) { | ||
port := 35555 | ||
start := port - 10 | ||
end := port + 10 | ||
|
||
go func() { | ||
for { | ||
conn, _ := ln.Accept() | ||
if conn != nil { | ||
conn.Close() | ||
} | ||
} | ||
}() | ||
r := &PortRange{Start: start, End: end} | ||
got, err := r.TakeOut() | ||
|
||
_, p, err := net.SplitHostPort(ln.Addr().String()) | ||
if err != nil { | ||
t.Fatal(err) | ||
if start != got || err != nil { | ||
t.Errorf("port range take out expected %d, but got %d", start, got) | ||
} | ||
} | ||
|
||
port, err := strconv.Atoi(p) | ||
func TestIsPortAvailable(t *testing.T) { | ||
used := 35555 | ||
unused := 36666 | ||
|
||
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", used)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
return ln, port | ||
} | ||
|
||
func TestTakeOut(t *testing.T) { | ||
ip := "127.0.0.1" | ||
|
||
ln, port := listenLocalPort(t) | ||
defer ln.Close() | ||
|
||
start := port - 10 | ||
end := port + 10 | ||
|
||
r1 := &PortRange{Start: start, End: end} | ||
got1, err := r1.TakeOut(ip) | ||
if start != got1 || err != nil { | ||
t.Errorf("port range take out expected %d, but got %d", start, got1) | ||
if isPortAvailable(used) { | ||
t.Error("port was listened, but isPortAvailable returns true") | ||
} | ||
|
||
r2 := &PortRange{Start: port, End: port} | ||
got2, err := r2.TakeOut(ip) | ||
if 0 != got2 || err == nil { | ||
t.Error("port range take out expected error, but got no error") | ||
if !isPortAvailable(unused) { | ||
t.Error("port was not listened, but isPortAvailable returns false") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters