This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulimit_test.go
85 lines (69 loc) · 2.25 KB
/
ulimit_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//stm: ignore
//go:build !windows
// +build !windows
// This file tests file descriptor limits; since this is an OS feature, it should not be annotated
package ulimit
import (
"fmt"
"os"
"strings"
"syscall"
"testing"
)
func TestManageFdLimit(t *testing.T) {
t.Log("Testing file descriptor count")
if _, _, err := ManageFdLimit(); err != nil {
t.Errorf("Cannot manage file descriptors")
}
}
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(),
"failed to raise ulimit to LOTUS_FD_MAX")
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")
}
}