Files
EdgeNode/internal/caches/open_file_pool_test.go

48 lines
1017 B
Go
Raw Permalink Normal View History

2024-05-17 18:30:33 +08:00
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
2022-01-12 21:09:00 +08:00
package caches_test
import (
2022-11-25 14:52:04 +08:00
"sync"
2022-01-12 21:09:00 +08:00
"testing"
2024-07-27 15:42:50 +08:00
"github.com/TeaOSLab/EdgeNode/internal/caches"
"github.com/iwind/TeaGo/rands"
2022-01-12 21:09:00 +08:00
)
func TestOpenFilePool_Get(t *testing.T) {
var pool = caches.NewOpenFilePool("a")
t.Log(pool.Filename())
t.Log(pool.Get())
2023-10-11 21:51:05 +08:00
t.Log(pool.Put(caches.NewOpenFile(nil, nil, nil, 0, 1)))
2022-01-12 21:09:00 +08:00
t.Log(pool.Get())
t.Log(pool.Get())
}
2022-11-25 14:52:04 +08:00
func TestOpenFilePool_Close(t *testing.T) {
var pool = caches.NewOpenFilePool("a")
2023-10-11 21:51:05 +08:00
pool.Put(caches.NewOpenFile(nil, nil, nil, 0, 1))
pool.Put(caches.NewOpenFile(nil, nil, nil, 0, 1))
2022-11-25 14:52:04 +08:00
pool.Close()
}
func TestOpenFilePool_Concurrent(t *testing.T) {
var pool = caches.NewOpenFilePool("a")
var concurrent = 1000
var wg = &sync.WaitGroup{}
wg.Add(concurrent)
for i := 0; i < concurrent; i++ {
go func() {
defer wg.Done()
if rands.Int(0, 1) == 1 {
2023-10-11 21:51:05 +08:00
pool.Put(caches.NewOpenFile(nil, nil, nil, 0, 1))
2022-11-25 14:52:04 +08:00
}
if rands.Int(0, 1) == 0 {
pool.Get()
}
}()
}
wg.Wait()
}