Skip to content

Commit

Permalink
Added some tests and fixed some lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Hahn committed Jun 17, 2016
1 parent 5982171 commit 098da0d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
4 changes: 4 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/topfreegames/mqttbot/logger"
)

// App is the struct that defines the application
type App struct {
Debug bool
Port int
Expand All @@ -17,6 +18,7 @@ type App struct {
MqttBot *bot.MqttBot
}

// GetApp creates an app given the parameters
func GetApp(host string, port int, debug bool) *App {
logger.SetupLogger(viper.GetString("logger.level"))
logger.Logger.Debug(fmt.Sprintf("Starting app with host: %s, port: %d, configFile: %s", host, port, viper.ConfigFileUsed()))
Expand All @@ -29,6 +31,7 @@ func GetApp(host string, port int, debug bool) *App {
return app
}

// Configure configures the application
func (app *App) Configure() {
app.setConfigurationDefaults()
app.loadConfiguration()
Expand All @@ -51,6 +54,7 @@ func (app *App) configureApplication() {
a.Get("/healthcheck", HealthCheckHandler(app))
}

// Start starts the application
func (app *App) Start() {
app.Api.Listen(fmt.Sprintf("%s:%d", app.Host, app.Port))
}
9 changes: 8 additions & 1 deletion bot/mqtt_bot.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bot

import (
"fmt"
"regexp"
"strings"
"sync"
Expand All @@ -12,17 +13,20 @@ import (
"github.com/topfreegames/mqttbot/plugins"
)

// PluginMapping defines the plugin to listen to given patterns
type PluginMapping struct {
Plugin string
MessagePattern string
}

// Subscription defines the plugin mappings to a given topic
type Subscription struct {
Topic string
Qos int
PluginMappings []*PluginMapping
}

// MqttBot defines the bot, it contains plugins, subscriptions and a client
type MqttBot struct {
Plugins *plugins.Plugins
Subscriptions []*Subscription
Expand All @@ -45,6 +49,7 @@ var h mqtt.MessageHandler = func(client mqtt.Client, msg mqtt.Message) {
}
}

// GetMqttBot returns a initialized mqtt bot
func GetMqttBot() *MqttBot {
once.Do(func() {
mqttBot = &MqttBot{}
Expand All @@ -63,6 +68,8 @@ var onClientConnectHandler = func(client mqtt.Client) {
mqttBot.StartBot()
}

// StartBot starts the bot, it subscribes the bot to the topics defined in the
// configuration file
func (b *MqttBot) StartBot() {
subscriptions := viper.Get("mqttserver.subscriptionRequests").([]interface{})
client := b.Client.MqttClient
Expand All @@ -87,7 +94,7 @@ func (b *MqttBot) StartBot() {
if token := client.Subscribe(topic, uint8(qos), h); token.Wait() && token.Error() != nil {
logger.Logger.Fatal(token.Error())
}
logger.Logger.Debug(fmt.Sprintf("Subscribed to %s", topic))
b.Subscriptions = append(b.Subscriptions, subscriptionNow)
}
logger.Logger.Debug("Successfully subscribed to mqtt topics matching patterns /chat/# and /bot/history/#")
}
12 changes: 5 additions & 7 deletions bot/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bot

// RouteIncludesTopic returns if the topic matches the given route
func RouteIncludesTopic(route []string, topic []string) bool {
if len(route) == 0 {
if len(topic) == 0 {
Expand All @@ -8,17 +9,14 @@ func RouteIncludesTopic(route []string, topic []string) bool {
return false
}

if len(topic) == 0 {
if route[0] == "#" {
return true
}
return false
}

if route[0] == "#" {
return true
}

if len(topic) == 0 {
return false
}

if (route[0] == "+") || (route[0] == topic[0]) {
return RouteIncludesTopic(route[1:], topic[1:])
}
Expand Down
38 changes: 38 additions & 0 deletions bot/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package bot

import (
"testing"
)

func RouteTest(t *testing.T) {
route := []string{"a", "b"}
topic := []string{"a", "b"}
if !RouteIncludesTopic(route, topic) {
t.Fail()
}

topic = []string{"a", "b", "c"}
if RouteIncludesTopic(route, topic) {
t.Fail()
}

route = []string{"a", "b", "c"}
topic = []string{"a", "b"}
if RouteIncludesTopic(route, topic) {
t.Fail()
}
}

func WildcardTest(t *testing.T) {
route := []string{"#"}
topic := []string{"anything", "really"}
if !RouteIncludesTopic(route, topic) {
t.Fail()
}

route = []string{"+", "level"}
topic = []string{"something", "level"}
if !RouteIncludesTopic(route, topic) {
t.Fail()
}
}

0 comments on commit 098da0d

Please sign in to comment.