Skip to content

Commit

Permalink
Make ProcManager an interface, generate mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkren committed Nov 8, 2019
1 parent d0656e3 commit cd541db
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 12 deletions.
81 changes: 81 additions & 0 deletions pkg/app/appserver/mock_proc_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 21 additions & 9 deletions pkg/app/appserver/proc_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,40 @@ import (
"github.com/SkycoinProject/skywire-mainnet/pkg/app/appcommon"
)

//go:generate mockery -name ProcManager -case underscore -inpkg

var (
errAppAlreadyExists = errors.New("app already exists")
errNoSuchApp = errors.New("no such app")
)

// ProcManager allows to manage skywire applications.
type ProcManager struct {
type ProcManager interface {
Run(log *logging.Logger, c appcommon.Config, args []string, stdout, stderr io.Writer) (appcommon.ProcID, error)
Exists(name string) bool
Stop(name string) error
Wait(name string) error
Range(next func(name string, proc *Proc) bool)
}

// procManager allows to manage skywire applications.
// Implements `ProcManager`.
type procManager struct {
log *logging.Logger
procs map[string]*Proc
mx sync.RWMutex
}

// NewProcManager constructs `ProcManager`.
func NewProcManager(log *logging.Logger) *ProcManager {
return &ProcManager{
func NewProcManager(log *logging.Logger) ProcManager {
return &procManager{
log: log,
procs: make(map[string]*Proc),
}
}

// Run runs the application according to its config and additional args.
func (m *ProcManager) Run(log *logging.Logger, c appcommon.Config, args []string,
func (m *procManager) Run(log *logging.Logger, c appcommon.Config, args []string,
stdout, stderr io.Writer) (appcommon.ProcID, error) {
if m.Exists(c.Name) {
return 0, errAppAlreadyExists
Expand Down Expand Up @@ -64,7 +76,7 @@ func (m *ProcManager) Run(log *logging.Logger, c appcommon.Config, args []string
}

// Exists check whether app exists in the manager instance.
func (m *ProcManager) Exists(name string) bool {
func (m *procManager) Exists(name string) bool {
m.mx.RLock()
defer m.mx.RUnlock()

Expand All @@ -73,7 +85,7 @@ func (m *ProcManager) Exists(name string) bool {
}

// Stop stops the application.
func (m *ProcManager) Stop(name string) error {
func (m *procManager) Stop(name string) error {
p, err := m.pop(name)
if err != nil {
return err
Expand All @@ -83,7 +95,7 @@ func (m *ProcManager) Stop(name string) error {
}

// Wait waits for the application to exit.
func (m *ProcManager) Wait(name string) error {
func (m *procManager) Wait(name string) error {
p, err := m.pop(name)
if err != nil {
return err
Expand All @@ -102,7 +114,7 @@ func (m *ProcManager) Wait(name string) error {

// Range allows to iterate over running skywire apps. Calls `next` on
// each iteration. If `next` returns falls - stops iteration.
func (m *ProcManager) Range(next func(name string, proc *Proc) bool) {
func (m *procManager) Range(next func(name string, proc *Proc) bool) {
m.mx.RLock()
defer m.mx.RUnlock()

Expand All @@ -114,7 +126,7 @@ func (m *ProcManager) Range(next func(name string, proc *Proc) bool) {
}

// pop removes application from the manager instance and returns it.
func (m *ProcManager) pop(name string) (*Proc, error) {
func (m *procManager) pop(name string) (*Proc, error) {
m.mx.Lock()
defer m.mx.Unlock()

Expand Down
2 changes: 0 additions & 2 deletions pkg/app/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type ClientConfig struct {
AppKey appcommon.Key
}

// TODO: move env names to constants

// ClientConfigFromEnv creates client config from the ENV args.
func ClientConfigFromEnv() (ClientConfig, error) {
appKey := os.Getenv(appcommon.EnvAppKey)
Expand Down
2 changes: 1 addition & 1 deletion pkg/visor/visor.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type Node struct {
rpcListener net.Listener
rpcDialers []*noise.RPCClientDialer

procManager *appserver.ProcManager
procManager appserver.ProcManager
}

// NewNode constructs new Node.
Expand Down

0 comments on commit cd541db

Please sign in to comment.