Skip to content

Commit

Permalink
internal: speedup+simplified integration tests (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
brainexe authored Jan 29, 2021
1 parent 489615d commit 9c7ee3f
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 20 deletions.
5 changes: 5 additions & 0 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ func (b *Bot) handleMessage(message msg.Message, fromUserContext bool) {
b.sendFallbackMessage(message)
}

// mark the message as handled...if someone needs this information
if message.Done != nil {
message.Done.Done()
}

logger.
WithField("duration", util.FormatDuration(time.Since(start))).
WithField("durationWithLatency", util.FormatDuration(time.Since(message.GetTime()))).
Expand Down
13 changes: 12 additions & 1 deletion bot/msg/message.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package msg

// Message is a wrapper which holds all important fields from slack.MessageEvent
import "sync"

type Message struct {
MessageRef
Done *sync.WaitGroup
Text string `json:"text,omitempty"`
}

func (msg Message) GetText() string {
func (msg *Message) GetText() string {
return msg.Text
}

// AddDoneHandler will register a sync.WaitGroup
func (msg *Message) AddDoneHandler() *sync.WaitGroup {
msg.Done = &sync.WaitGroup{}
msg.Done.Add(1)

return msg.Done
}
2 changes: 1 addition & 1 deletion bot/msg/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (msg MessageRef) GetUniqueKey() string {
// GetTime extracts the time of the Message
func (msg MessageRef) GetTime() time.Time {
if msg.GetTimestamp() == "" {
return time.Time{}
return time.Now()
}
timestamp, _ := strconv.ParseInt(msg.GetTimestamp()[0:10], 10, 64)

Expand Down
10 changes: 10 additions & 0 deletions bot/tester/tester.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ func StartFakeSlack(cfg *config.Config, output io.Writer) *slacktest.Server {
payload, _ := ioutil.ReadAll(r.Body)
query, _ := url.ParseQuery(string(payload))
text := query.Get("text")

// extract text from TextBlock
if text == "" {
blockJSON := query.Get("blocks")
var blocks []slack.SectionBlock
json.Unmarshal([]byte(blockJSON), &blocks)

text = blocks[0].Text.Text
}

fmt.Fprintf(output, formatSlackMessage(text)+"\n")

response := slack.Message{}
Expand Down
4 changes: 2 additions & 2 deletions bot/util/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ import (
log "github.com/sirupsen/logrus"
)

// NewServerContext wrapper for ctx to simply add childs which have a blocking shutdown process
// NewServerContext wrapper for ctx to simply add children which have a blocking shutdown process
// -> make sure all stuff is closed properly before exit
// todo directly add term/kill handling
func NewServerContext() *ServerContext {
ctx, cancel := context.WithCancel(context.Background())

Expand All @@ -20,6 +19,7 @@ func NewServerContext() *ServerContext {
}
}

// ServerContext is an extended context.Context to be able to wait for all children to have a proper shutdown
type ServerContext struct {
context.Context

Expand Down
11 changes: 11 additions & 0 deletions client/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package client
import (
"fmt"
"strings"
"sync"

"github.com/innogames/slack-bot/bot/config"
"github.com/innogames/slack-bot/bot/msg"
Expand All @@ -17,8 +18,18 @@ import (
)

// InternalMessages is internal queue of internal messages
// @deprecated -> use QueueMessage instead
var InternalMessages = make(chan msg.Message, 50)

// QueueMessage will register the given message in the queue...and returns a sync.WaitGroup which can be used to see when the message is handled
func QueueMessage(message msg.Message) *sync.WaitGroup {
done := message.AddDoneHandler()

InternalMessages <- message

return done
}

// AuthResponse is holding some basic Slack metadata for the current connection, like Bot-Id, Workspace etc
var AuthResponse slack.AuthTestResponse

Expand Down
18 changes: 11 additions & 7 deletions cmd/cli/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package main

import (
"io"
"testing"
"time"

"github.com/gookit/color"
"github.com/innogames/slack-bot/bot/config"
"github.com/innogames/slack-bot/bot/util"
"github.com/stretchr/testify/assert"
"io"
"testing"
"time"
)

func TestAll(t *testing.T) {
Expand All @@ -24,24 +23,29 @@ func TestAll(t *testing.T) {
expectedOutput.Write([]byte("Type in your command:\n"))

go startCli(ctx, input, output, cfg)
time.Sleep(time.Millisecond * 200)

testCommand("reply it works", "it works", input, expectedOutput)
testCommand("wtf", "Oops! Command `wtf` not found...try `help`.", input, expectedOutput)
testCommand("add reaction :smile:", "😄", input, expectedOutput)

// delay
testCommand("delay 10m reply I'm delayed", "I queued the command `reply I'm delayed` for 10m0s. Use `stop timer 0` to stop the timer", input, expectedOutput)
testCommand("stop timer 0", "Stopped timer!", input, expectedOutput)
testCommand("stop timer 0", "invalid timer", input, expectedOutput)

// custom commands
testCommand("add command 'wtf' 'reply bar'", "Added command: `reply bar`. Just use `wtf` in future.", input, expectedOutput)
testCommand("wtf", "executing command: `reply bar`\nbar", input, expectedOutput)

time.Sleep(time.Second * 2)

ctx.StopTheWorld()

assert.Equal(t, output.String(), expectedOutput.String())
assert.Equal(t, expectedOutput.String(), output.String())
}

func testCommand(command string, expectedOutput string, input io.Writer, output io.Writer) {
input.Write([]byte(command + "\n"))
time.Sleep(time.Millisecond * 200)

output.Write([]byte(">>>> " + command + "\n"))
output.Write([]byte(expectedOutput + "\n"))
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func startCli(ctx *util.ServerContext, input io.Reader, output io.Writer, cfg co
message.Channel = tester.TestChannel
message.User = "cli"

client.InternalMessages <- message
client.QueueMessage(message).Wait()
}
}
}
2 changes: 1 addition & 1 deletion command/pullrequest/pull_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func TestPullRequest(t *testing.T) {
time.Sleep(time.Millisecond * 10) // todo channel
})

t.Run("PR in reiew", func(t *testing.T) {
t.Run("PR in review", func(t *testing.T) {
commands, fetcher := initTest(slackClient)

message := msg.Message{}
Expand Down
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/bndr/gojenkins v1.0.1
github.com/fatih/color v1.9.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/gfleury/go-bitbucket-v1 v0.0.0-20200810125852-15f2a16ca820
github.com/gfleury/go-bitbucket-v1 v0.0.0-20210119103841-412cc3323e5e
github.com/gfleury/go-bitbucket-v1/test/bb-mock-server v0.0.0-20200810125852-15f2a16ca820
github.com/go-redis/redis/v7 v7.4.0
github.com/go-test/deep v1.0.7 // indirect
Expand Down Expand Up @@ -36,13 +36,14 @@ require (
github.com/trivago/tgo v1.0.7 // indirect
github.com/xanzy/go-gitlab v0.42.0
github.com/yuin/gopher-lua v0.0.0-20200603152657-dc2b0ca8b37e // indirect
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b // indirect
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58
golang.org/x/sys v0.0.0-20201118182958-a01c418693c7 // indirect
golang.org/x/text v0.3.4 // indirect
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/oauth2 v0.0.0-20210126194326-f9ce19ea3013
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)

replace github.com/spf13/viper => github.com/brainexe/viper v0.0.0-20201112092033-7bf4d99562ca

0 comments on commit 9c7ee3f

Please sign in to comment.