github.com/dorkamotorka/go/src@v0.0.0-20230614113921-187095f0e316/syscall/rlimit_test.go (about)

     1  // Copyright 2022 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package syscall_test
     6  
     7  import (
     8  	"os"
     9  	"runtime"
    10  	"testing"
    11  )
    12  
    13  func TestOpenFileLimit(t *testing.T) {
    14  	// For open file count,
    15  	// macOS sets the default soft limit to 256 and no hard limit.
    16  	// CentOS and Fedora set the default soft limit to 1024,
    17  	// with hard limits of 4096 and 524288, respectively.
    18  	// Check that we can open 1200 files, which proves
    19  	// that the rlimit is being raised appropriately on those systems.
    20  	fileCount := 1200
    21  
    22  	// OpenBSD has a default soft limit of 512 and hard limit of 1024.
    23  	if runtime.GOOS == "openbsd" {
    24  		fileCount = 768
    25  	}
    26  
    27  	var files []*os.File
    28  	for i := 0; i < fileCount; i++ {
    29  		f, err := os.Open("rlimit.go")
    30  		if err != nil {
    31  			t.Error(err)
    32  			break
    33  		}
    34  		files = append(files, f)
    35  	}
    36  
    37  	for _, f := range files {
    38  		f.Close()
    39  	}
    40  }