swarm/network/simulation: fix multiple services issue

This commit is contained in:
Janos Guljas 2018-11-22 11:15:28 +01:00
parent f0da48c19a
commit ae922ffb62
3 changed files with 69 additions and 0 deletions

View file

@ -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) {
sim := New(noopServiceFuncMap)
defer sim.Close()

View file

@ -80,6 +80,9 @@ func New(services map[string]ServiceFunc) (s *Simulation) {
adapterServices := make(map[string]adapters.ServiceFunc, len(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)
adapterServices[name] = func(ctx *adapters.ServiceContext) (node.Service, error) {
b := new(sync.Map)

View file

@ -205,3 +205,34 @@ func (t *noopService) Start(server *p2p.Server) error {
func (t *noopService) Stop() error {
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 newNoopService2(), nil, nil
}
// noopService2 is the service that does not do anything
// but implements node.Service interface.
type noopService2 struct{}
func newNoopService2() node.Service {
return &noopService2{}
}
func (t *noopService2) Protocols() []p2p.Protocol {
return []p2p.Protocol{}
}
func (t *noopService2) APIs() []rpc.API {
return []rpc.API{}
}
func (t *noopService2) Start(server *p2p.Server) error {
return nil
}
func (t *noopService2) Stop() error {
return nil
}