Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-reuseport-transport
Commits
0e2041a9
Commit
0e2041a9
authored
Mar 29, 2018
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more tests
parent
b7421d5f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
144 additions
and
91 deletions
+144
-91
transport_test.go
transport_test.go
+144
-91
No files found.
transport_test.go
View file @
0e2041a9
...
...
@@ -5,176 +5,229 @@ import (
"testing"
ma
"github.com/multiformats/go-multiaddr"
manet
"github.com/multiformats/go-multiaddr-net"
)
func
TestNoneAndSingle
(
t
*
testing
.
T
)
{
var
trA
Transport
var
trB
Transport
laddr
,
_
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/0"
)
listenerA
,
err
:=
trA
.
Listen
(
laddr
)
var
loopback
,
_
=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/0"
)
var
unspec
,
_
=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/tcp/0"
)
var
global
ma
.
Multiaddr
func
init
()
{
addrs
,
err
:=
manet
.
InterfaceMultiaddrs
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
return
}
defer
listenerA
.
Close
()
for
_
,
addr
:=
range
addrs
{
if
!
manet
.
IsIP6LinkLocal
(
addr
)
&&
!
manet
.
IsIPLoopback
(
addr
)
{
tcp
,
_
:=
ma
.
NewMultiaddr
(
"/tcp/0"
)
global
=
addr
.
Encapsulate
(
tcp
)
return
}
}
}
func
acceptOne
(
t
*
testing
.
T
,
listener
manet
.
Listener
)
<-
chan
struct
{}
{
t
.
Helper
()
done
:=
make
(
chan
struct
{})
go
func
()
{
defer
close
(
done
)
c
,
err
:=
listener
A
.
Accept
()
c
,
err
:=
listener
.
Accept
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
t
.
Error
(
err
)
return
}
c
.
Close
()
}()
return
done
}
func
dialOne
(
t
*
testing
.
T
,
tr
*
Transport
,
listener
manet
.
Listener
,
expected
...
int
)
int
{
t
.
Helper
()
c
,
err
:=
trB
.
Dial
(
listenerA
.
Multiaddr
())
done
:=
acceptOne
(
t
,
listener
)
c
,
err
:=
tr
.
Dial
(
listener
.
Multiaddr
())
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
port
:=
c
.
LocalAddr
()
.
(
*
net
.
TCPAddr
)
.
Port
<-
done
c
.
Close
()
if
len
(
expected
)
==
0
{
return
port
}
for
_
,
p
:=
range
expected
{
if
p
==
port
{
return
port
}
}
t
.
Errorf
(
"dialed from %d, expected to dial from one of %v"
,
port
,
expected
)
return
0
}
listenerB
,
err
:=
trB
.
Listen
(
laddr
)
func
TestNoneAndSingle
(
t
*
testing
.
T
)
{
var
trA
Transport
var
trB
Transport
listenerA
,
err
:=
trA
.
Listen
(
loopback
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listener
B
.
Close
()
defer
listener
A
.
Close
()
done
=
make
(
chan
struct
{})
go
func
()
{
defer
close
(
done
)
c
,
err
:=
listenerA
.
Accept
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
c
.
Close
()
}()
dialOne
(
t
,
&
trB
,
listenerA
)
c
,
err
=
trB
.
Dial
(
listenerA
.
Multiaddr
()
)
listenerB
,
err
:
=
trB
.
Listen
(
loopback
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
actual
:=
c
.
LocalAddr
()
.
(
*
net
.
TCPAddr
)
.
Port
expected
:=
listenerB
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
if
actual
!=
expected
{
t
.
Errorf
(
"expected to use port %d, used port %d"
,
expected
,
actual
)
}
<-
done
c
.
Close
()
defer
listenerB
.
Close
()
dialOne
(
t
,
&
trB
,
listenerA
,
listenerB
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
)
}
func
TestTwoLocal
(
t
*
testing
.
T
)
{
var
trA
Transport
var
trB
Transport
laddr
,
_
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/0"
)
listenerA
,
err
:=
trA
.
Listen
(
laddr
)
listenerA
,
err
:=
trA
.
Listen
(
loopback
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listenerA
.
Close
()
listenerB1
,
err
:=
trB
.
Listen
(
l
addr
)
listenerB1
,
err
:=
trB
.
Listen
(
l
oopback
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listenerB1
.
Close
()
listenerB2
,
err
:=
trB
.
Listen
(
l
addr
)
listenerB2
,
err
:=
trB
.
Listen
(
l
oopback
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listenerB2
.
Close
()
done
:=
make
(
chan
struct
{})
go
func
()
{
defer
close
(
done
)
c
,
err
:=
listenerA
.
Accept
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
c
.
Close
()
}()
dialOne
(
t
,
&
trB
,
listenerA
,
listenerB1
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
,
listenerB2
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
)
}
c
,
err
:=
trB
.
Dial
(
listenerA
.
Multiaddr
())
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
localPort
:=
c
.
LocalAddr
()
.
(
*
net
.
TCPAddr
)
.
Port
if
localPort
!=
listenerB1
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
&&
localPort
!=
listenerB2
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
{
t
.
Fatal
(
"didn't dial from one of our listener ports"
)
func
TestGlobalPreference
(
t
*
testing
.
T
)
{
if
global
==
nil
{
t
.
Skip
(
"no global addresses configured"
)
return
}
<-
done
c
.
Close
()
testPrefer
(
t
,
loopback
,
loopback
,
global
)
testPrefer
(
t
,
loopback
,
unspec
,
global
)
testPrefer
(
t
,
global
,
unspec
,
global
)
testPrefer
(
t
,
global
,
unspec
,
loopback
)
}
func
TestLocalAndUnspecified
(
t
*
testing
.
T
)
{
func
TestLoopbackPreference
(
t
*
testing
.
T
)
{
testPrefer
(
t
,
loopback
,
loopback
,
unspec
)
}
func
testPrefer
(
t
*
testing
.
T
,
listen
,
prefer
,
avoid
ma
.
Multiaddr
)
{
var
trA
Transport
var
trB
Transport
laddr
,
_
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/0"
)
unspec
,
_
:=
ma
.
NewMultiaddr
(
"/ip4/0.0.0.0/tcp/0"
)
listenerA
,
err
:=
trA
.
Listen
(
laddr
)
listenerA
,
err
:=
trA
.
Listen
(
listen
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listenerA
.
Close
()
listenerB1
,
err
:=
trB
.
Listen
(
laddr
)
listenerB1
,
err
:=
trB
.
Listen
(
avoid
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listenerB1
.
Close
()
listenerB2
,
err
:=
trB
.
Listen
(
unspec
)
dialOne
(
t
,
&
trB
,
listenerA
,
listenerB1
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
)
listenerB2
,
err
:=
trB
.
Listen
(
prefer
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listenerB2
.
Close
()
done
:=
make
(
chan
struct
{})
go
func
()
{
defer
close
(
done
)
c
,
err
:=
listenerA
.
Accept
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
c
.
Close
()
}()
dialOne
(
t
,
&
trB
,
listenerA
,
listenerB2
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
)
// Closing the listener should reset the dialer.
listenerB2
.
Close
()
dialOne
(
t
,
&
trB
,
listenerA
,
listenerB1
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
)
}
func
TestGlobalToGlobal
(
t
*
testing
.
T
)
{
if
global
==
nil
{
t
.
Skip
(
"no global addresses configured"
)
return
}
var
trA
Transport
var
trB
Transport
listenerA
,
err
:=
trA
.
Listen
(
global
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listenerA
.
Close
()
c
,
err
:=
trB
.
Dial
(
listenerA
.
Multiaddr
()
)
listenerB1
,
err
:=
trB
.
Listen
(
loopback
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
actual
:=
c
.
LocalAddr
()
.
(
*
net
.
TCPAddr
)
.
Port
expected
:=
listenerB1
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
if
actual
!=
expected
{
t
.
Errorf
(
"expected to use port %d, used port %d"
,
expected
,
actual
)
defer
listenerB1
.
Close
()
// It works (random port)
dialOne
(
t
,
&
trB
,
listenerA
)
listenerB2
,
err
:=
trB
.
Listen
(
global
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
<-
done
c
.
Close
()
defer
listenerB2
.
Close
()
// Uses global port.
dialOne
(
t
,
&
trB
,
listenerA
,
listenerB2
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
)
// Closing the listener should reset the dialer.
listenerB
1
.
Close
()
listenerB
2
.
Close
()
done
=
make
(
chan
struct
{})
go
func
()
{
defer
close
(
done
)
c
,
err
:=
listenerA
.
Accept
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
c
.
Close
()
}()
// It still works.
dialOne
(
t
,
&
trB
,
listenerA
)
}
func
TestDuplicateGlobal
(
t
*
testing
.
T
)
{
if
global
==
nil
{
t
.
Skip
(
"no global addresses configured"
)
return
}
var
trA
Transport
var
trB
Transport
listenerA
,
err
:=
trA
.
Listen
(
global
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listenerA
.
Close
()
c
,
err
=
trB
.
Dial
(
listenerA
.
Multiaddr
()
)
listenerB1
,
err
:
=
trB
.
Listen
(
global
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
actual
=
c
.
LocalAddr
()
.
(
*
net
.
TCPAddr
)
.
Port
expected
=
listenerB2
.
Addr
()
.
(
*
net
.
TCPAddr
)
.
Port
if
actual
!=
expected
{
t
.
Errorf
(
"expected to use port %d, used port %d"
,
expected
,
actual
)
defer
listenerB1
.
Close
()
listenerB2
,
err
:=
trB
.
Listen
(
global
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
defer
listenerB2
.
Close
()
// Check which port we're using
port
:=
dialOne
(
t
,
&
trB
,
listenerA
)
// Check consistency
for
i
:=
0
;
i
<
10
;
i
++
{
dialOne
(
t
,
&
trB
,
listenerA
,
port
)
}
<-
done
c
.
Close
()
}
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