github.com/TeaOSLab/EdgeNode@v1.3.8/internal/firewalls/nftables/expration_test.go (about)

     1  // Copyright 2023 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package nftables_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/firewalls/nftables"
     7  	"github.com/iwind/TeaGo/rands"
     8  	"github.com/iwind/TeaGo/types"
     9  	"net"
    10  	"testing"
    11  	"time"
    12  )
    13  
    14  func TestExpiration_Add(t *testing.T) {
    15  	var expiration = nftables.NewExpiration()
    16  	{
    17  		expiration.Add([]byte{'a', 'b', 'c'}, time.Now())
    18  		t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
    19  	}
    20  	{
    21  		expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(1*time.Second))
    22  		t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
    23  	}
    24  	{
    25  		expiration.Add([]byte{'a', 'b', 'c'}, time.Time{})
    26  		t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
    27  	}
    28  	{
    29  		expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(-1*time.Second))
    30  		t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
    31  	}
    32  	{
    33  		expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(-10*time.Second))
    34  		t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
    35  	}
    36  	{
    37  		expiration.Add([]byte{'a', 'b', 'c'}, time.Now().Add(1*time.Second))
    38  		expiration.Remove([]byte{'a', 'b', 'c'})
    39  		t.Log(expiration.Contains([]byte{'a', 'b', 'c'}))
    40  	}
    41  	{
    42  		expiration.Add(net.ParseIP("10.254.0.75").To4(), time.Now())
    43  		t.Log(expiration.Contains(net.ParseIP("10.254.0.75").To4()))
    44  	}
    45  }
    46  
    47  func BenchmarkNewExpiration(b *testing.B) {
    48  	var expiration = nftables.NewExpiration()
    49  	for i := 0; i < 10_000; i++ {
    50  		expiration.Add([]byte(types.String(types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255)))), time.Now().Add(3600*time.Second))
    51  	}
    52  	b.ResetTimer()
    53  
    54  	b.RunParallel(func(pb *testing.PB) {
    55  		for pb.Next() {
    56  			expiration.Add([]byte(types.String(types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255))+"."+types.String(rands.Int(0, 255)))), time.Now().Add(3600*time.Second))
    57  		}
    58  	})
    59  }