github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/ethdb/database_test.go (about)

     1  // Copyright 2014 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The Spectrum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package ethdb_test
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"os"
    24  	"strconv"
    25  	"sync"
    26  	"testing"
    27  
    28  	"github.com/SmartMeshFoundation/Spectrum/ethdb"
    29  )
    30  
    31  func newTestLDB() (*ethdb.LDBDatabase, func()) {
    32  	dirname, err := ioutil.TempDir(os.TempDir(), "ethdb_test_")
    33  	if err != nil {
    34  		panic("failed to create test file: " + err.Error())
    35  	}
    36  	db, err := ethdb.NewLDBDatabase(dirname, 0, 0)
    37  	if err != nil {
    38  		panic("failed to create test database: " + err.Error())
    39  	}
    40  
    41  	return db, func() {
    42  		db.Close()
    43  		os.RemoveAll(dirname)
    44  	}
    45  }
    46  
    47  var test_values = []string{"", "a", "1251", "\x00123\x00"}
    48  
    49  func TestLDB_PutGet(t *testing.T) {
    50  	db, remove := newTestLDB()
    51  	defer remove()
    52  	testPutGet(db, t)
    53  }
    54  
    55  func TestMemoryDB_PutGet(t *testing.T) {
    56  	db, _ := ethdb.NewMemDatabase()
    57  	testPutGet(db, t)
    58  }
    59  
    60  func testPutGet(db ethdb.Database, t *testing.T) {
    61  	t.Parallel()
    62  
    63  	for _, v := range test_values {
    64  		err := db.Put([]byte(v), []byte(v))
    65  		if err != nil {
    66  			t.Fatalf("put failed: %v", err)
    67  		}
    68  	}
    69  
    70  	for _, v := range test_values {
    71  		data, err := db.Get([]byte(v))
    72  		if err != nil {
    73  			t.Fatalf("get failed: %v", err)
    74  		}
    75  		if !bytes.Equal(data, []byte(v)) {
    76  			t.Fatalf("get returned wrong result, got %q expected %q", string(data), v)
    77  		}
    78  	}
    79  
    80  	for _, v := range test_values {
    81  		err := db.Put([]byte(v), []byte("?"))
    82  		if err != nil {
    83  			t.Fatalf("put override failed: %v", err)
    84  		}
    85  	}
    86  
    87  	for _, v := range test_values {
    88  		data, err := db.Get([]byte(v))
    89  		if err != nil {
    90  			t.Fatalf("get failed: %v", err)
    91  		}
    92  		if !bytes.Equal(data, []byte("?")) {
    93  			t.Fatalf("get returned wrong result, got %q expected ?", string(data))
    94  		}
    95  	}
    96  
    97  	for _, v := range test_values {
    98  		orig, err := db.Get([]byte(v))
    99  		if err != nil {
   100  			t.Fatalf("get failed: %v", err)
   101  		}
   102  		orig[0] = byte(0xff)
   103  		data, err := db.Get([]byte(v))
   104  		if err != nil {
   105  			t.Fatalf("get failed: %v", err)
   106  		}
   107  		if !bytes.Equal(data, []byte("?")) {
   108  			t.Fatalf("get returned wrong result, got %q expected ?", string(data))
   109  		}
   110  	}
   111  
   112  	for _, v := range test_values {
   113  		err := db.Delete([]byte(v))
   114  		if err != nil {
   115  			t.Fatalf("delete %q failed: %v", v, err)
   116  		}
   117  	}
   118  
   119  	for _, v := range test_values {
   120  		_, err := db.Get([]byte(v))
   121  		if err == nil {
   122  			t.Fatalf("got deleted value %q", v)
   123  		}
   124  	}
   125  }
   126  
   127  func TestLDB_ParallelPutGet(t *testing.T) {
   128  	db, remove := newTestLDB()
   129  	defer remove()
   130  	testParallelPutGet(db, t)
   131  }
   132  
   133  func TestMemoryDB_ParallelPutGet(t *testing.T) {
   134  	db, _ := ethdb.NewMemDatabase()
   135  	testParallelPutGet(db, t)
   136  }
   137  
   138  func testParallelPutGet(db ethdb.Database, t *testing.T) {
   139  	const n = 8
   140  	var pending sync.WaitGroup
   141  
   142  	pending.Add(n)
   143  	for i := 0; i < n; i++ {
   144  		go func(key string) {
   145  			defer pending.Done()
   146  			err := db.Put([]byte(key), []byte("v"+key))
   147  			if err != nil {
   148  				panic("put failed: " + err.Error())
   149  			}
   150  		}(strconv.Itoa(i))
   151  	}
   152  	pending.Wait()
   153  
   154  	pending.Add(n)
   155  	for i := 0; i < n; i++ {
   156  		go func(key string) {
   157  			defer pending.Done()
   158  			data, err := db.Get([]byte(key))
   159  			if err != nil {
   160  				panic("get failed: " + err.Error())
   161  			}
   162  			if !bytes.Equal(data, []byte("v"+key)) {
   163  				panic(fmt.Sprintf("get failed, got %q expected %q", []byte(data), []byte("v"+key)))
   164  			}
   165  		}(strconv.Itoa(i))
   166  	}
   167  	pending.Wait()
   168  
   169  	pending.Add(n)
   170  	for i := 0; i < n; i++ {
   171  		go func(key string) {
   172  			defer pending.Done()
   173  			err := db.Delete([]byte(key))
   174  			if err != nil {
   175  				panic("delete failed: " + err.Error())
   176  			}
   177  		}(strconv.Itoa(i))
   178  	}
   179  	pending.Wait()
   180  
   181  	pending.Add(n)
   182  	for i := 0; i < n; i++ {
   183  		go func(key string) {
   184  			defer pending.Done()
   185  			_, err := db.Get([]byte(key))
   186  			if err == nil {
   187  				panic("get succeeded")
   188  			}
   189  		}(strconv.Itoa(i))
   190  	}
   191  	pending.Wait()
   192  }