connection_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package kcp_test
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. . "v2ray.com/core/transport/internet/kcp"
  7. . "v2ray.com/ext/assert"
  8. )
  9. type NoOpConn struct{}
  10. func (o *NoOpConn) Overhead() int {
  11. return 0
  12. }
  13. // Write implements io.Writer.
  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) Reset(input func([]Segment)) {}
  39. func TestConnectionReadTimeout(t *testing.T) {
  40. assert := With(t)
  41. conn := NewConnection(1, &NoOpConn{}, &Config{})
  42. conn.SetReadDeadline(time.Now().Add(time.Second))
  43. b := make([]byte, 1024)
  44. nBytes, err := conn.Read(b)
  45. assert(nBytes, Equals, 0)
  46. assert(err, IsNotNil)
  47. conn.Terminate()
  48. }