github.com/bhojpur/cache@v0.0.4/pkg/memory/manydbs_test.go (about)

     1  package memory
     2  
     3  // Copyright (c) 2018 Bhojpur Consulting Private Limited, India. All rights reserved.
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy
     6  // of this software and associated documentation files (the "Software"), to deal
     7  // in the Software without restriction, including without limitation the rights
     8  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     9  // copies of the Software, and to permit persons to whom the Software is
    10  // furnished to do so, subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in
    13  // all copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    17  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    18  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    19  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    20  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    21  // THE SOFTWARE.
    22  
    23  import (
    24  	"fmt"
    25  	"math/rand"
    26  	"os"
    27  	"path/filepath"
    28  	"testing"
    29  )
    30  
    31  func createDb(t *testing.T) (*DB, func()) {
    32  	// First, create a temporary directory to be used for the duration of
    33  	// this test.
    34  	tempDirName, err := os.MkdirTemp("", "bhojpur_cache_test")
    35  	if err != nil {
    36  		t.Fatalf("error creating temp dir: %v", err)
    37  	}
    38  	path := filepath.Join(tempDirName, "testdb.db")
    39  
    40  	bdb, err := Open(path, 0600, nil)
    41  	if err != nil {
    42  		t.Fatalf("error creating Bhojpur Cache in-memory database: %v", err)
    43  	}
    44  
    45  	cleanup := func() {
    46  		bdb.Close()
    47  		os.RemoveAll(tempDirName)
    48  	}
    49  
    50  	return bdb, cleanup
    51  }
    52  
    53  func createAndPutKeys(t *testing.T) {
    54  	t.Parallel()
    55  
    56  	db, cleanup := createDb(t)
    57  	defer cleanup()
    58  
    59  	bucketName := []byte("bucket")
    60  
    61  	for i := 0; i < 100; i++ {
    62  		err := db.Update(func(tx *Tx) error {
    63  			nodes, err := tx.CreateBucketIfNotExists(bucketName)
    64  			if err != nil {
    65  				return err
    66  			}
    67  
    68  			var key [16]byte
    69  			rand.Read(key[:])
    70  			if err := nodes.Put(key[:], nil); err != nil {
    71  				return err
    72  			}
    73  
    74  			return nil
    75  		})
    76  		if err != nil {
    77  			t.Fatal(err)
    78  		}
    79  	}
    80  }
    81  
    82  func TestManyDBs(t *testing.T) {
    83  	if testing.Short() {
    84  		t.Skip("skipping test in short mode")
    85  	}
    86  
    87  	for i := 0; i < 100; i++ {
    88  		t.Run(fmt.Sprintf("%d", i), createAndPutKeys)
    89  	}
    90  }