From 437ee366dc1093f446d172bf7996c4b27da99b14 Mon Sep 17 00:00:00 2001 From: Sir Darkrengarius Date: Fri, 18 Oct 2019 23:46:00 +0300 Subject: [PATCH] Add `Procmanager` tests --- pkg/app2/appserver/proc_manager.go | 3 +- pkg/app2/appserver/proc_manager_test.go | 63 +++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 pkg/app2/appserver/proc_manager_test.go diff --git a/pkg/app2/appserver/proc_manager.go b/pkg/app2/appserver/proc_manager.go index fa891d649..c21532bd8 100644 --- a/pkg/app2/appserver/proc_manager.go +++ b/pkg/app2/appserver/proc_manager.go @@ -14,6 +14,7 @@ import ( var ( errAppAlreadyExists = errors.New("app already exists") + errNoSuchApp = errors.New("no such app") ) // ProcManager allows to manage skywire applications. @@ -117,7 +118,7 @@ func (m *ProcManager) pop(name string) (*Proc, error) { p, ok := m.procs[name] if !ok { - return nil, errors.New("no such app") + return nil, errNoSuchApp } delete(m.procs, name) diff --git a/pkg/app2/appserver/proc_manager_test.go b/pkg/app2/appserver/proc_manager_test.go new file mode 100644 index 000000000..a71da50e3 --- /dev/null +++ b/pkg/app2/appserver/proc_manager_test.go @@ -0,0 +1,63 @@ +package appserver + +import ( + "sort" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/skycoin/skycoin/src/util/logging" +) + +func TestProcManager_Exists(t *testing.T) { + m := NewProcManager(logging.MustGetLogger("proc_manager")) + + appName := "app" + + ok := m.Exists(appName) + require.False(t, ok) + + m.procs[appName] = nil + + ok = m.Exists(appName) + require.True(t, ok) +} + +func TestProcManager_Range(t *testing.T) { + m := NewProcManager(logging.MustGetLogger("proc_manager")) + + wantAppNames := []string{"app1", "app2", "app3"} + + for _, n := range wantAppNames { + m.procs[n] = nil + } + + var gotAppNames []string + next := func(name string, app *Proc) bool { + gotAppNames = append(gotAppNames, name) + require.Nil(t, app) + return true + } + + m.Range(next) + + sort.Strings(gotAppNames) + require.Equal(t, gotAppNames, wantAppNames) +} + +func TestProcManager_Pop(t *testing.T) { + m := NewProcManager(logging.MustGetLogger("proc_manager")) + + appName := "app" + + app, err := m.pop(appName) + require.Equal(t, err, errNoSuchApp) + + m.procs[appName] = nil + + app, err = m.pop(appName) + require.NoError(t, err) + require.Nil(t, app) + _, ok := m.procs[appName] + require.False(t, ok) +}