connection_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package kcp_test
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "v2ray.com/core/testing/assert"
  7. "v2ray.com/core/transport/internet/internal"
  8. . "v2ray.com/core/transport/internet/kcp"
  9. )
  10. type NoOpConn struct{}
  11. func (o *NoOpConn) Overhead() int {
  12. return 0
  13. }
  14. func (o *NoOpConn) Write(b []byte) (int, error) {
  15. return len(b), nil
  16. }
  17. func (o *NoOpConn) Close() error {
  18. return nil
  19. }
  20. func (o *NoOpConn) Read([]byte) (int, error) {
  21. panic("Should not be called.")
  22. }
  23. func (o *NoOpConn) LocalAddr() net.Addr {
  24. return nil
  25. }
  26. func (o *NoOpConn) RemoteAddr() net.Addr {
  27. return nil
  28. }
  29. func (o *NoOpConn) SetDeadline(time.Time) error {
  30. return nil
  31. }
  32. func (o *NoOpConn) SetReadDeadline(time.Time) error {
  33. return nil
  34. }
  35. func (o *NoOpConn) SetWriteDeadline(time.Time) error {
  36. return nil
  37. }
  38. func (o *NoOpConn) Id() internal.ConnectionID {
  39. return internal.ConnectionID{}
  40. }
  41. func (o *NoOpConn) Reset(input func([]Segment)) {}
  42. type NoOpRecycler struct{}
  43. func (o *NoOpRecycler) Put(internal.ConnectionID, net.Conn) {}
  44. func TestConnectionReadTimeout(t *testing.T) {
  45. assert := assert.On(t)
  46. conn := NewConnection(1, &NoOpConn{}, &NoOpRecycler{}, &Config{})
  47. conn.SetReadDeadline(time.Now().Add(time.Second))
  48. b := make([]byte, 1024)
  49. nBytes, err := conn.Read(b)
  50. assert.Int(nBytes).Equals(0)
  51. assert.Error(err).IsNotNil()
  52. conn.Terminate()
  53. }