Skip to content

Commit 5b6fa52

Browse files
ipchamaipchama
andauthored
Update/tests (#20)
* Some really basic testing for the generator. * Basic tests for handler and stat factories. Co-authored-by: ipchama <[email protected]>
1 parent 81ac517 commit 5b6fa52

File tree

3 files changed

+187
-0
lines changed

3 files changed

+187
-0
lines changed

generator/factory_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package generator_test
2+
3+
import (
4+
"github.com/ipchama/dhammer/generator"
5+
"github.com/ipchama/dhammer/stats"
6+
"testing"
7+
)
8+
9+
type TestHammerConfig struct {
10+
hType string
11+
}
12+
13+
func (t *TestHammerConfig) HammerType() string {
14+
return t.hType
15+
}
16+
17+
type TestGenerator struct {
18+
}
19+
20+
func (t *TestGenerator) Init() error {
21+
return nil
22+
}
23+
24+
func (t *TestGenerator) Update(i interface{}) error {
25+
return nil
26+
}
27+
28+
func (t *TestGenerator) Run() {
29+
}
30+
31+
func (t *TestGenerator) Stop() error {
32+
return nil
33+
}
34+
35+
func (t *TestGenerator) DeInit() error {
36+
return nil
37+
}
38+
39+
func TestNew(t *testing.T) {
40+
41+
o := &TestHammerConfig{
42+
hType: "__TEST__",
43+
}
44+
45+
if _, err := generator.New(nil, o, func(string) bool { return true }, func(error) bool { return true }, func(stats.StatValue) bool { return true }); err == nil {
46+
t.Errorf("Generator factory did not return error for unknown type.")
47+
}
48+
49+
if err := generator.AddGenerator(o.hType, func(g generator.GeneratorInitParams) generator.Generator { return &TestGenerator{} }); err != nil {
50+
t.Errorf("Generator factory failed to add new type.")
51+
}
52+
53+
if err := generator.AddGenerator(o.hType, func(g generator.GeneratorInitParams) generator.Generator { return &TestGenerator{} }); err == nil {
54+
t.Errorf("Generator factory allowed duplicate type.")
55+
}
56+
57+
if _, err := generator.New(nil, o, func(string) bool { return true }, func(error) bool { return true }, func(stats.StatValue) bool { return true }); err != nil {
58+
t.Errorf("Generator factory failed to return known type.")
59+
}
60+
61+
}

handler/factory_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package handler_test
2+
3+
import (
4+
"github.com/ipchama/dhammer/handler"
5+
"github.com/ipchama/dhammer/message"
6+
"github.com/ipchama/dhammer/stats"
7+
"testing"
8+
)
9+
10+
type TestHammerConfig struct {
11+
hType string
12+
}
13+
14+
func (t *TestHammerConfig) HammerType() string {
15+
return t.hType
16+
}
17+
18+
type TestHandler struct {
19+
}
20+
21+
func (t *TestHandler) Init() error {
22+
return nil
23+
}
24+
25+
func (t *TestHandler) ReceiveMessage(m message.Message) bool {
26+
return true
27+
}
28+
29+
func (t *TestHandler) Run() {
30+
}
31+
32+
func (t *TestHandler) Stop() error {
33+
return nil
34+
}
35+
36+
func (t *TestHandler) DeInit() error {
37+
return nil
38+
}
39+
40+
func TestNew(t *testing.T) {
41+
42+
o := &TestHammerConfig{
43+
hType: "__TEST__",
44+
}
45+
46+
if _, err := handler.New(nil, o, func(string) bool { return true }, func(error) bool { return true }, func(stats.StatValue) bool { return true }); err == nil {
47+
t.Errorf("Handler factory did not return error for unknown type.")
48+
}
49+
50+
if err := handler.AddHandler(o.hType, func(h handler.HandlerInitParams) handler.Handler { return &TestHandler{} }); err != nil {
51+
t.Errorf("Handler factory failed to add new type.")
52+
}
53+
54+
if err := handler.AddHandler(o.hType, func(h handler.HandlerInitParams) handler.Handler { return &TestHandler{} }); err == nil {
55+
t.Errorf("Handler factory allowed duplicate type.")
56+
}
57+
58+
if _, err := handler.New(nil, o, func(string) bool { return true }, func(error) bool { return true }, func(stats.StatValue) bool { return true }); err != nil {
59+
t.Errorf("Handler factory failed to return known type.")
60+
}
61+
62+
}

stats/factory_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package stats_test
2+
3+
import (
4+
"github.com/ipchama/dhammer/stats"
5+
"testing"
6+
)
7+
8+
type TestHammerConfig struct {
9+
hType string
10+
}
11+
12+
func (t *TestHammerConfig) HammerType() string {
13+
return t.hType
14+
}
15+
16+
type TestStats struct {
17+
}
18+
19+
func (t *TestStats) Init() error {
20+
return nil
21+
}
22+
23+
func (t *TestStats) AddStat(s stats.StatValue) bool {
24+
return true
25+
}
26+
27+
func (t *TestStats) Run() {
28+
}
29+
30+
func (t *TestStats) String() string {
31+
return ""
32+
}
33+
34+
func (t *TestStats) Stop() error {
35+
return nil
36+
}
37+
38+
func (t *TestStats) DeInit() error {
39+
return nil
40+
}
41+
42+
func TestNew(t *testing.T) {
43+
44+
o := &TestHammerConfig{
45+
hType: "__TEST__",
46+
}
47+
48+
if _, err := stats.New(o, func(string) bool { return true }, func(error) bool { return true }); err == nil {
49+
t.Errorf("Stats factory did not return error for unknown type.")
50+
}
51+
52+
if err := stats.AddStatter(o.hType, func(h stats.StatsInitParams) stats.Stats { return &TestStats{} }); err != nil {
53+
t.Errorf("Stats factory failed to add new type.")
54+
}
55+
56+
if err := stats.AddStatter(o.hType, func(h stats.StatsInitParams) stats.Stats { return &TestStats{} }); err == nil {
57+
t.Errorf("Stats factory allowed duplicate type.")
58+
}
59+
60+
if _, err := stats.New(o, func(string) bool { return true }, func(error) bool { return true }); err != nil {
61+
t.Errorf("Stats factory failed to return known type.")
62+
}
63+
64+
}

0 commit comments

Comments
 (0)