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-dms3-routing
Commits
f386e017
Commit
f386e017
authored
Sep 23, 2014
by
Jeromy
Committed by
Brian Tiger Chow
Sep 23, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
move mock routing tests to proper directory
parent
0febff30
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
155 additions
and
1 deletion
+155
-1
mock/routing.go
mock/routing.go
+1
-1
mock/routing_test.go
mock/routing_test.go
+154
-0
No files found.
mock/routing.go
View file @
f386e017
package
mock
router
package
mock
import
(
"errors"
...
...
mock/routing_test.go
0 → 100644
View file @
f386e017
package
mock
import
(
"bytes"
"testing"
context
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
"github.com/jbenet/go-ipfs/peer"
u
"github.com/jbenet/go-ipfs/util"
)
func
TestKeyNotFound
(
t
*
testing
.
T
)
{
vrs
:=
VirtualRoutingServer
()
empty
:=
vrs
.
Providers
(
u
.
Key
(
"not there"
))
if
len
(
empty
)
!=
0
{
t
.
Fatal
(
"should be empty"
)
}
}
func
TestSetAndGet
(
t
*
testing
.
T
)
{
pid
:=
peer
.
ID
([]
byte
(
"the peer id"
))
p
:=
&
peer
.
Peer
{
ID
:
pid
,
}
k
:=
u
.
Key
(
"42"
)
rs
:=
VirtualRoutingServer
()
err
:=
rs
.
Announce
(
p
,
k
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
providers
:=
rs
.
Providers
(
k
)
if
len
(
providers
)
!=
1
{
t
.
Fatal
(
"should be one"
)
}
for
_
,
elem
:=
range
providers
{
if
bytes
.
Equal
(
elem
.
ID
,
pid
)
{
return
}
}
t
.
Fatal
(
"ID should have matched"
)
}
func
TestClientFindProviders
(
t
*
testing
.
T
)
{
peer
:=
&
peer
.
Peer
{
ID
:
[]
byte
(
"42"
)}
rs
:=
VirtualRoutingServer
()
client
:=
rs
.
Client
(
peer
)
k
:=
u
.
Key
(
"hello"
)
err
:=
client
.
Provide
(
context
.
Background
(),
k
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
max
:=
100
providersFromHashTable
:=
rs
.
Providers
(
k
)
isInHT
:=
false
for
_
,
p
:=
range
providersFromHashTable
{
if
bytes
.
Equal
(
p
.
ID
,
peer
.
ID
)
{
isInHT
=
true
}
}
if
!
isInHT
{
t
.
Fatal
(
"Despite client providing key, peer wasn't in hash table as a provider"
)
}
providersFromClient
:=
client
.
FindProvidersAsync
(
context
.
Background
(),
u
.
Key
(
"hello"
),
max
)
isInClient
:=
false
for
p
:=
range
providersFromClient
{
if
bytes
.
Equal
(
p
.
ID
,
peer
.
ID
)
{
isInClient
=
true
}
}
if
!
isInClient
{
t
.
Fatal
(
"Despite client providing key, client didn't receive peer when finding providers"
)
}
}
func
TestClientOverMax
(
t
*
testing
.
T
)
{
rs
:=
VirtualRoutingServer
()
k
:=
u
.
Key
(
"hello"
)
numProvidersForHelloKey
:=
100
for
i
:=
0
;
i
<
numProvidersForHelloKey
;
i
++
{
peer
:=
&
peer
.
Peer
{
ID
:
[]
byte
(
string
(
i
)),
}
err
:=
rs
.
Announce
(
peer
,
k
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
providersFromHashTable
:=
rs
.
Providers
(
k
)
if
len
(
providersFromHashTable
)
!=
numProvidersForHelloKey
{
t
.
Log
(
1
==
len
(
providersFromHashTable
))
t
.
Fatal
(
"not all providers were returned"
)
}
max
:=
10
peer
:=
&
peer
.
Peer
{
ID
:
[]
byte
(
"TODO"
)}
client
:=
rs
.
Client
(
peer
)
providersFromClient
:=
client
.
FindProvidersAsync
(
context
.
Background
(),
k
,
max
)
i
:=
0
for
_
=
range
providersFromClient
{
i
++
}
if
i
!=
max
{
t
.
Fatal
(
"Too many providers returned"
)
}
}
// TODO does dht ensure won't receive self as a provider? probably not.
func
TestCanceledContext
(
t
*
testing
.
T
)
{
rs
:=
VirtualRoutingServer
()
k
:=
u
.
Key
(
"hello"
)
t
.
Log
(
"async'ly announce infinite stream of providers for key"
)
i
:=
0
go
func
()
{
// infinite stream
for
{
peer
:=
&
peer
.
Peer
{
ID
:
[]
byte
(
string
(
i
)),
}
err
:=
rs
.
Announce
(
peer
,
k
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
i
++
}
}()
local
:=
&
peer
.
Peer
{
ID
:
[]
byte
(
"peer id doesn't matter"
)}
client
:=
rs
.
Client
(
local
)
t
.
Log
(
"warning: max is finite so this test is non-deterministic"
)
t
.
Log
(
"context cancellation could simply take lower priority"
)
t
.
Log
(
"and result in receiving the max number of results"
)
max
:=
1000
t
.
Log
(
"cancel the context before consuming"
)
ctx
,
cancelFunc
:=
context
.
WithCancel
(
context
.
Background
())
cancelFunc
()
providers
:=
client
.
FindProvidersAsync
(
ctx
,
k
,
max
)
numProvidersReturned
:=
0
for
_
=
range
providers
{
numProvidersReturned
++
}
t
.
Log
(
numProvidersReturned
)
if
numProvidersReturned
==
max
{
t
.
Fatal
(
"Context cancel had no effect"
)
}
}
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