Explorar el Código

Add tests for relay conn

tags/v2.0.0-rc1
9seconds hace 5 años
padre
commit
54f4b397b6
Se han modificado 1 ficheros con 125 adiciones y 0 borrados
  1. 125
    0
      mtglib/internal/relay/conn_internal_test.go

+ 125
- 0
mtglib/internal/relay/conn_internal_test.go Ver fichero

@@ -0,0 +1,125 @@
1
+package relay
2
+
3
+import (
4
+	"context"
5
+	"errors"
6
+	"io"
7
+	"testing"
8
+
9
+	"github.com/9seconds/mtg/v2/testlib"
10
+	"github.com/stretchr/testify/mock"
11
+	"github.com/stretchr/testify/suite"
12
+)
13
+
14
+type ConnTestSuite struct {
15
+	suite.Suite
16
+
17
+	ctxCancel   context.CancelFunc
18
+	connMock    *testlib.NetConnMock
19
+	tickChannel chan struct{}
20
+	buf         []byte
21
+	c           conn
22
+}
23
+
24
+func (suite *ConnTestSuite) SetupTest() {
25
+	ctx, cancel := context.WithCancel(context.Background())
26
+
27
+	suite.tickChannel = make(chan struct{}, 1)
28
+	suite.connMock = &testlib.NetConnMock{}
29
+	suite.ctxCancel = cancel
30
+	suite.buf = make([]byte, 5)
31
+
32
+	suite.c = conn{
33
+		ReadWriteCloser: suite.connMock,
34
+		ctx:             ctx,
35
+		tickChannel:     suite.tickChannel,
36
+	}
37
+}
38
+
39
+func (suite *ConnTestSuite) TestReadOk() {
40
+	suite.connMock.On("Read", mock.Anything).Once().Return(len(suite.buf), nil)
41
+
42
+	n, err := suite.c.Read(suite.buf)
43
+	suite.NoError(err)
44
+	suite.Equal(len(suite.buf), n)
45
+
46
+	select {
47
+	case <-suite.tickChannel:
48
+	default:
49
+		suite.FailNow("cannot find a tick event")
50
+	}
51
+}
52
+
53
+func (suite *ConnTestSuite) TestReadErr() {
54
+	suite.connMock.On("Read", mock.Anything).Once().Return(0, io.EOF)
55
+
56
+	_, err := suite.c.Read(suite.buf)
57
+	suite.True(errors.Is(err, io.EOF))
58
+
59
+	select {
60
+	case <-suite.tickChannel:
61
+	default:
62
+		suite.FailNow("cannot find a tick event")
63
+	}
64
+}
65
+
66
+func (suite *ConnTestSuite) TestReadContextDone() {
67
+	suite.connMock.On("Read", mock.Anything).Once().Return(len(suite.buf), nil)
68
+	suite.ctxCancel()
69
+
70
+	suite.tickChannel <- struct{}{}
71
+
72
+	suite.c.Read(suite.buf)
73
+}
74
+
75
+func (suite *ConnTestSuite) TestWriteOk() {
76
+	suite.connMock.On("Write", mock.Anything).Once().Return(len(suite.buf), nil)
77
+
78
+	n, err := suite.c.Write(suite.buf)
79
+	suite.NoError(err)
80
+	suite.Equal(len(suite.buf), n)
81
+
82
+	select {
83
+	case <-suite.tickChannel:
84
+	default:
85
+		suite.FailNow("cannot find a tick event")
86
+	}
87
+}
88
+
89
+func (suite *ConnTestSuite) TestWriteErr() {
90
+	suite.connMock.On("Write", mock.Anything).Once().Return(0, io.EOF)
91
+
92
+	_, err := suite.c.Write(suite.buf)
93
+	suite.True(errors.Is(err, io.EOF))
94
+
95
+	select {
96
+	case <-suite.tickChannel:
97
+	default:
98
+		suite.FailNow("cannot find a tick event")
99
+	}
100
+}
101
+
102
+func (suite *ConnTestSuite) TestWriteContextDone() {
103
+	suite.connMock.On("Write", mock.Anything).Once().Return(len(suite.buf), nil)
104
+	suite.ctxCancel()
105
+
106
+	suite.tickChannel <- struct{}{}
107
+
108
+	suite.c.Write(suite.buf)
109
+}
110
+
111
+func (suite *ConnTestSuite) TearDownTest() {
112
+	select {
113
+	case <-suite.tickChannel:
114
+	default:
115
+	}
116
+
117
+	close(suite.tickChannel)
118
+
119
+	suite.connMock.AssertExpectations(suite.T())
120
+}
121
+
122
+func TestConn(t *testing.T) {
123
+	t.Parallel()
124
+	suite.Run(t, &ConnTestSuite{})
125
+}

Loading…
Cancelar
Guardar