Pārlūkot izejas kodu

Add tests for stream context

tags/v2.0.0-rc1
9seconds 5 gadus atpakaļ
vecāks
revīzija
75b95995a9
2 mainītis faili ar 109 papildinājumiem un 0 dzēšanām
  1. 14
    0
      mtglib/init_internal_test.go
  2. 95
    0
      mtglib/stream_context_internal_test.go

+ 14
- 0
mtglib/init_internal_test.go Parādīt failu

@@ -0,0 +1,14 @@
1
+package mtglib
2
+
3
+type NoopLogger struct{}
4
+
5
+func (n NoopLogger) Named(_ string) Logger             { return n }
6
+func (n NoopLogger) BindInt(_ string, _ int) Logger    { return n }
7
+func (n NoopLogger) BindStr(_, _ string) Logger        { return n }
8
+func (n NoopLogger) Printf(_ string, _ ...interface{}) {}
9
+func (n NoopLogger) Info(_ string)                     {}
10
+func (n NoopLogger) Warning(_ string)                  {}
11
+func (n NoopLogger) Debug(_ string)                    {}
12
+func (n NoopLogger) InfoError(_ string, _ error)       {}
13
+func (n NoopLogger) WarningError(_ string, _ error)    {}
14
+func (n NoopLogger) DebugError(_ string, _ error)      {}

+ 95
- 0
mtglib/stream_context_internal_test.go Parādīt failu

@@ -0,0 +1,95 @@
1
+package mtglib
2
+
3
+import (
4
+	"context"
5
+	"net"
6
+	"testing"
7
+
8
+	"github.com/9seconds/mtg/v2/testlib"
9
+	"github.com/stretchr/testify/suite"
10
+)
11
+
12
+type StreamContextTestSuite struct {
13
+	suite.Suite
14
+
15
+	connMock  *testlib.NetConnMock
16
+	logger    NoopLogger
17
+	ctx       *streamContext
18
+	ctxCancel context.CancelFunc
19
+}
20
+
21
+func (suite *StreamContextTestSuite) SetupSuite() {
22
+	suite.logger = NoopLogger{}
23
+}
24
+
25
+func (suite *StreamContextTestSuite) SetupTest() {
26
+	ctx, cancel := context.WithCancel(context.Background())
27
+	ctx = context.WithValue(ctx, "key", "value") // nolint: golint, revive, staticcheck
28
+
29
+	suite.ctxCancel = cancel
30
+	suite.connMock = &testlib.NetConnMock{}
31
+
32
+	addr := &net.TCPAddr{
33
+		IP:   net.ParseIP("10.0.0.10"),
34
+		Port: 6676,
35
+	}
36
+	suite.connMock.On("RemoteAddr").Return(addr)
37
+
38
+	suite.ctx = newStreamContext(ctx, suite.logger, suite.connMock)
39
+}
40
+
41
+func (suite *StreamContextTestSuite) TearDownTest() {
42
+	suite.ctxCancel()
43
+	suite.connMock.AssertExpectations(suite.T())
44
+}
45
+
46
+func (suite *StreamContextTestSuite) TestContextInterface() {
47
+	_, ok := suite.ctx.Deadline()
48
+	suite.False(ok)
49
+
50
+	select {
51
+	case <-suite.ctx.Done():
52
+		suite.FailNow("unexpectedly done")
53
+	default:
54
+	}
55
+
56
+	suite.NoError(suite.ctx.Err())
57
+	suite.Equal("value", suite.ctx.Value("key"))
58
+
59
+	suite.ctxCancel()
60
+
61
+	select {
62
+	case <-suite.ctx.Done():
63
+		suite.Error(suite.ctx.Err())
64
+	default:
65
+		suite.FailNow("unexpectedly not done")
66
+	}
67
+}
68
+
69
+func (suite *StreamContextTestSuite) TestClientIP() {
70
+	suite.Equal("10.0.0.10", suite.ctx.ClientIP().String())
71
+}
72
+
73
+func (suite *StreamContextTestSuite) TestClose() {
74
+	suite.connMock.On("Close").Once().Return(nil)
75
+
76
+	tgConnMock := &testlib.NetConnMock{}
77
+	tgConnMock.On("Close").Once().Return(nil)
78
+
79
+	suite.ctx.telegramConn = tgConnMock
80
+	suite.ctx.Close()
81
+
82
+	select {
83
+	case <-suite.ctx.Done():
84
+		suite.Error(suite.ctx.Err())
85
+	default:
86
+		suite.FailNow("unexpectedly not done")
87
+	}
88
+
89
+	tgConnMock.AssertExpectations(suite.T())
90
+}
91
+
92
+func TestStreamContext(t *testing.T) {
93
+	t.Parallel()
94
+	suite.Run(t, &StreamContextTestSuite{})
95
+}

Notiek ielāde…
Atcelt
Saglabāt