github.com/TeaOSLab/EdgeNode@v1.3.8/internal/caches/open_file_cache_test.go (about)

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
     2  
     3  package caches_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/caches"
     7  	"github.com/TeaOSLab/EdgeNode/internal/utils/testutils"
     8  	"github.com/iwind/TeaGo/types"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestNewOpenFileCache_Close(t *testing.T) {
    14  	cache, err := caches.NewOpenFileCache(1024)
    15  	if err != nil {
    16  		t.Fatal(err)
    17  	}
    18  	cache.Debug()
    19  	cache.Put("a.txt", caches.NewOpenFile(nil, nil, nil, 0, 1<<20))
    20  	cache.Put("b.txt", caches.NewOpenFile(nil, nil, nil, 0, 1<<20))
    21  	cache.Put("b.txt", caches.NewOpenFile(nil, nil, nil, 0, 1<<20))
    22  	cache.Put("b.txt", caches.NewOpenFile(nil, nil, nil, 0, 1<<20))
    23  	cache.Put("c.txt", caches.NewOpenFile(nil, nil, nil, 0, 1<<20))
    24  
    25  	cache.Get("b.txt")
    26  	cache.Get("d.txt") // not exist
    27  	cache.Close("a.txt")
    28  
    29  	if testutils.IsSingleTesting() {
    30  		time.Sleep(100 * time.Second)
    31  	}
    32  }
    33  
    34  func TestNewOpenFileCache_OverSize(t *testing.T) {
    35  	cache, err := caches.NewOpenFileCache(1024)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	cache.SetCapacity(1 << 30)
    41  
    42  	cache.Debug()
    43  
    44  	for i := 0; i < 100; i++ {
    45  		cache.Put("a"+types.String(i)+".txt", caches.NewOpenFile(nil, nil, nil, 0, 128<<20))
    46  	}
    47  
    48  	if testutils.IsSingleTesting() {
    49  		time.Sleep(100 * time.Second)
    50  	}
    51  }
    52  
    53  func TestNewOpenFileCache_CloseAll(t *testing.T) {
    54  	cache, err := caches.NewOpenFileCache(1024)
    55  	if err != nil {
    56  		t.Fatal(err)
    57  	}
    58  	cache.Debug()
    59  	cache.Put("a.txt", caches.NewOpenFile(nil, nil, nil, 0, 1))
    60  	cache.Put("b.txt", caches.NewOpenFile(nil, nil, nil, 0, 1))
    61  	cache.Put("c.txt", caches.NewOpenFile(nil, nil, nil, 0, 1))
    62  	cache.Get("b.txt")
    63  	cache.Get("d.txt")
    64  	cache.CloseAll()
    65  
    66  	if testutils.IsSingleTesting() {
    67  		time.Sleep(6 * time.Second)
    68  	}
    69  }