-
Notifications
You must be signed in to change notification settings - Fork 646
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #220 from jrick/memfix
Fix incorrect unsafe usage
- Loading branch information
Showing
7 changed files
with
172 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package bbolt | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"math/rand" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func createDb(t *testing.T) (*DB, func()) { | ||
// First, create a temporary directory to be used for the duration of | ||
// this test. | ||
tempDirName, err := ioutil.TempDir("", "bboltmemtest") | ||
if err != nil { | ||
t.Fatalf("error creating temp dir: %v", err) | ||
} | ||
path := filepath.Join(tempDirName, "testdb.db") | ||
|
||
bdb, err := Open(path, 0600, nil) | ||
if err != nil { | ||
t.Fatalf("error creating bbolt db: %v", err) | ||
} | ||
|
||
cleanup := func() { | ||
bdb.Close() | ||
os.RemoveAll(tempDirName) | ||
} | ||
|
||
return bdb, cleanup | ||
} | ||
|
||
func createAndPutKeys(t *testing.T) { | ||
t.Parallel() | ||
|
||
db, cleanup := createDb(t) | ||
defer cleanup() | ||
|
||
bucketName := []byte("bucket") | ||
|
||
for i := 0; i < 100; i++ { | ||
err := db.Update(func(tx *Tx) error { | ||
nodes, err := tx.CreateBucketIfNotExists(bucketName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var key [16]byte | ||
rand.Read(key[:]) | ||
if err := nodes.Put(key[:], nil); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
} | ||
|
||
func TestManyDBs(t *testing.T) { | ||
for i := 0; i < 100; i++ { | ||
t.Run(fmt.Sprintf("%d", i), createAndPutKeys) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.