diff options
author | Chris <ccbrown112@gmail.com> | 2017-09-11 10:02:02 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-11 10:02:02 -0500 |
commit | 402491b7e52c4d836c1274976cdb387852cfd17b (patch) | |
tree | e8adcbdf0af5370f8af11e3fc1021a328c971a5d /plugin/pluginenv/environment_test.go | |
parent | a69bed712d53e9a7984915fffffc8a2fd1647a7a (diff) | |
download | chat-402491b7e52c4d836c1274976cdb387852cfd17b.tar.gz chat-402491b7e52c4d836c1274976cdb387852cfd17b.tar.bz2 chat-402491b7e52c4d836c1274976cdb387852cfd17b.zip |
PLT-7407: Back-end plugins (#7409)
* tie back-end plugins together
* fix comment typo
* add tests and a bit of polish
* tests and polish
* add test, don't let backend executable paths escape the plugin directory
Diffstat (limited to 'plugin/pluginenv/environment_test.go')
-rw-r--r-- | plugin/pluginenv/environment_test.go | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/plugin/pluginenv/environment_test.go b/plugin/pluginenv/environment_test.go index e9d0820bb..f24ef8d3d 100644 --- a/plugin/pluginenv/environment_test.go +++ b/plugin/pluginenv/environment_test.go @@ -1,10 +1,14 @@ package pluginenv import ( + "context" "fmt" "io/ioutil" + "net/http" + "net/http/httptest" "os" "path/filepath" + "sync" "testing" "github.com/stretchr/testify/assert" @@ -298,3 +302,70 @@ func TestEnvironment_ShutdownError(t *testing.T) { assert.Equal(t, env.ActivePluginIds(), []string{"foo"}) assert.Len(t, env.Shutdown(), 2) } + +func TestEnvironment_ConcurrentHookInvocations(t *testing.T) { + dir := initTmpDir(t, map[string]string{ + "foo/plugin.json": `{"id": "foo", "backend": {}}`, + }) + defer os.RemoveAll(dir) + + var provider MockProvider + defer provider.AssertExpectations(t) + + var api struct{ plugin.API } + var supervisor MockSupervisor + defer supervisor.AssertExpectations(t) + var hooks plugintest.Hooks + defer hooks.AssertExpectations(t) + + env, err := New( + SearchPath(dir), + APIProvider(provider.API), + SupervisorProvider(provider.Supervisor), + ) + require.NoError(t, err) + defer env.Shutdown() + + provider.On("API").Return(&api, nil) + provider.On("Supervisor").Return(&supervisor, nil) + + supervisor.On("Start").Return(nil) + supervisor.On("Stop").Return(nil) + supervisor.On("Hooks").Return(&hooks) + + ch := make(chan bool) + + hooks.On("OnActivate", &api).Return(nil) + hooks.On("OnDeactivate").Return(nil) + hooks.On("ServeHTTP", mock.AnythingOfType("*httptest.ResponseRecorder"), mock.AnythingOfType("*http.Request")).Run(func(args mock.Arguments) { + r := args.Get(1).(*http.Request) + if r.URL.Path == "/1" { + <-ch + } else { + ch <- true + } + }) + + assert.NoError(t, env.ActivatePlugin("foo")) + + rec := httptest.NewRecorder() + + wg := sync.WaitGroup{} + wg.Add(2) + + go func() { + req, err := http.NewRequest("GET", "/1", nil) + require.NoError(t, err) + env.Hooks().ServeHTTP(rec, req.WithContext(context.WithValue(context.Background(), "plugin_id", "foo"))) + wg.Done() + }() + + go func() { + req, err := http.NewRequest("GET", "/2", nil) + require.NoError(t, err) + env.Hooks().ServeHTTP(rec, req.WithContext(context.WithValue(context.Background(), "plugin_id", "foo"))) + wg.Done() + }() + + wg.Wait() +} |