nebula/timeout_test.go

175 lines
4.7 KiB
Go
Raw Normal View History

2019-11-19 10:00:20 -07:00
package nebula
import (
"net/netip"
2019-11-19 10:00:20 -07:00
"testing"
"time"
"github.com/slackhq/nebula/firewall"
"github.com/stretchr/testify/assert"
2019-11-19 10:00:20 -07:00
)
func TestNewTimerWheel(t *testing.T) {
// Make sure we get an object we expect
2023-01-18 09:56:42 -07:00
tw := NewTimerWheel[firewall.Packet](time.Second, time.Second*10)
assert.Equal(t, 12, tw.wheelLen)
2019-11-19 10:00:20 -07:00
assert.Equal(t, 0, tw.current)
assert.Nil(t, tw.lastTick)
assert.Equal(t, time.Second*1, tw.tickDuration)
assert.Equal(t, time.Second*10, tw.wheelDuration)
assert.Len(t, tw.wheel, 12)
2019-11-19 10:00:20 -07:00
// Assert the math is correct
2023-01-18 09:56:42 -07:00
tw = NewTimerWheel[firewall.Packet](time.Second*3, time.Second*10)
assert.Equal(t, 5, tw.wheelLen)
2019-11-19 10:00:20 -07:00
2023-01-18 09:56:42 -07:00
tw = NewTimerWheel[firewall.Packet](time.Second*120, time.Minute*10)
assert.Equal(t, 7, tw.wheelLen)
2023-01-18 09:56:42 -07:00
// Test empty purge of non nil items
i, ok := tw.Purge()
assert.Equal(t, firewall.Packet{}, i)
assert.False(t, ok)
// Test empty purges of nil items
tw2 := NewTimerWheel[*int](time.Second, time.Second*10)
i2, ok := tw2.Purge()
assert.Nil(t, i2)
assert.False(t, ok)
2019-11-19 10:00:20 -07:00
}
func TestTimerWheel_findWheel(t *testing.T) {
2023-01-18 09:56:42 -07:00
tw := NewTimerWheel[firewall.Packet](time.Second, time.Second*10)
assert.Len(t, tw.wheel, 12)
2019-11-19 10:00:20 -07:00
// Current + tick + 1 since we don't know how far into current we are
assert.Equal(t, 2, tw.findWheel(time.Second*1))
// Scale up to min duration
assert.Equal(t, 2, tw.findWheel(time.Millisecond*1))
// Make sure we hit that last index
assert.Equal(t, 11, tw.findWheel(time.Second*10))
2019-11-19 10:00:20 -07:00
// Scale down to max duration
assert.Equal(t, 11, tw.findWheel(time.Second*11))
2019-11-19 10:00:20 -07:00
tw.current = 1
// Make sure we account for the current position properly
assert.Equal(t, 3, tw.findWheel(time.Second*1))
assert.Equal(t, 0, tw.findWheel(time.Second*10))
2019-11-19 10:00:20 -07:00
}
func TestTimerWheel_Add(t *testing.T) {
2023-01-18 09:56:42 -07:00
tw := NewTimerWheel[firewall.Packet](time.Second, time.Second*10)
2019-11-19 10:00:20 -07:00
fp1 := firewall.Packet{}
2019-11-19 10:00:20 -07:00
tw.Add(fp1, time.Second*1)
// Make sure we set head and tail properly
assert.NotNil(t, tw.wheel[2])
2023-01-18 09:56:42 -07:00
assert.Equal(t, fp1, tw.wheel[2].Head.Item)
2019-11-19 10:00:20 -07:00
assert.Nil(t, tw.wheel[2].Head.Next)
2023-01-18 09:56:42 -07:00
assert.Equal(t, fp1, tw.wheel[2].Tail.Item)
2019-11-19 10:00:20 -07:00
assert.Nil(t, tw.wheel[2].Tail.Next)
// Make sure we only modify head
fp2 := firewall.Packet{}
2019-11-19 10:00:20 -07:00
tw.Add(fp2, time.Second*1)
2023-01-18 09:56:42 -07:00
assert.Equal(t, fp2, tw.wheel[2].Head.Item)
assert.Equal(t, fp1, tw.wheel[2].Head.Next.Item)
assert.Equal(t, fp1, tw.wheel[2].Tail.Item)
2019-11-19 10:00:20 -07:00
assert.Nil(t, tw.wheel[2].Tail.Next)
// Make sure we use free'd items first
2023-01-18 09:56:42 -07:00
tw.itemCache = &TimeoutItem[firewall.Packet]{}
2019-11-19 10:00:20 -07:00
tw.itemsCached = 1
tw.Add(fp2, time.Second*1)
assert.Nil(t, tw.itemCache)
assert.Equal(t, 0, tw.itemsCached)
// Ensure that all configurations of a wheel does not result in calculating an overflow of the wheel
for min := time.Duration(1); min < 100; min++ {
for max := min; max < 100; max++ {
2023-01-18 09:56:42 -07:00
tw = NewTimerWheel[firewall.Packet](min, max)
for current := 0; current < tw.wheelLen; current++ {
tw.current = current
for timeout := time.Duration(0); timeout <= tw.wheelDuration; timeout++ {
tick := tw.findWheel(timeout)
if tick >= tw.wheelLen {
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)
}
}
}
}
}
2019-11-19 10:00:20 -07:00
}
func TestTimerWheel_Purge(t *testing.T) {
// First advance should set the lastTick and do nothing else
2023-01-18 09:56:42 -07:00
tw := NewTimerWheel[firewall.Packet](time.Second, time.Second*10)
2019-11-19 10:00:20 -07:00
assert.Nil(t, tw.lastTick)
2023-01-18 09:56:42 -07:00
tw.Advance(time.Now())
2019-11-19 10:00:20 -07:00
assert.NotNil(t, tw.lastTick)
assert.Equal(t, 0, tw.current)
fps := []firewall.Packet{
{LocalIP: netip.MustParseAddr("0.0.0.1")},
{LocalIP: netip.MustParseAddr("0.0.0.2")},
{LocalIP: netip.MustParseAddr("0.0.0.3")},
{LocalIP: netip.MustParseAddr("0.0.0.4")},
2019-11-19 10:00:20 -07:00
}
tw.Add(fps[0], time.Second*1)
tw.Add(fps[1], time.Second*1)
tw.Add(fps[2], time.Second*2)
tw.Add(fps[3], time.Second*2)
ta := time.Now().Add(time.Second * 3)
lastTick := *tw.lastTick
2023-01-18 09:56:42 -07:00
tw.Advance(ta)
2019-11-19 10:00:20 -07:00
assert.Equal(t, 3, tw.current)
assert.True(t, tw.lastTick.After(lastTick))
// Make sure we get all 4 packets back
for i := 0; i < 4; i++ {
p, has := tw.Purge()
assert.True(t, has)
assert.Equal(t, fps[i], p)
}
// Make sure there aren't any leftover
_, ok := tw.Purge()
assert.False(t, ok)
assert.Nil(t, tw.expired.Head)
assert.Nil(t, tw.expired.Tail)
// Make sure we cached the free'd items
assert.Equal(t, 4, tw.itemsCached)
ci := tw.itemCache
for i := 0; i < 4; i++ {
assert.NotNil(t, ci)
ci = ci.Next
}
assert.Nil(t, ci)
2023-01-18 09:56:42 -07:00
// Let's make sure we roll over properly
2019-11-19 10:00:20 -07:00
ta = ta.Add(time.Second * 5)
2023-01-18 09:56:42 -07:00
tw.Advance(ta)
2019-11-19 10:00:20 -07:00
assert.Equal(t, 8, tw.current)
ta = ta.Add(time.Second * 2)
2023-01-18 09:56:42 -07:00
tw.Advance(ta)
2019-11-19 10:00:20 -07:00
assert.Equal(t, 10, tw.current)
ta = ta.Add(time.Second * 1)
2023-01-18 09:56:42 -07:00
tw.Advance(ta)
assert.Equal(t, 11, tw.current)
2019-11-19 10:00:20 -07:00
ta = ta.Add(time.Second * 1)
2023-01-18 09:56:42 -07:00
tw.Advance(ta)
2019-11-19 10:00:20 -07:00
assert.Equal(t, 0, tw.current)
}