summaryrefslogtreecommitdiffstats
path: root/plugin/rpcplugin/ipc_test.go
blob: 76699a11e1cc168dbc4232e15205dda5dfe70790 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package rpcplugin

import (
	"context"
	"io/ioutil"
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/mattermost/mattermost-server/plugin/rpcplugin/rpcplugintest"
)

func TestIPC(t *testing.T) {
	dir, err := ioutil.TempDir("", "")
	require.NoError(t, err)
	defer os.RemoveAll(dir)

	pingpong := filepath.Join(dir, "pingpong.exe")
	rpcplugintest.CompileGo(t, `
		package main

		import (
			"log"

			"github.com/mattermost/mattermost-server/plugin/rpcplugin"
		)

		func main() {
			ipc, err := rpcplugin.InheritedProcessIPC()
			if err != nil {
				log.Fatal("unable to get inherited ipc")
			}
			defer ipc.Close()
			_, err = ipc.Write([]byte("ping"))
			if err != nil {
				log.Fatal("unable to write to ipc")
			}
			b := make([]byte, 10)
			n, err := ipc.Read(b)
			if err != nil {
				log.Fatal("unable to read from ipc")
			}
			if n != 4 || string(b[:4]) != "pong" {
				log.Fatal("unexpected response")
			}
		}
	`, pingpong)

	p, ipc, err := NewProcess(context.Background(), pingpong)
	require.NoError(t, err)
	defer ipc.Close()
	b := make([]byte, 10)
	n, err := ipc.Read(b)
	require.NoError(t, err)
	assert.Equal(t, 4, n)
	assert.Equal(t, "ping", string(b[:4]))
	_, err = ipc.Write([]byte("pong"))
	require.NoError(t, err)
	require.NoError(t, p.Wait())
}