Skip to content

Commit

Permalink
service: simplify Stop() & improve errors
Browse files Browse the repository at this point in the history
* return Stop() errors for all dependencies, not just the first one.
* removed nil check for Service.Configuration, no need for backward compatibility anymore.
* removed unneeded TestStopDependency() & TestIntegrationStopDependency() tests.
  • Loading branch information
ilgooz committed Apr 16, 2019
1 parent 6a42f33 commit 2fb5770
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 80 deletions.
47 changes: 16 additions & 31 deletions service/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sync"

"github.com/mesg-foundation/core/container"
"github.com/mesg-foundation/core/x/xerrors"
)

// Stop stops a service.
Expand All @@ -13,39 +14,23 @@ func (s *Service) Stop(c container.Container) error {
return err
}

if err := s.StopDependencies(c); err != nil {
return err
}
return c.DeleteNetwork(s.namespace())
}

// StopDependencies stops all dependencies.
func (s *Service) StopDependencies(c container.Container) error {
var mutex sync.Mutex
var wg sync.WaitGroup
var err error
stop := func(d *Dependency) {
defer wg.Done()
errStop := d.Stop(c, s)
mutex.Lock()
defer mutex.Unlock()
if errStop != nil && err == nil {
err = errStop
}
}
if s.Configuration != nil {
var (
wg sync.WaitGroup
errs xerrors.SyncErrors
)
for _, d := range append(s.Dependencies, s.Configuration) {
wg.Add(1)
go stop(s.Configuration)
}
for _, dep := range s.Dependencies {
wg.Add(1)
go stop(dep)
go func(namespace []string) {
defer wg.Done()
if err := c.StopService(namespace); err != nil {
errs.Append(err)
}
}(d.namespace(s.namespace()))
}
wg.Wait()
return err
}
if err := errs.ErrorOrNil(); err != nil {
return err
}

// Stop stops a dependency.
func (d *Dependency) Stop(c container.Container, s *Service) error {
return c.StopService(d.namespace(s.namespace()))
return c.DeleteNetwork(s.namespace())
}
27 changes: 0 additions & 27 deletions service/stop_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package service
import (
"testing"

"github.com/mesg-foundation/core/container"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -52,32 +51,6 @@ func TestIntegrationStopNonRunningService(t *testing.T) {
require.Equal(t, STOPPED, status)
}

func TestIntegrationStopDependency(t *testing.T) {
var (
service = &Service{
Hash: "1",
Name: "TestStopDependency",
Dependencies: []*Dependency{
{
Key: "test",
Image: "http-server",
},
},
}
c = newIntegrationContainer(t)
)

networkID, err := c.CreateNetwork(service.namespace())
require.NoError(t, err)
defer c.DeleteNetwork(service.namespace())
dep := service.Dependencies[0]
dep.Start(c, service, networkID)
err = dep.Stop(c, service)
require.NoError(t, err)
status, _ := dep.Status(c, service)
require.Equal(t, container.STOPPED, status)
}

func TestIntegrationNetworkDeleted(t *testing.T) {
var (
service = &Service{
Expand Down
22 changes: 0 additions & 22 deletions service/stop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,3 @@ func TestStopRunningService(t *testing.T) {

mc.AssertExpectations(t)
}

func TestStopDependency(t *testing.T) {
var (
dependencyKey = "1"
s = &Service{
Hash: "1",
Name: "TestStopService",
Dependencies: []*Dependency{
{
Key: dependencyKey,
Image: "http-server",
},
},
}
mc = &mocks.Container{}
)

d, _ := s.getDependency(dependencyKey)
mc.On("StopService", d.namespace(s.namespace())).Once().Return(nil)
require.NoError(t, d.Stop(mc, s))
mc.AssertExpectations(t)
}

0 comments on commit 2fb5770

Please sign in to comment.