Skip to content

Commit

Permalink
Refactor and test creation of moduler instances
Browse files Browse the repository at this point in the history
  • Loading branch information
ruflin committed Mar 8, 2016
1 parent 7de9604 commit 339e708
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 9 deletions.
2 changes: 1 addition & 1 deletion metricbeat/helper/metricset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type MockMetricSeter struct {
}

func (m *MockMetricSeter) Fetch(ms *MetricSet) (events []common.MapStr, err error) {
m.counter = m.counter + 1
m.counter += 1

event := common.MapStr{
"counter": m.counter,
Expand Down
4 changes: 2 additions & 2 deletions metricbeat/helper/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type Module struct {
}

// NewModule creates a new module
func NewModule(cfg *ucfg.Config, moduler Moduler) (*Module, error) {
func NewModule(cfg *ucfg.Config, moduler func() Moduler) (*Module, error) {

// Module config defaults
config := ModuleConfig{
Expand All @@ -52,7 +52,7 @@ func NewModule(cfg *ucfg.Config, moduler Moduler) (*Module, error) {
name: config.Module,
Config: config,
cfg: cfg,
moduler: moduler,
moduler: moduler(),
metricSets: map[string]*MetricSet{},
wg: sync.WaitGroup{},
done: make(chan struct{}),
Expand Down
47 changes: 47 additions & 0 deletions metricbeat/helper/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/urso/ucfg"
"reflect"
)

func TestGetMetricSetsList(t *testing.T) {
Expand All @@ -19,3 +21,48 @@ func TestGetMetricSetsList(t *testing.T) {
assert.Equal(t, "test1, test2", module.getMetricSetsList())

}

func TestNewModule(t *testing.T) {

config, _ := ucfg.NewFrom(ModuleConfig{
Module: "test",
})

module, err := NewModule(config, NewMockModuler)
assert.NoError(t, err)
assert.NotNil(t, module)

err = module.moduler.Setup(config)
assert.NoError(t, err)
}

// Check that the moduler inside each module is a different instance
func TestNewModulerDifferentInstance(t *testing.T) {

config, _ := ucfg.NewFrom(ModuleConfig{
Module: "test",
})

module1, err := NewModule(config, NewMockModuler)
assert.NoError(t, err)
module2, err := NewModule(config, NewMockModuler)
assert.NoError(t, err)

module1.moduler.Setup(config)
assert.False(t, reflect.DeepEqual(module1.moduler, module2.moduler))
assert.True(t, reflect.DeepEqual(module1.moduler, module1.moduler))
}

// New creates new instance of Moduler
func NewMockModuler() Moduler {
return &MockModuler{}
}

type MockModuler struct {
counter int
}

func (m *MockModuler) Setup(cfg *ucfg.Config) error {
m.counter += 1
return nil
}
6 changes: 3 additions & 3 deletions metricbeat/helper/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
var Registry = Register{}

type Register struct {
Modulers map[string]Moduler
Modulers map[string]func() Moduler
MetricSeters map[string]map[string]func() MetricSeter
}

// AddModule registers the given module with the registry
func (r *Register) AddModuler(name string, m Moduler) {
func (r *Register) AddModuler(name string, m func() Moduler) {

if r.Modulers == nil {
r.Modulers = map[string]Moduler{}
r.Modulers = map[string]func() Moduler{}
}

logp.Info("Register module: %s", name)
Expand Down
7 changes: 6 additions & 1 deletion metricbeat/module/apache/apache.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
)

func init() {
helper.Registry.AddModuler("apache", Moduler{})
helper.Registry.AddModuler("apache", New)
}

// New creates new instance of Moduler
func New() helper.Moduler {
return &Moduler{}
}

type Moduler struct{}
Expand Down
7 changes: 6 additions & 1 deletion metricbeat/module/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ import (
)

func init() {
helper.Registry.AddModuler("mysql", Moduler{})
helper.Registry.AddModuler("mysql", New)
}

// New creates new instance of Moduler
func New() helper.Moduler {
return &Moduler{}
}

type Moduler struct{}
Expand Down
7 changes: 6 additions & 1 deletion metricbeat/module/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import (
)

func init() {
helper.Registry.AddModuler("redis", Moduler{})
helper.Registry.AddModuler("redis", New)
}

// New creates new instance of Moduler
func New() helper.Moduler {
return &Moduler{}
}

type Moduler struct{}
Expand Down

0 comments on commit 339e708

Please sign in to comment.