Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p-quic-transport
Commits
46ad9d04
Unverified
Commit
46ad9d04
authored
Mar 18, 2017
by
Marten Seemann
Browse files
add tests for the listener
parent
ec281897
Changes
1
Show whitespace changes
Inline
Side-by-side
listener_test.go
0 → 100644
View file @
46ad9d04
package
libp2pquic
import
(
"errors"
"net"
tpt
"github.com/libp2p/go-libp2p-transport"
ma
"github.com/multiformats/go-multiaddr"
.
"github.com/onsi/ginkgo"
.
"github.com/onsi/gomega"
)
type
mockNetListener
struct
{
connToAccept
net
.
Conn
acceptErr
error
closed
bool
}
func
(
m
*
mockNetListener
)
Accept
()
(
net
.
Conn
,
error
)
{
if
m
.
acceptErr
!=
nil
{
return
nil
,
m
.
acceptErr
}
return
m
.
connToAccept
,
nil
}
func
(
m
*
mockNetListener
)
Close
()
error
{
m
.
closed
=
true
return
nil
}
func
(
m
*
mockNetListener
)
Addr
()
net
.
Addr
{
panic
(
"not implemented"
)
}
var
_
net
.
Listener
=
&
mockNetListener
{}
var
_
=
Describe
(
"Listener"
,
func
()
{
var
(
l
*
listener
netListener
*
mockNetListener
transport
tpt
.
Transport
)
BeforeEach
(
func
()
{
netListener
=
&
mockNetListener
{}
transport
=
&
QuicTransport
{}
l
=
&
listener
{
quicListener
:
netListener
,
transport
:
transport
,
}
})
It
(
"returns its addr"
,
func
()
{
laddr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/udp/12345/quic"
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
l
,
err
=
newListener
(
laddr
,
nil
,
nil
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
Expect
(
l
.
Addr
()
.
String
())
.
To
(
Equal
(
"127.0.0.1:12345"
))
})
It
(
"returns its multiaddr"
,
func
()
{
laddr
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/udp/12346/quic"
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
l
,
err
=
newListener
(
laddr
,
nil
,
nil
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
Expect
(
l
.
Multiaddr
()
.
String
())
.
To
(
Equal
(
"/ip4/127.0.0.1/udp/12346/quic"
))
})
It
(
"closes"
,
func
()
{
err
:=
l
.
Close
()
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
Expect
(
netListener
.
closed
)
.
To
(
BeTrue
())
})
Context
(
"accepting"
,
func
()
{
It
(
"accepts a new conn"
,
func
()
{
remoteAddr
,
err
:=
net
.
ResolveUDPAddr
(
"udp"
,
"127.0.0.1:1234"
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
localAddr
,
err
:=
net
.
ResolveUDPAddr
(
"udp"
,
"127.0.0.1:4321"
)
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
udpConn
,
err
:=
net
.
DialUDP
(
"udp"
,
localAddr
,
remoteAddr
)
netListener
.
connToAccept
=
udpConn
conn
,
err
:=
l
.
Accept
()
Expect
(
err
)
.
ToNot
(
HaveOccurred
())
Expect
(
conn
.
LocalMultiaddr
()
.
String
())
.
To
(
Equal
(
"/ip4/127.0.0.1/udp/4321"
))
Expect
(
conn
.
RemoteMultiaddr
()
.
String
())
.
To
(
Equal
(
"/ip4/127.0.0.1/udp/1234"
))
Expect
(
conn
.
Transport
())
.
To
(
Equal
(
transport
))
})
It
(
"errors if it can't read the muliaddresses of a conn"
,
func
()
{
netListener
.
connToAccept
=
&
net
.
UDPConn
{}
_
,
err
:=
l
.
Accept
()
Expect
(
err
)
.
To
(
HaveOccurred
())
Expect
(
err
.
Error
())
.
To
(
ContainSubstring
(
"nil multiaddr"
))
})
It
(
"errors if no connection can be accepted"
,
func
()
{
testErr
:=
errors
.
New
(
"test error"
)
netListener
.
acceptErr
=
testErr
_
,
err
:=
l
.
Accept
()
Expect
(
err
)
.
To
(
MatchError
(
testErr
))
})
})
})
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