summaryrefslogtreecommitdiff
path: root/protocol_test.go
blob: 55c603a5e8ff57689ed955c41d62adedcf800306 (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
package roadrunner

import (
	"github.com/pkg/errors"
	"github.com/spiral/goridge/v2"
	"github.com/stretchr/testify/assert"
	"testing"
)

type relayMock struct {
	error   bool
	payload string
}

func (r *relayMock) Send(data []byte, flags byte) (err error) {
	if r.error {
		return errors.New("send error")
	}

	return nil
}

func (r *relayMock) Receive() (data []byte, p goridge.Prefix, err error) {
	return []byte(r.payload), goridge.NewPrefix().WithFlag(goridge.PayloadControl), nil
}

func (r *relayMock) Close() error {
	return nil
}

func Test_Protocol_Errors(t *testing.T) {
	err := sendControl(&relayMock{}, make(chan int))
	assert.Error(t, err)
}

func Test_Protocol_FetchPID(t *testing.T) {
	pid, err := fetchPID(&relayMock{error: false, payload: "{\"pid\":100}"})
	assert.NoError(t, err)
	assert.Equal(t, 100, pid)

	_, err = fetchPID(&relayMock{error: true, payload: "{\"pid\":100}"})
	assert.Error(t, err)

	_, err = fetchPID(&relayMock{error: false, payload: "{\"pid:100"})
	assert.Error(t, err)
}