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 f8ff158
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
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
91 changes: 91 additions & 0 deletions control/strategy/pool_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//

/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Expand Down Expand Up @@ -26,6 +28,7 @@ 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/smartystreets/goconvey/convey"
)

Expand Down Expand Up @@ -210,3 +213,91 @@ 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() {
var ap1, ap2 AvailablePlugin
var err error

plugin := NewMockAvailablePlugin().WithStrategy(plugin.ConfigRouting)
pool, _ := NewPool(plugin.String(), plugin)

Convey("With given config id, for some task, router is not busy", func() {
ap1, err = pool.SelectAP("TaskID", cfg)
So(ap1, ShouldNotBeNil)
So(err, ShouldBeNil)
})

Convey("With same given config id, for some other task, router is not busy", func() {
ap2, err = pool.SelectAP("AnotherTaskID", cfg)
So(ap2, ShouldNotBeNil)
So(err, ShouldBeNil)
So(ap1, ShouldResemble, ap2)
})

Convey("With another config id, router is busy", func() {
ap, err := pool.SelectAP("YetAnotherTaskID", otherCfg)
So(ap, ShouldBeNil)
So(err, ShouldResemble, ErrCouldNotSelect)
})
})

Convey("When 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, router is not busy", 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() {
var ap1, ap2 AvailablePlugin
var err error

plugin := NewMockAvailablePlugin().WithStrategy(plugin.StickyRouting)
pool, _ := NewPool(plugin.String(), plugin)

Convey("With empty config, for some task, router is not busy", func() {
ap1, err = pool.SelectAP("TaskID", nil)
So(ap1, ShouldNotBeNil)
So(err, ShouldBeNil)
})

Convey("With config, for some task, router is not busy", func() {
cfg := map[string]ctypes.ConfigValue{"foo": ctypes.ConfigValueStr{"bar"}}
ap2, err = pool.SelectAP("TaskID", cfg)
So(ap2, ShouldNotBeNil)
So(err, ShouldBeNil)
So(ap2, ShouldResemble, ap1)
})

Convey("With empty config, for some other task, router is busy", func() {
ap, err := pool.SelectAP("AnotherTaskID", nil)
So(ap, ShouldBeNil)
So(err, ShouldResemble, ErrCouldNotSelect)
})
})
}

0 comments on commit f8ff158

Please sign in to comment.