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

     1  // Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
     2  
     3  package caches_test
     4  
     5  import (
     6  	"github.com/TeaOSLab/EdgeNode/internal/caches"
     7  	"github.com/iwind/TeaGo/rands"
     8  	"sync"
     9  	"testing"
    10  )
    11  
    12  func TestOpenFilePool_Get(t *testing.T) {
    13  	var pool = caches.NewOpenFilePool("a")
    14  	t.Log(pool.Filename())
    15  	t.Log(pool.Get())
    16  	t.Log(pool.Put(caches.NewOpenFile(nil, nil, nil, 0, 1)))
    17  	t.Log(pool.Get())
    18  	t.Log(pool.Get())
    19  }
    20  
    21  func TestOpenFilePool_Close(t *testing.T) {
    22  	var pool = caches.NewOpenFilePool("a")
    23  	pool.Put(caches.NewOpenFile(nil, nil, nil, 0, 1))
    24  	pool.Put(caches.NewOpenFile(nil, nil, nil, 0, 1))
    25  	pool.Close()
    26  }
    27  
    28  func TestOpenFilePool_Concurrent(t *testing.T) {
    29  	var pool = caches.NewOpenFilePool("a")
    30  	var concurrent = 1000
    31  	var wg = &sync.WaitGroup{}
    32  	wg.Add(concurrent)
    33  	for i := 0; i < concurrent; i++ {
    34  		go func() {
    35  			defer wg.Done()
    36  
    37  			if rands.Int(0, 1) == 1 {
    38  				pool.Put(caches.NewOpenFile(nil, nil, nil, 0, 1))
    39  			}
    40  			if rands.Int(0, 1) == 0 {
    41  				pool.Get()
    42  			}
    43  		}()
    44  	}
    45  	wg.Wait()
    46  }