github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/plugin/rpcplugin/ipc_test.go (about) 1 package rpcplugin 2 3 import ( 4 "context" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/mattermost/mattermost-server/plugin/rpcplugin/rpcplugintest" 14 ) 15 16 func TestIPC(t *testing.T) { 17 dir, err := ioutil.TempDir("", "") 18 require.NoError(t, err) 19 defer os.RemoveAll(dir) 20 21 pingpong := filepath.Join(dir, "pingpong.exe") 22 rpcplugintest.CompileGo(t, ` 23 package main 24 25 import ( 26 "log" 27 28 "github.com/mattermost/mattermost-server/plugin/rpcplugin" 29 ) 30 31 func main() { 32 ipc, err := rpcplugin.InheritedProcessIPC() 33 if err != nil { 34 log.Fatal("unable to get inherited ipc") 35 } 36 defer ipc.Close() 37 _, err = ipc.Write([]byte("ping")) 38 if err != nil { 39 log.Fatal("unable to write to ipc") 40 } 41 b := make([]byte, 10) 42 n, err := ipc.Read(b) 43 if err != nil { 44 log.Fatal("unable to read from ipc") 45 } 46 if n != 4 || string(b[:4]) != "pong" { 47 log.Fatal("unexpected response") 48 } 49 } 50 `, pingpong) 51 52 p, ipc, err := NewProcess(context.Background(), pingpong) 53 require.NoError(t, err) 54 defer ipc.Close() 55 b := make([]byte, 10) 56 n, err := ipc.Read(b) 57 require.NoError(t, err) 58 assert.Equal(t, 4, n) 59 assert.Equal(t, "ping", string(b[:4])) 60 _, err = ipc.Write([]byte("pong")) 61 require.NoError(t, err) 62 require.NoError(t, p.Wait()) 63 }