diff options
author | Chris <ccbrown112@gmail.com> | 2017-08-16 17:23:38 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-08-16 17:23:38 -0500 |
commit | f80d50adbddf55a043dfcab5b47d7c1e22749b7d (patch) | |
tree | 5deb606debb6322716c9cdcc6c58be4f68b74223 /plugin/rpcplugin/supervisor_test.go | |
parent | 4f85ed985d478ddf6692fa4f7d8d98d2a412d18c (diff) | |
download | chat-f80d50adbddf55a043dfcab5b47d7c1e22749b7d.tar.gz chat-f80d50adbddf55a043dfcab5b47d7c1e22749b7d.tar.bz2 chat-f80d50adbddf55a043dfcab5b47d7c1e22749b7d.zip |
PLT-7407: Back-end plugin mechanism (#7177)
* begin backend plugin wip
* flesh out rpcplugin. everything done except for minor supervisor stubs
* done with basic plugin infrastructure
* simplify tests
* remove unused test lines
Diffstat (limited to 'plugin/rpcplugin/supervisor_test.go')
-rw-r--r-- | plugin/rpcplugin/supervisor_test.go | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/plugin/rpcplugin/supervisor_test.go b/plugin/rpcplugin/supervisor_test.go new file mode 100644 index 000000000..1d046bf82 --- /dev/null +++ b/plugin/rpcplugin/supervisor_test.go @@ -0,0 +1,130 @@ +package rpcplugin + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/mattermost/platform/plugin" +) + +func TestSupervisor(t *testing.T) { + dir, err := ioutil.TempDir("", "") + require.NoError(t, err) + defer os.RemoveAll(dir) + + backend := filepath.Join(dir, "backend") + compileGo(t, ` + package main + + import ( + "github.com/mattermost/platform/plugin" + "github.com/mattermost/platform/plugin/rpcplugin" + ) + + type MyPlugin struct {} + + func (p *MyPlugin) OnActivate(api plugin.API) error { + return nil + } + + func (p *MyPlugin) OnDeactivate() error { + return nil + } + + func main() { + rpcplugin.Main(&MyPlugin{}) + } + `, backend) + + ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "backend": {"executable": "backend"}}`), 0600) + + bundle := plugin.BundleInfoForPath(dir) + supervisor, err := SupervisorProvider(bundle) + require.NoError(t, err) + require.NoError(t, supervisor.Start()) + require.NoError(t, supervisor.Hooks().OnActivate(nil)) + require.NoError(t, supervisor.Stop()) +} + +// If plugin development goes really wrong, let's make sure plugin activation won't block forever. +func TestSupervisor_StartTimeout(t *testing.T) { + dir, err := ioutil.TempDir("", "") + require.NoError(t, err) + defer os.RemoveAll(dir) + + backend := filepath.Join(dir, "backend") + compileGo(t, ` + package main + + func main() { + for { + } + } + `, backend) + + ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "backend": {"executable": "backend"}}`), 0600) + + bundle := plugin.BundleInfoForPath(dir) + supervisor, err := SupervisorProvider(bundle) + require.NoError(t, err) + require.Error(t, supervisor.Start()) +} + +// Crashed plugins should be relaunched. +func TestSupervisor_PluginCrash(t *testing.T) { + dir, err := ioutil.TempDir("", "") + require.NoError(t, err) + defer os.RemoveAll(dir) + + backend := filepath.Join(dir, "backend") + compileGo(t, ` + package main + + import ( + "os" + + "github.com/mattermost/platform/plugin" + "github.com/mattermost/platform/plugin/rpcplugin" + ) + + type MyPlugin struct {} + + func (p *MyPlugin) OnActivate(api plugin.API) error { + os.Exit(1) + return nil + } + + func (p *MyPlugin) OnDeactivate() error { + return nil + } + + func main() { + rpcplugin.Main(&MyPlugin{}) + } + `, backend) + + ioutil.WriteFile(filepath.Join(dir, "plugin.json"), []byte(`{"id": "foo", "backend": {"executable": "backend"}}`), 0600) + + bundle := plugin.BundleInfoForPath(dir) + supervisor, err := SupervisorProvider(bundle) + require.NoError(t, err) + require.NoError(t, supervisor.Start()) + require.Error(t, supervisor.Hooks().OnActivate(nil)) + + recovered := false + for i := 0; i < 30; i++ { + if supervisor.Hooks().OnDeactivate() == nil { + recovered = true + break + } + time.Sleep(time.Millisecond * 100) + } + assert.True(t, recovered) + require.NoError(t, supervisor.Stop()) +} |