github.com/TeaOSLab/EdgeNode@v1.3.8/internal/iplibrary/ip_list_kv_test.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package iplibrary_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
     7  	"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
     8  	"github.com/TeaOSLab/EdgeNode/internal/utils/fasttime"
     9  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
    10  	"github.com/TeaOSLab/EdgeNode/internal/zero"
    11  	"testing"
    12  	"time"
    13  )
    14  
    15  func TestKVIPList_AddItem(t *testing.T) {
    16  	kv, err := iplibrary.NewKVIPList()
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  
    21  	defer func() {
    22  		_ = kv.Flush()
    23  	}()
    24  
    25  	{
    26  		err = kv.AddItem(&pb.IPItem{
    27  			Id:        1,
    28  			IpFrom:    "192.168.1.101",
    29  			IpTo:      "",
    30  			Version:   1,
    31  			ExpiredAt: fasttime.NewFastTime().Unix() + 60,
    32  			ListId:    1,
    33  			IsDeleted: false,
    34  			ListType:  "white",
    35  		})
    36  		if err != nil {
    37  			t.Fatal(err)
    38  		}
    39  	}
    40  
    41  	{
    42  		err = kv.AddItem(&pb.IPItem{
    43  			Id:        2,
    44  			IpFrom:    "192.168.1.102",
    45  			IpTo:      "",
    46  			Version:   2,
    47  			ExpiredAt: fasttime.NewFastTime().Unix() + 60,
    48  			ListId:    1,
    49  			IsDeleted: false,
    50  			ListType:  "white",
    51  		})
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  	}
    56  
    57  	{
    58  		err = kv.AddItem(&pb.IPItem{
    59  			Id:        3,
    60  			IpFrom:    "192.168.1.103",
    61  			IpTo:      "",
    62  			Version:   3,
    63  			ExpiredAt: fasttime.NewFastTime().Unix() + 60,
    64  			ListId:    1,
    65  			IsDeleted: false,
    66  			ListType:  "white",
    67  		})
    68  		if err != nil {
    69  			t.Fatal(err)
    70  		}
    71  	}
    72  }
    73  
    74  func TestKVIPList_AddItems_Many(t *testing.T) {
    75  	kv, err := iplibrary.NewKVIPList()
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	defer func() {
    81  		_ = kv.Flush()
    82  	}()
    83  
    84  	var count = 2
    85  	var from = 1
    86  	if testutils.IsSingleTesting() {
    87  		count = 2_000_000
    88  	}
    89  
    90  	var before = time.Now()
    91  	defer func() {
    92  		t.Logf("cost: %.2f s", time.Since(before).Seconds())
    93  	}()
    94  
    95  	for i := from; i <= from+count; i++ {
    96  		err = kv.AddItem(&pb.IPItem{
    97  			Id:        int64(i),
    98  			IpFrom:    testutils.RandIP(),
    99  			IpTo:      "",
   100  			Version:   int64(i),
   101  			ExpiredAt: fasttime.NewFastTime().Unix() + 86400,
   102  			ListId:    1,
   103  			IsDeleted: false,
   104  			ListType:  "white",
   105  		})
   106  		if err != nil {
   107  			t.Fatal(err)
   108  		}
   109  	}
   110  }
   111  
   112  func TestKVIPList_DeleteExpiredItems(t *testing.T) {
   113  	kv, err := iplibrary.NewKVIPList()
   114  	if err != nil {
   115  		t.Fatal(err)
   116  	}
   117  
   118  	defer func() {
   119  		_ = kv.Flush()
   120  	}()
   121  
   122  	err = kv.DeleteExpiredItems()
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  }
   127  
   128  func TestKVIPList_UpdateMaxVersion(t *testing.T) {
   129  	kv, err := iplibrary.NewKVIPList()
   130  	if err != nil {
   131  		t.Fatal(err)
   132  	}
   133  
   134  	defer func() {
   135  		_ = kv.Flush()
   136  	}()
   137  
   138  	err = kv.UpdateMaxVersion(101)
   139  	if err != nil {
   140  		t.Fatal(err)
   141  	}
   142  
   143  	maxVersion, err := kv.ReadMaxVersion()
   144  	if err != nil {
   145  		t.Fatal(err)
   146  	}
   147  
   148  	t.Log("version:", maxVersion)
   149  }
   150  
   151  func TestKVIPList_ReadMaxVersion(t *testing.T) {
   152  	kv, err := iplibrary.NewKVIPList()
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  
   157  	maxVersion, err := kv.ReadMaxVersion()
   158  	if err != nil {
   159  		t.Fatal(err)
   160  	}
   161  
   162  	t.Log("version:", maxVersion)
   163  }
   164  
   165  func TestKVIPList_ReadItems(t *testing.T) {
   166  	kv, err := iplibrary.NewKVIPList()
   167  	if err != nil {
   168  		t.Fatal(err)
   169  	}
   170  
   171  	for {
   172  		items, goNext, readErr := kv.ReadItems(0, 2)
   173  		if readErr != nil {
   174  			t.Fatal(readErr)
   175  		}
   176  		t.Log("====")
   177  		for _, item := range items {
   178  			t.Log(item.Id)
   179  		}
   180  
   181  		if !goNext {
   182  			break
   183  		}
   184  	}
   185  }
   186  
   187  func TestKVIPList_CountItems(t *testing.T) {
   188  	kv, err := iplibrary.NewKVIPList()
   189  	if err != nil {
   190  		t.Fatal(err)
   191  	}
   192  
   193  	var count int
   194  	var m = map[int64]zero.Zero{}
   195  	for {
   196  		items, goNext, readErr := kv.ReadItems(0, 1000)
   197  		if readErr != nil {
   198  			t.Fatal(readErr)
   199  		}
   200  		for _, item := range items {
   201  			count++
   202  			m[item.Id] = zero.Zero{}
   203  		}
   204  
   205  		if !goNext {
   206  			break
   207  		}
   208  	}
   209  	t.Log("count:", count, "len:", len(m))
   210  }
   211  
   212  func TestKVIPList_Inspect(t *testing.T) {
   213  	kv, err := iplibrary.NewKVIPList()
   214  	if err != nil {
   215  		t.Fatal(err)
   216  	}
   217  	err = kv.TestInspect(t)
   218  	if err != nil {
   219  		t.Fatal(err)
   220  	}
   221  }