connection_test.go 1.1 KB

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