Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Unit tests added for pool.SelectAP
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-krolik committed May 22, 2016
1 parent 409b4c2 commit 4a6fe7f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion control/strategy/config_based.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions control/strategy/config_based_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build small

/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Expand Down
1 change: 1 addition & 0 deletions control/strategy/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
78 changes: 78 additions & 0 deletions control/strategy/pool_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// +build small

/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Expand Down Expand Up @@ -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"
)

Expand Down Expand Up @@ -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))
})
})
}
2 changes: 1 addition & 1 deletion control/strategy/sticky_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// +build legacy
// +build small

/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Expand Down

0 comments on commit 4a6fe7f

Please sign in to comment.