Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
93854bba
Commit
93854bba
authored
Nov 26, 2018
by
Janoš Guljaš
Committed by
Anton Evangelatov
Nov 26, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
swarm/network/simulation: fix New function for-loop scope (#18161)
parent
f0515800
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
56 additions
and
1 deletion
+56
-1
node_test.go
swarm/network/simulation/node_test.go
+35
-0
simulation.go
swarm/network/simulation/simulation.go
+7
-0
simulation_test.go
swarm/network/simulation/simulation_test.go
+13
-0
delivery.go
swarm/network/stream/delivery.go
+1
-1
No files found.
swarm/network/simulation/node_test.go
View file @
93854bba
...
@@ -160,6 +160,41 @@ func TestAddNodeWithService(t *testing.T) {
...
@@ -160,6 +160,41 @@ func TestAddNodeWithService(t *testing.T) {
}
}
}
}
func
TestAddNodeMultipleServices
(
t
*
testing
.
T
)
{
sim
:=
New
(
map
[
string
]
ServiceFunc
{
"noop1"
:
noopServiceFunc
,
"noop2"
:
noopService2Func
,
})
defer
sim
.
Close
()
id
,
err
:=
sim
.
AddNode
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
n
:=
sim
.
Net
.
GetNode
(
id
)
.
Node
.
(
*
adapters
.
SimNode
)
if
n
.
Service
(
"noop1"
)
==
nil
{
t
.
Error
(
"service noop1 not found on node"
)
}
if
n
.
Service
(
"noop2"
)
==
nil
{
t
.
Error
(
"service noop2 not found on node"
)
}
}
func
TestAddNodeDuplicateServiceError
(
t
*
testing
.
T
)
{
sim
:=
New
(
map
[
string
]
ServiceFunc
{
"noop1"
:
noopServiceFunc
,
"noop2"
:
noopServiceFunc
,
})
defer
sim
.
Close
()
wantErr
:=
"duplicate service: *simulation.noopService"
_
,
err
:=
sim
.
AddNode
()
if
err
.
Error
()
!=
wantErr
{
t
.
Errorf
(
"got error %q, want %q"
,
err
,
wantErr
)
}
}
func
TestAddNodes
(
t
*
testing
.
T
)
{
func
TestAddNodes
(
t
*
testing
.
T
)
{
sim
:=
New
(
noopServiceFuncMap
)
sim
:=
New
(
noopServiceFuncMap
)
defer
sim
.
Close
()
defer
sim
.
Close
()
...
...
swarm/network/simulation/simulation.go
View file @
93854bba
...
@@ -68,6 +68,10 @@ type ServiceFunc func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Se
...
@@ -68,6 +68,10 @@ type ServiceFunc func(ctx *adapters.ServiceContext, bucket *sync.Map) (s node.Se
// New creates a new Simulation instance with new
// New creates a new Simulation instance with new
// simulations.Network initialized with provided services.
// simulations.Network initialized with provided services.
// Services map must have unique keys as service names and
// every ServiceFunc must return a node.Service of the unique type.
// This restriction is required by node.Node.Start() function
// which is used to start node.Service returned by ServiceFunc.
func
New
(
services
map
[
string
]
ServiceFunc
)
(
s
*
Simulation
)
{
func
New
(
services
map
[
string
]
ServiceFunc
)
(
s
*
Simulation
)
{
s
=
&
Simulation
{
s
=
&
Simulation
{
buckets
:
make
(
map
[
enode
.
ID
]
*
sync
.
Map
),
buckets
:
make
(
map
[
enode
.
ID
]
*
sync
.
Map
),
...
@@ -76,6 +80,9 @@ func New(services map[string]ServiceFunc) (s *Simulation) {
...
@@ -76,6 +80,9 @@ func New(services map[string]ServiceFunc) (s *Simulation) {
adapterServices
:=
make
(
map
[
string
]
adapters
.
ServiceFunc
,
len
(
services
))
adapterServices
:=
make
(
map
[
string
]
adapters
.
ServiceFunc
,
len
(
services
))
for
name
,
serviceFunc
:=
range
services
{
for
name
,
serviceFunc
:=
range
services
{
// Scope this variables correctly
// as they will be in the adapterServices[name] function accessed later.
name
,
serviceFunc
:=
name
,
serviceFunc
s
.
serviceNames
=
append
(
s
.
serviceNames
,
name
)
s
.
serviceNames
=
append
(
s
.
serviceNames
,
name
)
adapterServices
[
name
]
=
func
(
ctx
*
adapters
.
ServiceContext
)
(
node
.
Service
,
error
)
{
adapterServices
[
name
]
=
func
(
ctx
*
adapters
.
ServiceContext
)
(
node
.
Service
,
error
)
{
b
:=
new
(
sync
.
Map
)
b
:=
new
(
sync
.
Map
)
...
...
swarm/network/simulation/simulation_test.go
View file @
93854bba
...
@@ -205,3 +205,16 @@ func (t *noopService) Start(server *p2p.Server) error {
...
@@ -205,3 +205,16 @@ func (t *noopService) Start(server *p2p.Server) error {
func
(
t
*
noopService
)
Stop
()
error
{
func
(
t
*
noopService
)
Stop
()
error
{
return
nil
return
nil
}
}
// a helper function for most basic noop service
// of a different type then noopService to test
// multiple services on one node.
func
noopService2Func
(
ctx
*
adapters
.
ServiceContext
,
b
*
sync
.
Map
)
(
node
.
Service
,
func
(),
error
)
{
return
new
(
noopService2
),
nil
,
nil
}
// noopService2 is the service that does not do anything
// but implements node.Service interface.
type
noopService2
struct
{
noopService
}
swarm/network/stream/delivery.go
View file @
93854bba
...
@@ -255,7 +255,7 @@ func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (
...
@@ -255,7 +255,7 @@ func (d *Delivery) RequestFromPeers(ctx context.Context, req *network.Request) (
}
}
sp
=
d
.
getPeer
(
id
)
sp
=
d
.
getPeer
(
id
)
if
sp
==
nil
{
if
sp
==
nil
{
log
.
Warn
(
"Delivery.RequestFromPeers: peer not found"
,
"id"
,
id
)
//
log.Warn("Delivery.RequestFromPeers: peer not found", "id", id)
return
true
return
true
}
}
spID
=
&
id
spID
=
&
id
...
...
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