github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/agents/db_kv_test.go (about)

     1  // Copyright 2024 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package agents_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/utils/agents"
     7  	"strconv"
     8  	"testing"
     9  )
    10  
    11  func TestKVDB_InsertAgentIP(t *testing.T) {
    12  	var db = agents.NewKVDB()
    13  	err := db.Init()
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  
    18  	defer func() {
    19  		_ = db.Flush()
    20  	}()
    21  
    22  	for i := 1; i <= 5; i++ {
    23  		err = db.InsertAgentIP(int64(i), "192.168.2."+strconv.Itoa(i), "example")
    24  		if err != nil {
    25  			t.Fatal(err)
    26  		}
    27  	}
    28  }
    29  
    30  func TestKVDB_ListAgentIPs(t *testing.T) {
    31  	var db = agents.NewKVDB()
    32  	err := db.Init()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	const count = 10
    38  
    39  	for {
    40  		agentIPs, listErr := db.ListAgentIPs(0, count)
    41  		if listErr != nil {
    42  			t.Fatal(listErr)
    43  		}
    44  		t.Log("===")
    45  		for _, agentIP := range agentIPs {
    46  			t.Logf("%+v", agentIP)
    47  		}
    48  
    49  		if len(agentIPs) < count {
    50  			break
    51  		}
    52  	}
    53  }