diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/plugin_deadlock_test.go | 211 | ||||
-rw-r--r-- | app/plugin_hooks_test.go | 3 |
2 files changed, 212 insertions, 2 deletions
diff --git a/app/plugin_deadlock_test.go b/app/plugin_deadlock_test.go new file mode 100644 index 000000000..381206943 --- /dev/null +++ b/app/plugin_deadlock_test.go @@ -0,0 +1,211 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package app + +import ( + "os" + "strings" + "testing" + "text/template" + "time" + + "github.com/mattermost/mattermost-server/model" +) + +func TestPluginDeadlock(t *testing.T) { + t.Run("Single Plugin", func(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + pluginPostOnActivate := template.Must(template.New("pluginPostOnActivate").Parse(` + package main + + import ( + "github.com/mattermost/mattermost-server/plugin" + "github.com/mattermost/mattermost-server/model" + ) + + type MyPlugin struct { + plugin.MattermostPlugin + } + + func (p *MyPlugin) OnActivate() error { + _, err := p.API.CreatePost(&model.Post{ + UserId: "{{.User.Id}}", + ChannelId: "{{.Channel.Id}}", + Message: "message", + }) + if err != nil { + panic(err.Error()) + } + + return nil + } + + func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { + if _, from_plugin := post.Props["from_plugin"]; from_plugin { + return post, "" + } + + p.API.CreatePost(&model.Post{ + UserId: "{{.User.Id}}", + ChannelId: "{{.Channel.Id}}", + Message: "message", + Props: map[string]interface{}{ + "from_plugin": true, + }, + }) + + return post, "" + } + + func main() { + plugin.ClientMain(&MyPlugin{}) + } +`, + )) + + templateData := struct { + User *model.User + Channel *model.Channel + }{ + th.BasicUser, + th.BasicChannel, + } + + plugins := []string{} + pluginTemplates := []*template.Template{ + pluginPostOnActivate, + } + for _, pluginTemplate := range pluginTemplates { + b := &strings.Builder{} + pluginTemplate.Execute(b, templateData) + + plugins = append(plugins, b.String()) + } + + done := make(chan bool) + go func() { + SetAppEnvironmentWithPlugins(t, plugins, th.App, th.App.NewPluginAPI) + close(done) + }() + + select { + case <-done: + case <-time.After(30 * time.Second): + t.Fatal("plugin failed to activate: likely deadlocked") + go func() { + time.Sleep(5 * time.Second) + os.Exit(1) + }() + } + }) + + t.Run("Multiple Plugins", func(t *testing.T) { + th := Setup().InitBasic() + defer th.TearDown() + + pluginPostOnHasBeenPosted := template.Must(template.New("pluginPostOnHasBeenPosted").Parse(` + package main + + import ( + "github.com/mattermost/mattermost-server/plugin" + "github.com/mattermost/mattermost-server/model" + ) + + type MyPlugin struct { + plugin.MattermostPlugin + } + + func (p *MyPlugin) MessageWillBePosted(c *plugin.Context, post *model.Post) (*model.Post, string) { + if _, from_plugin := post.Props["from_plugin"]; from_plugin { + return post, "" + } + + p.API.CreatePost(&model.Post{ + UserId: "{{.User.Id}}", + ChannelId: "{{.Channel.Id}}", + Message: "message", + Props: map[string]interface{}{ + "from_plugin": true, + }, + }) + + return post, "" + } + + func main() { + plugin.ClientMain(&MyPlugin{}) + } +`, + )) + + pluginPostOnActivate := template.Must(template.New("pluginPostOnActivate").Parse(` + package main + + import ( + "github.com/mattermost/mattermost-server/plugin" + "github.com/mattermost/mattermost-server/model" + ) + + type MyPlugin struct { + plugin.MattermostPlugin + } + + func (p *MyPlugin) OnActivate() error { + _, err := p.API.CreatePost(&model.Post{ + UserId: "{{.User.Id}}", + ChannelId: "{{.Channel.Id}}", + Message: "message", + }) + if err != nil { + panic(err.Error()) + } + + return nil + } + + func main() { + plugin.ClientMain(&MyPlugin{}) + } +`, + )) + + templateData := struct { + User *model.User + Channel *model.Channel + }{ + th.BasicUser, + th.BasicChannel, + } + + plugins := []string{} + pluginTemplates := []*template.Template{ + pluginPostOnHasBeenPosted, + pluginPostOnActivate, + } + for _, pluginTemplate := range pluginTemplates { + b := &strings.Builder{} + pluginTemplate.Execute(b, templateData) + + plugins = append(plugins, b.String()) + } + + done := make(chan bool) + go func() { + SetAppEnvironmentWithPlugins(t, plugins, th.App, th.App.NewPluginAPI) + close(done) + }() + + select { + case <-done: + case <-time.After(30 * time.Second): + t.Fatal("plugin failed to activate: likely deadlocked") + go func() { + time.Sleep(5 * time.Second) + os.Exit(1) + }() + } + }) +} diff --git a/app/plugin_hooks_test.go b/app/plugin_hooks_test.go index 9846d628c..6e8434cab 100644 --- a/app/plugin_hooks_test.go +++ b/app/plugin_hooks_test.go @@ -43,6 +43,7 @@ func SetAppEnvironmentWithPlugins(t *testing.T, pluginCode []string, app *App, a env, err := plugin.NewEnvironment(apiFunc, pluginDir, webappPluginDir, app.Log) require.NoError(t, err) + app.Plugins = env for _, code := range pluginCode { pluginId := model.NewId() backend := filepath.Join(pluginDir, pluginId, "backend.exe") @@ -51,8 +52,6 @@ func SetAppEnvironmentWithPlugins(t *testing.T, pluginCode []string, app *App, a ioutil.WriteFile(filepath.Join(pluginDir, pluginId, "plugin.json"), []byte(`{"id": "`+pluginId+`", "backend": {"executable": "backend.exe"}}`), 0600) env.Activate(pluginId) } - - app.Plugins = env } func TestHookMessageWillBePosted(t *testing.T) { |