Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
18cfe02d
提交
18cfe02d
authored
10月 18, 2014
作者:
Juan Batiz-Benet
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
dht tests with context
上级
0b97d29c
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
60 行增加
和
54 行删除
+60
-54
dht_test.go
routing/dht/dht_test.go
+53
-49
ext_test.go
routing/dht/ext_test.go
+7
-5
没有找到文件。
routing/dht/dht_test.go
浏览文件 @
18cfe02d
...
...
@@ -21,9 +21,7 @@ import (
"time"
)
func
setupDHT
(
t
*
testing
.
T
,
p
*
peer
.
Peer
)
*
IpfsDHT
{
ctx
:=
context
.
Background
()
func
setupDHT
(
ctx
context
.
Context
,
t
*
testing
.
T
,
p
*
peer
.
Peer
)
*
IpfsDHT
{
peerstore
:=
peer
.
NewPeerstore
()
dhts
:=
netservice
.
NewService
(
nil
)
// nil handler for now, need to patch it
...
...
@@ -38,12 +36,12 @@ func setupDHT(t *testing.T, p *peer.Peer) *IpfsDHT {
t
.
Fatal
(
err
)
}
d
:=
NewDHT
(
p
,
peerstore
,
net
,
dhts
,
ds
.
NewMapDatastore
())
d
:=
NewDHT
(
ctx
,
p
,
peerstore
,
net
,
dhts
,
ds
.
NewMapDatastore
())
dhts
.
SetHandler
(
d
)
return
d
}
func
setupDHTS
(
n
int
,
t
*
testing
.
T
)
([]
ma
.
Multiaddr
,
[]
*
peer
.
Peer
,
[]
*
IpfsDHT
)
{
func
setupDHTS
(
ctx
context
.
Context
,
n
int
,
t
*
testing
.
T
)
([]
ma
.
Multiaddr
,
[]
*
peer
.
Peer
,
[]
*
IpfsDHT
)
{
var
addrs
[]
ma
.
Multiaddr
for
i
:=
0
;
i
<
n
;
i
++
{
a
,
err
:=
ma
.
NewMultiaddr
(
fmt
.
Sprintf
(
"/ip4/127.0.0.1/tcp/%d"
,
5000
+
i
))
...
...
@@ -61,7 +59,7 @@ func setupDHTS(n int, t *testing.T) ([]ma.Multiaddr, []*peer.Peer, []*IpfsDHT) {
dhts
:=
make
([]
*
IpfsDHT
,
n
)
for
i
:=
0
;
i
<
n
;
i
++
{
dhts
[
i
]
=
setupDHT
(
t
,
peers
[
i
])
dhts
[
i
]
=
setupDHT
(
ctx
,
t
,
peers
[
i
])
}
return
addrs
,
peers
,
dhts
...
...
@@ -87,7 +85,7 @@ func makePeer(addr ma.Multiaddr) *peer.Peer {
func
TestPing
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
ctx
:=
context
.
Background
()
u
.
Debug
=
false
addrA
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/2222"
)
if
err
!=
nil
{
...
...
@@ -101,28 +99,28 @@ func TestPing(t *testing.T) {
peerA
:=
makePeer
(
addrA
)
peerB
:=
makePeer
(
addrB
)
dhtA
:=
setupDHT
(
t
,
peerA
)
dhtB
:=
setupDHT
(
t
,
peerB
)
dhtA
:=
setupDHT
(
ctx
,
t
,
peerA
)
dhtB
:=
setupDHT
(
ctx
,
t
,
peerB
)
defer
dhtA
.
Halt
()
defer
dhtB
.
Halt
()
defer
dhtA
.
network
.
Close
()
defer
dhtB
.
network
.
Close
()
_
,
err
=
dhtA
.
Connect
(
c
ontext
.
Background
()
,
peerB
)
_
,
err
=
dhtA
.
Connect
(
c
tx
,
peerB
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
//Test that we can ping the node
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
()
,
5
*
time
.
Millisecond
)
err
=
dhtA
.
Ping
(
ctx
,
peerB
)
ctx
T
,
_
:=
context
.
WithTimeout
(
ctx
,
5
*
time
.
Millisecond
)
err
=
dhtA
.
Ping
(
ctx
T
,
peerB
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
ctx
,
_
=
context
.
WithTimeout
(
context
.
Background
()
,
5
*
time
.
Millisecond
)
err
=
dhtB
.
Ping
(
ctx
,
peerA
)
ctx
T
,
_
=
context
.
WithTimeout
(
ctx
,
5
*
time
.
Millisecond
)
err
=
dhtB
.
Ping
(
ctx
T
,
peerA
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -131,6 +129,7 @@ func TestPing(t *testing.T) {
func
TestValueGetSet
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
ctx
:=
context
.
Background
()
u
.
Debug
=
false
addrA
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/1235"
)
if
err
!=
nil
{
...
...
@@ -144,23 +143,23 @@ func TestValueGetSet(t *testing.T) {
peerA
:=
makePeer
(
addrA
)
peerB
:=
makePeer
(
addrB
)
dhtA
:=
setupDHT
(
t
,
peerA
)
dhtB
:=
setupDHT
(
t
,
peerB
)
dhtA
:=
setupDHT
(
ctx
,
t
,
peerA
)
dhtB
:=
setupDHT
(
ctx
,
t
,
peerB
)
defer
dhtA
.
Halt
()
defer
dhtB
.
Halt
()
defer
dhtA
.
network
.
Close
()
defer
dhtB
.
network
.
Close
()
_
,
err
=
dhtA
.
Connect
(
c
ontext
.
Background
()
,
peerB
)
_
,
err
=
dhtA
.
Connect
(
c
tx
,
peerB
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
ctxT
,
_
:=
context
.
WithTimeout
(
c
ontext
.
Background
()
,
time
.
Second
)
ctxT
,
_
:=
context
.
WithTimeout
(
c
tx
,
time
.
Second
)
dhtA
.
PutValue
(
ctxT
,
"hello"
,
[]
byte
(
"world"
))
ctxT
,
_
=
context
.
WithTimeout
(
c
ontext
.
Background
()
,
time
.
Second
*
2
)
ctxT
,
_
=
context
.
WithTimeout
(
c
tx
,
time
.
Second
*
2
)
val
,
err
:=
dhtA
.
GetValue
(
ctxT
,
"hello"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -170,7 +169,7 @@ func TestValueGetSet(t *testing.T) {
t
.
Fatalf
(
"Expected 'world' got '%s'"
,
string
(
val
))
}
ctxT
,
_
=
context
.
WithTimeout
(
c
ontext
.
Background
()
,
time
.
Second
*
2
)
ctxT
,
_
=
context
.
WithTimeout
(
c
tx
,
time
.
Second
*
2
)
val
,
err
=
dhtB
.
GetValue
(
ctxT
,
"hello"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -183,10 +182,11 @@ func TestValueGetSet(t *testing.T) {
func
TestProvides
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
ctx
:=
context
.
Background
()
u
.
Debug
=
false
_
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
_
,
peers
,
dhts
:=
setupDHTS
(
ctx
,
4
,
t
)
defer
func
()
{
for
i
:=
0
;
i
<
4
;
i
++
{
dhts
[
i
]
.
Halt
()
...
...
@@ -194,17 +194,17 @@ func TestProvides(t *testing.T) {
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
1
])
_
,
err
:=
dhts
[
0
]
.
Connect
(
c
tx
,
peers
[
1
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
2
])
_
,
err
=
dhts
[
1
]
.
Connect
(
c
tx
,
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
3
])
_
,
err
=
dhts
[
1
]
.
Connect
(
c
tx
,
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -219,14 +219,14 @@ func TestProvides(t *testing.T) {
t
.
Fatal
(
err
)
}
err
=
dhts
[
3
]
.
Provide
(
c
ontext
.
Background
()
,
u
.
Key
(
"hello"
))
err
=
dhts
[
3
]
.
Provide
(
c
tx
,
u
.
Key
(
"hello"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
time
.
Sleep
(
time
.
Millisecond
*
60
)
ctxT
,
_
:=
context
.
WithTimeout
(
c
ontext
.
Background
()
,
time
.
Second
)
ctxT
,
_
:=
context
.
WithTimeout
(
c
tx
,
time
.
Second
)
provchan
:=
dhts
[
0
]
.
FindProvidersAsync
(
ctxT
,
u
.
Key
(
"hello"
),
1
)
after
:=
time
.
After
(
time
.
Second
)
...
...
@@ -243,9 +243,10 @@ func TestProvides(t *testing.T) {
func
TestProvidesAsync
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
ctx
:=
context
.
Background
()
u
.
Debug
=
false
_
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
_
,
peers
,
dhts
:=
setupDHTS
(
ctx
,
4
,
t
)
defer
func
()
{
for
i
:=
0
;
i
<
4
;
i
++
{
dhts
[
i
]
.
Halt
()
...
...
@@ -253,17 +254,17 @@ func TestProvidesAsync(t *testing.T) {
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
1
])
_
,
err
:=
dhts
[
0
]
.
Connect
(
c
tx
,
peers
[
1
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
2
])
_
,
err
=
dhts
[
1
]
.
Connect
(
c
tx
,
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
3
])
_
,
err
=
dhts
[
1
]
.
Connect
(
c
tx
,
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -278,21 +279,21 @@ func TestProvidesAsync(t *testing.T) {
t
.
Fatal
(
err
)
}
err
=
dhts
[
3
]
.
Provide
(
c
ontext
.
Background
()
,
u
.
Key
(
"hello"
))
err
=
dhts
[
3
]
.
Provide
(
c
tx
,
u
.
Key
(
"hello"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
time
.
Sleep
(
time
.
Millisecond
*
60
)
ctx
,
_
:=
context
.
WithTimeout
(
context
.
TODO
()
,
time
.
Millisecond
*
300
)
provs
:=
dhts
[
0
]
.
FindProvidersAsync
(
ctx
,
u
.
Key
(
"hello"
),
5
)
ctx
T
,
_
:=
context
.
WithTimeout
(
ctx
,
time
.
Millisecond
*
300
)
provs
:=
dhts
[
0
]
.
FindProvidersAsync
(
ctx
T
,
u
.
Key
(
"hello"
),
5
)
select
{
case
p
:=
<-
provs
:
if
!
p
.
ID
.
Equal
(
dhts
[
3
]
.
self
.
ID
)
{
t
.
Fatalf
(
"got a provider, but not the right one. %s"
,
p
)
}
case
<-
ctx
.
Done
()
:
case
<-
ctx
T
.
Done
()
:
t
.
Fatal
(
"Didnt get back providers"
)
}
}
...
...
@@ -300,8 +301,9 @@ func TestProvidesAsync(t *testing.T) {
func
TestLayeredGet
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
ctx
:=
context
.
Background
()
u
.
Debug
=
false
_
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
_
,
peers
,
dhts
:=
setupDHTS
(
ctx
,
4
,
t
)
defer
func
()
{
for
i
:=
0
;
i
<
4
;
i
++
{
dhts
[
i
]
.
Halt
()
...
...
@@ -309,17 +311,17 @@ func TestLayeredGet(t *testing.T) {
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
1
])
_
,
err
:=
dhts
[
0
]
.
Connect
(
c
tx
,
peers
[
1
])
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to connect: %s"
,
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
2
])
_
,
err
=
dhts
[
1
]
.
Connect
(
c
tx
,
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
3
])
_
,
err
=
dhts
[
1
]
.
Connect
(
c
tx
,
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -329,14 +331,14 @@ func TestLayeredGet(t *testing.T) {
t
.
Fatal
(
err
)
}
err
=
dhts
[
3
]
.
Provide
(
c
ontext
.
Background
()
,
u
.
Key
(
"hello"
))
err
=
dhts
[
3
]
.
Provide
(
c
tx
,
u
.
Key
(
"hello"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
time
.
Sleep
(
time
.
Millisecond
*
60
)
ctxT
,
_
:=
context
.
WithTimeout
(
c
ontext
.
Background
()
,
time
.
Second
)
ctxT
,
_
:=
context
.
WithTimeout
(
c
tx
,
time
.
Second
)
val
,
err
:=
dhts
[
0
]
.
GetValue
(
ctxT
,
u
.
Key
(
"hello"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -351,9 +353,10 @@ func TestLayeredGet(t *testing.T) {
func
TestFindPeer
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
ctx
:=
context
.
Background
()
u
.
Debug
=
false
_
,
peers
,
dhts
:=
setupDHTS
(
4
,
t
)
_
,
peers
,
dhts
:=
setupDHTS
(
ctx
,
4
,
t
)
defer
func
()
{
for
i
:=
0
;
i
<
4
;
i
++
{
dhts
[
i
]
.
Halt
()
...
...
@@ -361,22 +364,22 @@ func TestFindPeer(t *testing.T) {
}
}()
_
,
err
:=
dhts
[
0
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
1
])
_
,
err
:=
dhts
[
0
]
.
Connect
(
c
tx
,
peers
[
1
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
2
])
_
,
err
=
dhts
[
1
]
.
Connect
(
c
tx
,
peers
[
2
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
_
,
err
=
dhts
[
1
]
.
Connect
(
c
ontext
.
Background
()
,
peers
[
3
])
_
,
err
=
dhts
[
1
]
.
Connect
(
c
tx
,
peers
[
3
])
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
ctxT
,
_
:=
context
.
WithTimeout
(
c
ontext
.
Background
()
,
time
.
Second
)
ctxT
,
_
:=
context
.
WithTimeout
(
c
tx
,
time
.
Second
)
p
,
err
:=
dhts
[
0
]
.
FindPeer
(
ctxT
,
peers
[
2
]
.
ID
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -394,6 +397,7 @@ func TestFindPeer(t *testing.T) {
func
TestConnectCollision
(
t
*
testing
.
T
)
{
// t.Skip("skipping test to debug another")
ctx
:=
context
.
Background
()
u
.
Debug
=
false
addrA
,
err
:=
ma
.
NewMultiaddr
(
"/ip4/127.0.0.1/tcp/1235"
)
if
err
!=
nil
{
...
...
@@ -407,8 +411,8 @@ func TestConnectCollision(t *testing.T) {
peerA
:=
makePeer
(
addrA
)
peerB
:=
makePeer
(
addrB
)
dhtA
:=
setupDHT
(
t
,
peerA
)
dhtB
:=
setupDHT
(
t
,
peerB
)
dhtA
:=
setupDHT
(
ctx
,
t
,
peerA
)
dhtB
:=
setupDHT
(
ctx
,
t
,
peerB
)
defer
dhtA
.
Halt
()
defer
dhtB
.
Halt
()
...
...
@@ -417,14 +421,14 @@ func TestConnectCollision(t *testing.T) {
done
:=
make
(
chan
struct
{})
go
func
()
{
_
,
err
=
dhtA
.
Connect
(
c
ontext
.
Background
()
,
peerB
)
_
,
err
=
dhtA
.
Connect
(
c
tx
,
peerB
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
done
<-
struct
{}{}
}()
go
func
()
{
_
,
err
=
dhtB
.
Connect
(
c
ontext
.
Background
()
,
peerA
)
_
,
err
=
dhtB
.
Connect
(
c
tx
,
peerA
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
routing/dht/ext_test.go
浏览文件 @
18cfe02d
...
...
@@ -110,7 +110,7 @@ func TestGetFailures(t *testing.T) {
local
:=
new
(
peer
.
Peer
)
local
.
ID
=
peer
.
ID
(
"test_peer"
)
d
:=
NewDHT
(
local
,
peerstore
,
fn
,
fs
,
ds
.
NewMapDatastore
())
d
:=
NewDHT
(
ctx
,
local
,
peerstore
,
fn
,
fs
,
ds
.
NewMapDatastore
())
other
:=
&
peer
.
Peer
{
ID
:
peer
.
ID
(
"other_peer"
)}
d
.
Update
(
other
)
...
...
@@ -200,6 +200,7 @@ func _randPeer() *peer.Peer {
func
TestNotFound
(
t
*
testing
.
T
)
{
// t.Skip("skipping test because it makes a lot of output")
ctx
:=
context
.
Background
()
fn
:=
&
fauxNet
{}
fs
:=
&
fauxSender
{}
...
...
@@ -207,7 +208,7 @@ func TestNotFound(t *testing.T) {
local
.
ID
=
peer
.
ID
(
"test_peer"
)
peerstore
:=
peer
.
NewPeerstore
()
d
:=
NewDHT
(
local
,
peerstore
,
fn
,
fs
,
ds
.
NewMapDatastore
())
d
:=
NewDHT
(
ctx
,
local
,
peerstore
,
fn
,
fs
,
ds
.
NewMapDatastore
())
var
ps
[]
*
peer
.
Peer
for
i
:=
0
;
i
<
5
;
i
++
{
...
...
@@ -243,7 +244,7 @@ func TestNotFound(t *testing.T) {
})
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
()
,
time
.
Second
*
5
)
ctx
,
_
=
context
.
WithTimeout
(
ctx
,
time
.
Second
*
5
)
v
,
err
:=
d
.
GetValue
(
ctx
,
u
.
Key
(
"hello"
))
log
.
Debug
(
"get value got %v"
,
v
)
if
err
!=
nil
{
...
...
@@ -265,6 +266,7 @@ func TestNotFound(t *testing.T) {
func
TestLessThanKResponses
(
t
*
testing
.
T
)
{
// t.Skip("skipping test because it makes a lot of output")
ctx
:=
context
.
Background
()
u
.
Debug
=
false
fn
:=
&
fauxNet
{}
fs
:=
&
fauxSender
{}
...
...
@@ -272,7 +274,7 @@ func TestLessThanKResponses(t *testing.T) {
local
:=
new
(
peer
.
Peer
)
local
.
ID
=
peer
.
ID
(
"test_peer"
)
d
:=
NewDHT
(
local
,
peerstore
,
fn
,
fs
,
ds
.
NewMapDatastore
())
d
:=
NewDHT
(
ctx
,
local
,
peerstore
,
fn
,
fs
,
ds
.
NewMapDatastore
())
var
ps
[]
*
peer
.
Peer
for
i
:=
0
;
i
<
5
;
i
++
{
...
...
@@ -307,7 +309,7 @@ func TestLessThanKResponses(t *testing.T) {
})
ctx
,
_
:=
context
.
WithTimeout
(
context
.
Background
()
,
time
.
Second
*
30
)
ctx
,
_
=
context
.
WithTimeout
(
ctx
,
time
.
Second
*
30
)
_
,
err
:=
d
.
GetValue
(
ctx
,
u
.
Key
(
"hello"
))
if
err
!=
nil
{
switch
err
{
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论