summaryrefslogtreecommitdiff
path: root/plugins/rpc/tests/plugin2.go
blob: 347e03303b4ed5e51a2cff6619359fcf510a0c56 (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
package tests

import (
	"net"
	"net/rpc"
	"time"

	"github.com/spiral/errors"
	"github.com/spiral/goridge/v3"
)

// plugin2 makes a call to the plugin1 via RPC
// this is just a simulation of external call FOR TEST
// you don't need to do such things :)
type Plugin2 struct {
}

func (p2 *Plugin2) Init() error {
	return nil
}

func (p2 *Plugin2) Serve() chan error {
	errCh := make(chan error, 1)

	go func() {
		time.Sleep(time.Second * 3)

		conn, err := net.Dial("tcp", "127.0.0.1:6001")
		if err != nil {
			errCh <- errors.E(errors.Serve, err)
			return
		}
		client := rpc.NewClientWithCodec(goridge.NewClientCodec(conn))
		var ret string
		err = client.Call("rpc_test.plugin1.Hello", "Valery", &ret)
		if err != nil {
			errCh <- err
			return
		}
		if ret != "Hello, username: Valery" {
			errCh <- errors.E("wrong response")
			return
		}
		// to stop exec
		errCh <- errors.E(errors.Disabled)
		return
	}()

	return errCh
}

func (p2 *Plugin2) Stop() error {
	return nil
}