Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

relay_test.go 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package relay_test
  2. import (
  3. "context"
  4. "io"
  5. "testing"
  6. "time"
  7. "github.com/9seconds/mtg/v2/internal/testlib"
  8. "github.com/9seconds/mtg/v2/mtglib/internal/relay"
  9. "github.com/stretchr/testify/mock"
  10. "github.com/stretchr/testify/suite"
  11. )
  12. type RelayTestSuite struct {
  13. suite.Suite
  14. ctx context.Context
  15. ctxCancel context.CancelFunc
  16. r *relay.Relay
  17. }
  18. func (suite *RelayTestSuite) SetupTest() {
  19. suite.ctx, suite.ctxCancel = context.WithCancel(context.Background())
  20. suite.r = relay.AcquireRelay(suite.ctx, loggerMock{}, 4096, time.Second)
  21. }
  22. func (suite *RelayTestSuite) TearDownTest() {
  23. suite.ctxCancel()
  24. relay.ReleaseRelay(suite.r)
  25. suite.r = nil
  26. }
  27. func (suite *RelayTestSuite) TestCancelled() {
  28. suite.ctxCancel()
  29. eastConn := &rwcMock{}
  30. eastConn.Write([]byte{1, 2, 3, 4, 5}) // nolint: errcheck
  31. westConn := &rwcMock{}
  32. westConn.Write([]byte{100, 101, 102}) // nolint: errcheck
  33. suite.Nil(suite.r.Process(eastConn, westConn))
  34. }
  35. func (suite *RelayTestSuite) TestCopyFine() {
  36. eastConn := &rwcMock{}
  37. eastConn.Write([]byte{1, 2, 3, 4, 5}) // nolint: errcheck
  38. westConn := &rwcMock{}
  39. westConn.Write([]byte{100, 101, 102}) // nolint: errcheck
  40. // yes, this test is not good enough. but apparently, if it hangs,
  41. // we can debug most of possible issues.
  42. _ = suite.r.Process(eastConn, westConn)
  43. }
  44. func (suite *RelayTestSuite) TestTimeout() {
  45. eastConn := &rwcMock{}
  46. eastConn.Write([]byte{1, 2, 3, 4, 5}) // nolint: errcheck
  47. westConn := &testlib.NetConnMock{}
  48. westConn.On("Close").Return(nil)
  49. westConn.On("Read", mock.Anything).Return(0, io.EOF).Run(func(_ mock.Arguments) {
  50. time.Sleep(2 * time.Second)
  51. })
  52. westConn.On("Write", mock.Anything).Return(0, io.EOF).Run(func(_ mock.Arguments) {
  53. time.Sleep(2 * time.Second)
  54. })
  55. suite.Error(suite.r.Process(eastConn, westConn))
  56. }
  57. func TestRelay(t *testing.T) {
  58. t.Parallel()
  59. suite.Run(t, &RelayTestSuite{})
  60. }