Skip to content

Commit

Permalink
explicitly close os.File to force release of file descriptor (filecoi…
Browse files Browse the repository at this point in the history
…n-project#97)

* fix: affirm that we don't run out of file descriptors

* be sure to close file instead of waiting for GC
  • Loading branch information
laser authored and gracenoah committed Dec 20, 2020
1 parent 8f9e7b9 commit fbb9f97
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions proofs_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ffi

import (
"bytes"
"crypto/rand"
"io"
"io/ioutil"
"math/big"
"testing"

Expand Down Expand Up @@ -68,6 +70,45 @@ func TestJsonMarshalSymmetry(t *testing.T) {
}
}

func TestDoesNotExhaustFileDescriptors(t *testing.T) {
m := 500 // loops
n := uint64(508) // quantity of piece bytes

for i := 0; i < m; i++ {
// create a temporary file over which we'll compute CommP
file, err := ioutil.TempFile("", "")
if err != nil {
panic(err)
}

// create a slice of random bytes (represents our piece)
b := make([]byte, n)

// load up our byte slice with random bytes
if _, err = rand.Read(b); err != nil {
panic(err)
}

// write buffer to temp file
if _, err := bytes.NewBuffer(b).WriteTo(file); err != nil {
panic(err)
}

// seek to beginning of file
if _, err := file.Seek(0, 0); err != nil {
panic(err)
}

if _, err = GeneratePieceCID(abi.RegisteredProof_StackedDRG2KiBSeal, file.Name(), abi.UnpaddedPieceSize(n)); err != nil {
panic(err)
}

if err = file.Close(); err != nil {
panic(err)
}
}
}

func newTestingTeeHelper(t *testing.T) *testingTeeHelper {
return &testingTeeHelper{t: t}
}
Expand Down

0 comments on commit fbb9f97

Please sign in to comment.