From 4a6fe7f18f360243b7fb34f51506e47f6a78369d Mon Sep 17 00:00:00 2001 From: Krolik Date: Sun, 22 May 2016 12:48:01 +0200 Subject: [PATCH] Unit tests added for pool.SelectAP --- control/strategy/config_based.go | 2 +- control/strategy/config_based_test.go | 2 + control/strategy/pool.go | 1 + control/strategy/pool_test.go | 78 +++++++++++++++++++++++++++ control/strategy/sticky_test.go | 2 +- 5 files changed, 83 insertions(+), 2 deletions(-) diff --git a/control/strategy/config_based.go b/control/strategy/config_based.go index 91a20ee4e..3bfb0c287 100644 --- a/control/strategy/config_based.go +++ b/control/strategy/config_based.go @@ -29,7 +29,7 @@ import ( "time" ) -// config-based provides a strategy that ... concurrency count is 1 +// config-based provides a strategy that selects plugin based on given config type configBased struct { plugins map[string]AvailablePlugin metricCache map[string]*cache diff --git a/control/strategy/config_based_test.go b/control/strategy/config_based_test.go index 5a0039aa5..3c7c59c27 100644 --- a/control/strategy/config_based_test.go +++ b/control/strategy/config_based_test.go @@ -1,3 +1,5 @@ +// +build small + /* http://www.apache.org/licenses/LICENSE-2.0.txt diff --git a/control/strategy/pool.go b/control/strategy/pool.go index debf5e6cd..e877e5285 100644 --- a/control/strategy/pool.go +++ b/control/strategy/pool.go @@ -372,6 +372,7 @@ func (p *pool) SelectAP(taskID string, config map[string]ctypes.ConfigValue) (Av } func idFromCfg(cfg map[string]ctypes.ConfigValue) string { + //TODO: check for nil map var buff bytes.Buffer enc := gob.NewEncoder(&buff) err := enc.Encode(cfg) diff --git a/control/strategy/pool_test.go b/control/strategy/pool_test.go index b3939f2a9..1be70fbb2 100644 --- a/control/strategy/pool_test.go +++ b/control/strategy/pool_test.go @@ -1,3 +1,5 @@ +// +build small + /* http://www.apache.org/licenses/LICENSE-2.0.txt @@ -26,6 +28,8 @@ import ( "github.com/intelsdi-x/snap/control/plugin" . "github.com/intelsdi-x/snap/control/strategy/fixtures" + "github.com/intelsdi-x/snap/core/ctypes" + "github.com/intelsdi-x/snap/core/serror" . "github.com/smartystreets/goconvey/convey" ) @@ -210,3 +214,77 @@ func TestPoolEligibility(t *testing.T) { }) }) } + +func TestPoolSelectAPDefaultRouter(t *testing.T) { + Convey("For plugin defined with default strategy", t, func() { + plugin := NewMockAvailablePlugin().WithStrategy(plugin.DefaultRouting) + pool, _ := NewPool(plugin.String(), plugin) + + Convey("Then AvailablePlugin is selected", func() { + ap, err := pool.SelectAP("TaskID", nil) + So(ap, ShouldNotBeNil) + So(err, ShouldBeNil) + }) + }) +} + +func TestPoolSelectAPConfigRouter(t *testing.T) { + Convey("Given task id and configuration", t, func() { + cfg := map[string]ctypes.ConfigValue{"foo": ctypes.ConfigValueStr{"bar"}} + otherCfg := map[string]ctypes.ConfigValue{"foo": ctypes.ConfigValueStr{"baz"}} + + Convey("When plugin is defined with config based strategy", func() { + plugin := NewMockAvailablePlugin().WithStrategy(plugin.ConfigRouting) + pool, _ := NewPool(plugin.String(), plugin) + + Convey("Then given routering is handled", func() { + ap, err := pool.SelectAP("TaskID", cfg) + So(ap, ShouldNotBeNil) + So(err, ShouldBeNil) + + ap, err = pool.SelectAP("AnotherTaskID", cfg) + So(ap, ShouldNotBeNil) + So(err, ShouldBeNil) + So(ap, ShouldEqual, plugin) + + ap, err = pool.SelectAP("YetAnotherTaskID", otherCfg) + So(ap, ShouldBeNil) + So(err, ShouldResemble, serror.New(ErrCouldNotSelect)) + }) + }) + + Convey("When another plugin is defined with config based strategy", func() { + plugin := NewMockAvailablePlugin().WithStrategy(plugin.ConfigRouting) + pool, _ := NewPool(plugin.String(), plugin) + + Convey("With empty config, for some task, then routing is handled", func() { + ap, err := pool.SelectAP("TaskID", map[string]ctypes.ConfigValue{}) + So(ap, ShouldNotBeNil) + So(err, ShouldBeNil) + }) + }) + }) +} + +func TestPoolSelectAPStickyRouter(t *testing.T) { + Convey("For plugin defined with sticky strategy", t, func() { + plugin := NewMockAvailablePlugin().WithStrategy(plugin.StickyRouting) + pool, _ := NewPool(plugin.String(), plugin) + + Convey("With empty config, for some task, routering is handled", func() { + ap1, err := pool.SelectAP("TaskID", nil) + So(ap1, ShouldNotBeNil) + So(err, ShouldBeNil) + + cfg := map[string]ctypes.ConfigValue{"foo": ctypes.ConfigValueStr{"bar"}} + ap2, err := pool.SelectAP("TaskID", cfg) + So(ap2, ShouldNotBeNil) + So(err, ShouldBeNil) + So(ap2, ShouldEqual, ap1) + + ap3, err := pool.SelectAP("AnotherTaskID", nil) + So(ap3, ShouldBeNil) + So(err, ShouldResemble, serror.New(ErrCouldNotSelect)) + }) + }) +} diff --git a/control/strategy/sticky_test.go b/control/strategy/sticky_test.go index 5376fe445..0b8d29d97 100644 --- a/control/strategy/sticky_test.go +++ b/control/strategy/sticky_test.go @@ -1,4 +1,4 @@ -// +build legacy +// +build small /* http://www.apache.org/licenses/LICENSE-2.0.txt