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
dms3
go-unixfs
Commits
f10b4bd8
Commit
f10b4bd8
authored
Oct 16, 2014
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed old swarm test
parent
3a284661
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
85 deletions
+92
-85
net/swarm/swarm_test.go
net/swarm/swarm_test.go
+92
-85
No files found.
net/swarm/swarm_test.go
View file @
f10b4bd8
package
swarm
import
(
"bytes"
"fmt"
"testing"
"time"
ci
"github.com/jbenet/go-ipfs/crypto"
msg
"github.com/jbenet/go-ipfs/net/message"
peer
"github.com/jbenet/go-ipfs/peer"
u
"github.com/jbenet/go-ipfs/util"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
msgio
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-msgio"
ma
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
manet
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr/net"
mh
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
)
func
p
i
ng
Listen
(
t
*
testing
.
T
,
listener
manet
.
Listener
,
peer
*
peer
.
Peer
)
{
func
p
o
ng
(
ctx
context
.
Context
,
swarm
*
Swarm
)
{
for
{
c
,
err
:=
listener
.
Accept
()
if
err
==
nil
{
go
pong
(
t
,
c
,
peer
)
}
}
}
func
pong
(
t
*
testing
.
T
,
c
manet
.
Conn
,
peer
*
peer
.
Peer
)
{
mrw
:=
msgio
.
NewReadWriter
(
c
)
for
{
data
:=
make
([]
byte
,
1024
)
n
,
err
:=
mrw
.
ReadMsg
(
data
)
if
err
!=
nil
{
fmt
.
Printf
(
"error %v
\n
"
,
err
)
return
}
d
:=
string
(
data
[
:
n
])
if
d
!=
"ping"
{
t
.
Errorf
(
"error: didn't receive ping: '%v'
\n
"
,
d
)
return
}
err
=
mrw
.
WriteMsg
([]
byte
(
"pong"
))
if
err
!=
nil
{
fmt
.
Printf
(
"error %v
\n
"
,
err
)
select
{
case
<-
ctx
.
Done
()
:
return
case
m1
:=
<-
swarm
.
Incoming
:
if
bytes
.
Equal
(
m1
.
Data
(),
[]
byte
(
"ping"
))
{
m2
:=
msg
.
New
(
m1
.
Peer
(),
[]
byte
(
"pong"
))
swarm
.
Outgoing
<-
m2
}
}
}
}
func
setupPeer
(
id
string
,
addr
string
)
(
*
peer
.
Peer
,
error
)
{
func
setupPeer
(
t
*
testing
.
T
,
id
string
,
addr
string
)
*
peer
.
Peer
{
tcp
,
err
:=
ma
.
NewMultiaddr
(
addr
)
if
err
!=
nil
{
return
nil
,
err
t
.
Fatal
(
err
)
}
mh
,
err
:=
mh
.
FromHexString
(
id
)
if
err
!=
nil
{
return
nil
,
err
t
.
Fatal
(
err
)
}
p
:=
&
peer
.
Peer
{
ID
:
peer
.
ID
(
mh
)}
sk
,
pk
,
err
:=
ci
.
GenerateKeyPair
(
ci
.
RSA
,
512
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
p
.
PrivKey
=
sk
p
.
PubKey
=
pk
p
.
AddAddress
(
tcp
)
return
p
,
nil
return
p
}
func
Test
Swarm
(
t
*
testing
.
T
)
{
t
.
Skip
(
"TODO FIXME nil pointer"
)
func
make
Swarm
s
(
ctx
context
.
Context
,
t
*
testing
.
T
,
peers
map
[
string
]
string
)
[]
*
Swarm
{
swarms
:=
[]
*
Swarm
{}
local
,
err
:=
setupPeer
(
"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a30"
,
"/ip4/127.0.0.1/tcp/1234"
)
if
err
!=
nil
{
t
.
Fatal
(
"error setting up peer"
,
err
)
for
key
,
addr
:=
range
peers
{
local
:=
setupPeer
(
t
,
key
,
addr
)
peerstore
:=
peer
.
NewPeerstore
()
swarm
,
err
:=
NewSwarm
(
ctx
,
local
,
peerstore
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
swarms
=
append
(
swarms
,
swarm
)
}
peerstore
:=
peer
.
NewPeerstore
()
return
swarms
}
swarm
,
err
:=
NewSwarm
(
context
.
Background
(),
local
,
peerstore
)
if
err
!=
nil
{
t
.
Error
(
err
)
}
var
peers
[]
*
peer
.
Peer
var
listeners
[]
manet
.
Listener
peerNames
:=
map
[
string
]
string
{
func
TestSwarm
(
t
*
testing
.
T
)
{
peers
:=
map
[
string
]
string
{
"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a30"
:
"/ip4/127.0.0.1/tcp/1234"
,
"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a31"
:
"/ip4/127.0.0.1/tcp/2345"
,
"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a32"
:
"/ip4/127.0.0.1/tcp/3456"
,
"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
:
"/ip4/127.0.0.1/tcp/4567"
,
"11140beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a34"
:
"/ip4/127.0.0.1/tcp/5678"
,
}
for
k
,
n
:=
range
peerNames
{
peer
,
err
:=
setupPeer
(
k
,
n
)
ctx
:=
context
.
Background
()
swarms
:=
makeSwarms
(
ctx
,
t
,
peers
)
// connect everyone
for
_
,
s
:=
range
swarms
{
peers
,
err
:=
s
.
peers
.
All
()
if
err
!=
nil
{
t
.
Fatal
(
"error setting up peer"
,
err
)
t
.
Fatal
(
err
)
}
a
:=
peer
.
NetAddress
(
"tcp"
)
if
a
==
nil
{
t
.
Fatal
(
"error setting up peer (addr is nil)"
,
peer
)
for
_
,
p
:=
range
*
peers
{
fmt
.
Println
(
"dialing"
)
if
_
,
err
:=
s
.
Dial
(
p
);
err
!=
nil
{
t
.
Fatal
(
"error swarm dialing to peer"
,
err
)
}
fmt
.
Println
(
"dialed"
)
}
listener
,
err
:=
manet
.
Listen
(
a
)
if
err
!=
nil
{
t
.
Fatal
(
"error setting up listener"
,
err
)
}
// ping/pong
for
_
,
s1
:=
range
swarms
{
ctx
,
cancel
:=
context
.
WithCancel
(
ctx
)
// setup all others to pong
for
_
,
s2
:=
range
swarms
{
if
s1
==
s2
{
continue
}
go
pong
(
ctx
,
s2
)
}
go
pingListen
(
t
,
listener
,
peer
)
_
,
err
=
s
warm
.
Dial
(
peer
)
peers
,
err
:
=
s
1
.
peers
.
All
(
)
if
err
!=
nil
{
t
.
Fatal
(
"error swarm dialing to peer"
,
err
)
t
.
Fatal
(
err
)
}
// ok done, add it.
peers
=
append
(
peers
,
peer
)
listeners
=
append
(
listeners
,
listener
)
}
MsgNum
:=
1000
for
k
:=
0
;
k
<
MsgNum
;
k
++
{
for
_
,
p
:=
range
peers
{
swarm
.
Outgoing
<-
msg
.
New
(
p
,
[]
byte
(
"ping"
))
MsgNum
:=
1000
for
k
:=
0
;
k
<
MsgNum
;
k
++
{
for
_
,
p
:=
range
*
peers
{
s1
.
Outgoing
<-
msg
.
New
(
p
,
[]
byte
(
"ping"
))
}
}
}
got
:=
map
[
u
.
Key
]
int
{}
got
:=
map
[
u
.
Key
]
int
{}
for
k
:=
0
;
k
<
(
MsgNum
*
len
(
*
peers
));
k
++
{
msg
:=
<-
s1
.
Incoming
if
string
(
msg
.
Data
())
!=
"pong"
{
t
.
Error
(
"unexpected conn output"
,
msg
.
Data
)
}
for
k
:=
0
;
k
<
(
MsgNum
*
len
(
peers
));
k
++
{
msg
:=
<-
swarm
.
Incoming
if
string
(
msg
.
Data
())
!=
"pong"
{
t
.
Error
(
"unexpected conn output"
,
msg
.
Data
)
n
,
_
:=
got
[
msg
.
Peer
()
.
Key
()]
got
[
msg
.
Peer
()
.
Key
()]
=
n
+
1
}
n
,
_
:=
got
[
msg
.
Peer
()
.
Key
()]
got
[
msg
.
Peer
()
.
Key
()]
=
n
+
1
}
if
len
(
peers
)
!=
len
(
got
)
{
t
.
Error
(
"got less messages than sent"
)
}
if
len
(
*
peers
)
!=
len
(
got
)
{
t
.
Error
(
"got less messages than sent"
)
}
for
p
,
n
:=
range
got
{
if
n
!=
MsgNum
{
t
.
Error
(
"peer did not get all msgs"
,
p
,
n
,
"/"
,
MsgNum
)
for
p
,
n
:=
range
got
{
if
n
!=
MsgNum
{
t
.
Error
(
"peer did not get all msgs"
,
p
,
n
,
"/"
,
MsgNum
)
}
}
cancel
()
<-
time
.
After
(
50
*
time
.
Millisecond
)
}
swarm
.
Close
()
for
_
,
listener
:=
range
listeners
{
listener
.
Close
()
for
_
,
s
:=
range
swarms
{
s
.
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