mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-01 16:01:55 +03:00
Add tests for stream context
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
package mtglib
|
||||||
|
|
||||||
|
type NoopLogger struct{}
|
||||||
|
|
||||||
|
func (n NoopLogger) Named(_ string) Logger { return n }
|
||||||
|
func (n NoopLogger) BindInt(_ string, _ int) Logger { return n }
|
||||||
|
func (n NoopLogger) BindStr(_, _ string) Logger { return n }
|
||||||
|
func (n NoopLogger) Printf(_ string, _ ...interface{}) {}
|
||||||
|
func (n NoopLogger) Info(_ string) {}
|
||||||
|
func (n NoopLogger) Warning(_ string) {}
|
||||||
|
func (n NoopLogger) Debug(_ string) {}
|
||||||
|
func (n NoopLogger) InfoError(_ string, _ error) {}
|
||||||
|
func (n NoopLogger) WarningError(_ string, _ error) {}
|
||||||
|
func (n NoopLogger) DebugError(_ string, _ error) {}
|
||||||
@@ -0,0 +1,95 @@
|
|||||||
|
package mtglib
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/9seconds/mtg/v2/testlib"
|
||||||
|
"github.com/stretchr/testify/suite"
|
||||||
|
)
|
||||||
|
|
||||||
|
type StreamContextTestSuite struct {
|
||||||
|
suite.Suite
|
||||||
|
|
||||||
|
connMock *testlib.NetConnMock
|
||||||
|
logger NoopLogger
|
||||||
|
ctx *streamContext
|
||||||
|
ctxCancel context.CancelFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *StreamContextTestSuite) SetupSuite() {
|
||||||
|
suite.logger = NoopLogger{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *StreamContextTestSuite) SetupTest() {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
ctx = context.WithValue(ctx, "key", "value") // nolint: golint, revive, staticcheck
|
||||||
|
|
||||||
|
suite.ctxCancel = cancel
|
||||||
|
suite.connMock = &testlib.NetConnMock{}
|
||||||
|
|
||||||
|
addr := &net.TCPAddr{
|
||||||
|
IP: net.ParseIP("10.0.0.10"),
|
||||||
|
Port: 6676,
|
||||||
|
}
|
||||||
|
suite.connMock.On("RemoteAddr").Return(addr)
|
||||||
|
|
||||||
|
suite.ctx = newStreamContext(ctx, suite.logger, suite.connMock)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *StreamContextTestSuite) TearDownTest() {
|
||||||
|
suite.ctxCancel()
|
||||||
|
suite.connMock.AssertExpectations(suite.T())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *StreamContextTestSuite) TestContextInterface() {
|
||||||
|
_, ok := suite.ctx.Deadline()
|
||||||
|
suite.False(ok)
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-suite.ctx.Done():
|
||||||
|
suite.FailNow("unexpectedly done")
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
|
||||||
|
suite.NoError(suite.ctx.Err())
|
||||||
|
suite.Equal("value", suite.ctx.Value("key"))
|
||||||
|
|
||||||
|
suite.ctxCancel()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-suite.ctx.Done():
|
||||||
|
suite.Error(suite.ctx.Err())
|
||||||
|
default:
|
||||||
|
suite.FailNow("unexpectedly not done")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *StreamContextTestSuite) TestClientIP() {
|
||||||
|
suite.Equal("10.0.0.10", suite.ctx.ClientIP().String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (suite *StreamContextTestSuite) TestClose() {
|
||||||
|
suite.connMock.On("Close").Once().Return(nil)
|
||||||
|
|
||||||
|
tgConnMock := &testlib.NetConnMock{}
|
||||||
|
tgConnMock.On("Close").Once().Return(nil)
|
||||||
|
|
||||||
|
suite.ctx.telegramConn = tgConnMock
|
||||||
|
suite.ctx.Close()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-suite.ctx.Done():
|
||||||
|
suite.Error(suite.ctx.Err())
|
||||||
|
default:
|
||||||
|
suite.FailNow("unexpectedly not done")
|
||||||
|
}
|
||||||
|
|
||||||
|
tgConnMock.AssertExpectations(suite.T())
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestStreamContext(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
suite.Run(t, &StreamContextTestSuite{})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user