timeout_system_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "github.com/slackhq/nebula/iputil"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestNewSystemTimerWheel(t *testing.T) {
  10. // Make sure we get an object we expect
  11. tw := NewSystemTimerWheel(time.Second, time.Second*10)
  12. assert.Equal(t, 12, tw.wheelLen)
  13. assert.Equal(t, 0, tw.current)
  14. assert.Nil(t, tw.lastTick)
  15. assert.Equal(t, time.Second*1, tw.tickDuration)
  16. assert.Equal(t, time.Second*10, tw.wheelDuration)
  17. assert.Len(t, tw.wheel, 12)
  18. // Assert the math is correct
  19. tw = NewSystemTimerWheel(time.Second*3, time.Second*10)
  20. assert.Equal(t, 5, tw.wheelLen)
  21. tw = NewSystemTimerWheel(time.Second*120, time.Minute*10)
  22. assert.Equal(t, 7, tw.wheelLen)
  23. }
  24. func TestSystemTimerWheel_findWheel(t *testing.T) {
  25. tw := NewSystemTimerWheel(time.Second, time.Second*10)
  26. assert.Len(t, tw.wheel, 12)
  27. // Current + tick + 1 since we don't know how far into current we are
  28. assert.Equal(t, 2, tw.findWheel(time.Second*1))
  29. // Scale up to min duration
  30. assert.Equal(t, 2, tw.findWheel(time.Millisecond*1))
  31. // Make sure we hit that last index
  32. assert.Equal(t, 11, tw.findWheel(time.Second*10))
  33. // Scale down to max duration
  34. assert.Equal(t, 11, tw.findWheel(time.Second*11))
  35. tw.current = 1
  36. // Make sure we account for the current position properly
  37. assert.Equal(t, 3, tw.findWheel(time.Second*1))
  38. assert.Equal(t, 0, tw.findWheel(time.Second*10))
  39. // Ensure that all configurations of a wheel does not result in calculating an overflow of the wheel
  40. for min := time.Duration(1); min < 100; min++ {
  41. for max := min; max < 100; max++ {
  42. tw = NewSystemTimerWheel(min, max)
  43. for current := 0; current < tw.wheelLen; current++ {
  44. tw.current = current
  45. for timeout := time.Duration(0); timeout <= tw.wheelDuration; timeout++ {
  46. tick := tw.findWheel(timeout)
  47. if tick >= tw.wheelLen {
  48. t.Errorf("Min: %v; Max: %v; Wheel len: %v; Current Tick: %v; Insert timeout: %v; Calc tick: %v", min, max, tw.wheelLen, current, timeout, tick)
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }
  55. func TestSystemTimerWheel_Add(t *testing.T) {
  56. tw := NewSystemTimerWheel(time.Second, time.Second*10)
  57. fp1 := iputil.Ip2VpnIp(net.ParseIP("1.2.3.4"))
  58. tw.Add(fp1, time.Second*1)
  59. // Make sure we set head and tail properly
  60. assert.NotNil(t, tw.wheel[2])
  61. assert.Equal(t, fp1, tw.wheel[2].Head.Item)
  62. assert.Nil(t, tw.wheel[2].Head.Next)
  63. assert.Equal(t, fp1, tw.wheel[2].Tail.Item)
  64. assert.Nil(t, tw.wheel[2].Tail.Next)
  65. // Make sure we only modify head
  66. fp2 := iputil.Ip2VpnIp(net.ParseIP("1.2.3.4"))
  67. tw.Add(fp2, time.Second*1)
  68. assert.Equal(t, fp2, tw.wheel[2].Head.Item)
  69. assert.Equal(t, fp1, tw.wheel[2].Head.Next.Item)
  70. assert.Equal(t, fp1, tw.wheel[2].Tail.Item)
  71. assert.Nil(t, tw.wheel[2].Tail.Next)
  72. // Make sure we use free'd items first
  73. tw.itemCache = &SystemTimeoutItem{}
  74. tw.itemsCached = 1
  75. tw.Add(fp2, time.Second*1)
  76. assert.Nil(t, tw.itemCache)
  77. assert.Equal(t, 0, tw.itemsCached)
  78. }
  79. func TestSystemTimerWheel_Purge(t *testing.T) {
  80. // First advance should set the lastTick and do nothing else
  81. tw := NewSystemTimerWheel(time.Second, time.Second*10)
  82. assert.Nil(t, tw.lastTick)
  83. tw.advance(time.Now())
  84. assert.NotNil(t, tw.lastTick)
  85. assert.Equal(t, 0, tw.current)
  86. fps := []iputil.VpnIp{9, 10, 11, 12}
  87. //fp1 := ip2int(net.ParseIP("1.2.3.4"))
  88. tw.Add(fps[0], time.Second*1)
  89. tw.Add(fps[1], time.Second*1)
  90. tw.Add(fps[2], time.Second*2)
  91. tw.Add(fps[3], time.Second*2)
  92. ta := time.Now().Add(time.Second * 3)
  93. lastTick := *tw.lastTick
  94. tw.advance(ta)
  95. assert.Equal(t, 3, tw.current)
  96. assert.True(t, tw.lastTick.After(lastTick))
  97. // Make sure we get all 4 packets back
  98. for i := 0; i < 4; i++ {
  99. assert.Contains(t, fps, tw.Purge())
  100. }
  101. // Make sure there aren't any leftover
  102. assert.Nil(t, tw.Purge())
  103. assert.Nil(t, tw.expired.Head)
  104. assert.Nil(t, tw.expired.Tail)
  105. // Make sure we cached the free'd items
  106. assert.Equal(t, 4, tw.itemsCached)
  107. ci := tw.itemCache
  108. for i := 0; i < 4; i++ {
  109. assert.NotNil(t, ci)
  110. ci = ci.Next
  111. }
  112. assert.Nil(t, ci)
  113. // Lets make sure we roll over properly
  114. ta = ta.Add(time.Second * 5)
  115. tw.advance(ta)
  116. assert.Equal(t, 8, tw.current)
  117. ta = ta.Add(time.Second * 2)
  118. tw.advance(ta)
  119. assert.Equal(t, 10, tw.current)
  120. ta = ta.Add(time.Second * 1)
  121. tw.advance(ta)
  122. assert.Equal(t, 11, tw.current)
  123. ta = ta.Add(time.Second * 1)
  124. tw.advance(ta)
  125. assert.Equal(t, 0, tw.current)
  126. }