lotus/lib/ulimit/ulimit_test.go

87 lines
2.3 KiB
Go
Raw Normal View History

2020-03-22 21:08:22 +00:00
// +build !windows
package ulimit
import (
"fmt"
"os"
"strings"
"syscall"
"testing"
2021-08-26 13:59:54 +00:00
"github.com/filecoin-project/lotus/build"
2020-03-22 21:08:22 +00:00
)
func TestManageFdLimit(t *testing.T) {
t.Log("Testing file descriptor count")
if _, _, err := ManageFdLimit(); err != nil {
t.Errorf("Cannot manage file descriptors")
}
2021-08-26 13:59:54 +00:00
if build.DefaultFDLimit != uint64(16<<10) {
2020-03-22 21:08:22 +00:00
t.Errorf("Maximum file descriptors default value changed")
}
}
func TestManageInvalidNFds(t *testing.T) {
t.Logf("Testing file descriptor invalidity")
var err error
if err = os.Unsetenv("IPFS_FD_MAX"); err != nil {
t.Fatal("Cannot unset the IPFS_FD_MAX env variable")
}
rlimit := syscall.Rlimit{}
if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil {
t.Fatal("Cannot get the file descriptor count")
}
value := rlimit.Max + rlimit.Cur
if err = os.Setenv("IPFS_FD_MAX", fmt.Sprintf("%d", value)); err != nil {
t.Fatal("Cannot set the IPFS_FD_MAX env variable")
}
t.Logf("setting ulimit to %d, max %d, cur %d", value, rlimit.Max, rlimit.Cur)
if changed, new, err := ManageFdLimit(); err == nil {
t.Errorf("ManageFdLimit should return an error: changed %t, new: %d", changed, new)
} else if err != nil {
flag := strings.Contains(err.Error(),
2020-08-21 01:43:13 +00:00
"failed to raise ulimit to LOTUS_FD_MAX")
2020-03-22 21:08:22 +00:00
if !flag {
t.Error("ManageFdLimit returned unexpected error", err)
}
}
// unset all previous operations
if err = os.Unsetenv("IPFS_FD_MAX"); err != nil {
t.Fatal("Cannot unset the IPFS_FD_MAX env variable")
}
}
func TestManageFdLimitWithEnvSet(t *testing.T) {
t.Logf("Testing file descriptor manager with IPFS_FD_MAX set")
var err error
if err = os.Unsetenv("IPFS_FD_MAX"); err != nil {
t.Fatal("Cannot unset the IPFS_FD_MAX env variable")
}
rlimit := syscall.Rlimit{}
if err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rlimit); err != nil {
t.Fatal("Cannot get the file descriptor count")
}
value := rlimit.Max - rlimit.Cur + 1
if err = os.Setenv("IPFS_FD_MAX", fmt.Sprintf("%d", value)); err != nil {
t.Fatal("Cannot set the IPFS_FD_MAX env variable")
}
if _, _, err = ManageFdLimit(); err != nil {
t.Errorf("Cannot manage file descriptor count")
}
// unset all previous operations
if err = os.Unsetenv("IPFS_FD_MAX"); err != nil {
t.Fatal("Cannot unset the IPFS_FD_MAX env variable")
}
2020-03-22 21:39:27 +00:00
}