activity_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/goarista/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. nil)
  34. if err != nil {
  35. t.Fatalf("NewActivityMonitoredConn failed")
  36. }
  37. realStartTime := time.Now().UTC()
  38. monotonicStartTime := monotime.Now()
  39. time.Sleep(100 * time.Millisecond)
  40. _, err = conn.Read(buffer)
  41. if err != nil {
  42. t.Fatalf("read before initial timeout failed")
  43. }
  44. time.Sleep(100 * time.Millisecond)
  45. _, err = conn.Read(buffer)
  46. if err != nil {
  47. t.Fatalf("previous read failed to extend timeout")
  48. }
  49. time.Sleep(100 * time.Millisecond)
  50. _, err = conn.Write(buffer)
  51. if err != nil {
  52. t.Fatalf("previous read failed to extend timeout")
  53. }
  54. time.Sleep(100 * time.Millisecond)
  55. _, err = conn.Read(buffer)
  56. if err != nil {
  57. t.Fatalf("previous write failed to extend timeout")
  58. }
  59. lastSuccessfulReadTime := monotime.Now()
  60. time.Sleep(100 * time.Millisecond)
  61. _, err = conn.Write(buffer)
  62. if err != nil {
  63. t.Fatalf("previous read failed to extend timeout")
  64. }
  65. time.Sleep(300 * time.Millisecond)
  66. _, err = conn.Read(buffer)
  67. if err != iotest.ErrTimeout {
  68. t.Fatalf("failed to timeout")
  69. }
  70. if realStartTime.Round(time.Millisecond) != conn.GetStartTime().Round(time.Millisecond) {
  71. t.Fatalf("unexpected GetStartTime")
  72. }
  73. diff := lastSuccessfulReadTime.Sub(monotonicStartTime).Nanoseconds() - conn.GetActiveDuration().Nanoseconds()
  74. if diff < 0 {
  75. diff = -diff
  76. }
  77. if diff > (1 * time.Millisecond).Nanoseconds() {
  78. t.Fatalf("unexpected GetActiveDuration")
  79. }
  80. }
  81. func TestActivityMonitoredLRUConns(t *testing.T) {
  82. lruConns := NewLRUConns()
  83. dummy1 := &dummyConn{}
  84. conn1, err := NewActivityMonitoredConn(dummy1, 0, true, nil, lruConns.Add(dummy1))
  85. if err != nil {
  86. t.Fatalf("NewActivityMonitoredConn failed")
  87. }
  88. dummy2 := &dummyConn{}
  89. conn2, err := NewActivityMonitoredConn(dummy2, 0, true, nil, lruConns.Add(dummy2))
  90. if err != nil {
  91. t.Fatalf("NewActivityMonitoredConn failed")
  92. }
  93. dummy3 := &dummyConn{}
  94. conn3, err := NewActivityMonitoredConn(dummy3, 0, true, nil, lruConns.Add(dummy3))
  95. if err != nil {
  96. t.Fatalf("NewActivityMonitoredConn failed")
  97. }
  98. buffer := make([]byte, 1024)
  99. conn1.Read(buffer)
  100. conn2.Read(buffer)
  101. conn3.Read(buffer)
  102. conn3.Write(buffer)
  103. conn2.Write(buffer)
  104. conn1.Write(buffer)
  105. if dummy1.IsClosed() || dummy2.IsClosed() || dummy3.IsClosed() {
  106. t.Fatalf("unexpected IsClosed state")
  107. }
  108. lruConns.CloseOldest()
  109. if dummy1.IsClosed() || dummy2.IsClosed() || !dummy3.IsClosed() {
  110. t.Fatalf("unexpected IsClosed state")
  111. }
  112. lruConns.CloseOldest()
  113. if dummy1.IsClosed() || !dummy2.IsClosed() || !dummy3.IsClosed() {
  114. t.Fatalf("unexpected IsClosed state")
  115. }
  116. lruConns.CloseOldest()
  117. if !dummy1.IsClosed() || !dummy2.IsClosed() || !dummy3.IsClosed() {
  118. t.Fatalf("unexpected IsClosed state")
  119. }
  120. }