activity_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2020, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "testing"
  22. "testing/iotest"
  23. "time"
  24. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/monotime"
  25. )
  26. func TestActivityMonitoredConn(t *testing.T) {
  27. buffer := make([]byte, 1024)
  28. conn, err := NewActivityMonitoredConn(
  29. &dummyConn{},
  30. 200*time.Millisecond,
  31. true,
  32. nil)
  33. if err != nil {
  34. t.Fatalf("NewActivityMonitoredConn failed")
  35. }
  36. realStartTime := time.Now().UTC()
  37. monotonicStartTime := monotime.Now()
  38. time.Sleep(100 * time.Millisecond)
  39. _, err = conn.Read(buffer)
  40. if err != nil {
  41. t.Fatalf("read before initial timeout failed")
  42. }
  43. time.Sleep(100 * time.Millisecond)
  44. _, err = conn.Read(buffer)
  45. if err != nil {
  46. t.Fatalf("previous read failed to extend timeout")
  47. }
  48. time.Sleep(100 * time.Millisecond)
  49. _, err = conn.Write(buffer)
  50. if err != nil {
  51. t.Fatalf("previous read failed to extend timeout")
  52. }
  53. time.Sleep(100 * time.Millisecond)
  54. _, err = conn.Read(buffer)
  55. if err != nil {
  56. t.Fatalf("previous write failed to extend timeout")
  57. }
  58. lastSuccessfulReadTime := monotime.Now()
  59. time.Sleep(100 * time.Millisecond)
  60. _, err = conn.Write(buffer)
  61. if err != nil {
  62. t.Fatalf("previous read failed to extend timeout")
  63. }
  64. time.Sleep(300 * time.Millisecond)
  65. _, err = conn.Read(buffer)
  66. if err != iotest.ErrTimeout {
  67. t.Fatalf("failed to timeout")
  68. }
  69. if realStartTime.Round(time.Millisecond) != conn.GetStartTime().Round(time.Millisecond) {
  70. t.Fatalf("unexpected GetStartTime")
  71. }
  72. diff := lastSuccessfulReadTime.Sub(monotonicStartTime).Nanoseconds() - conn.GetActiveDuration().Nanoseconds()
  73. if diff < 0 {
  74. diff = -diff
  75. }
  76. if diff > (1 * time.Millisecond).Nanoseconds() {
  77. t.Fatalf("unexpected GetActiveDuration")
  78. }
  79. }
  80. func TestActivityMonitoredLRUConns(t *testing.T) {
  81. lruConns := NewLRUConns()
  82. dummy1 := &dummyConn{}
  83. conn1, err := NewActivityMonitoredConn(dummy1, 0, true, lruConns.Add(dummy1))
  84. if err != nil {
  85. t.Fatalf("NewActivityMonitoredConn failed")
  86. }
  87. dummy2 := &dummyConn{}
  88. conn2, err := NewActivityMonitoredConn(dummy2, 0, true, lruConns.Add(dummy2))
  89. if err != nil {
  90. t.Fatalf("NewActivityMonitoredConn failed")
  91. }
  92. dummy3 := &dummyConn{}
  93. conn3, err := NewActivityMonitoredConn(dummy3, 0, true, lruConns.Add(dummy3))
  94. if err != nil {
  95. t.Fatalf("NewActivityMonitoredConn failed")
  96. }
  97. buffer := make([]byte, 1024)
  98. conn1.Read(buffer)
  99. conn2.Read(buffer)
  100. conn3.Read(buffer)
  101. conn3.Write(buffer)
  102. conn2.Write(buffer)
  103. conn1.Write(buffer)
  104. if dummy1.IsClosed() || dummy2.IsClosed() || dummy3.IsClosed() {
  105. t.Fatalf("unexpected IsClosed state")
  106. }
  107. lruConns.CloseOldest()
  108. if dummy1.IsClosed() || dummy2.IsClosed() || !dummy3.IsClosed() {
  109. t.Fatalf("unexpected IsClosed state")
  110. }
  111. lruConns.CloseOldest()
  112. if dummy1.IsClosed() || !dummy2.IsClosed() || !dummy3.IsClosed() {
  113. t.Fatalf("unexpected IsClosed state")
  114. }
  115. lruConns.CloseOldest()
  116. if !dummy1.IsClosed() || !dummy2.IsClosed() || !dummy3.IsClosed() {
  117. t.Fatalf("unexpected IsClosed state")
  118. }
  119. }