github.com/ethereum/go-ethereum@v1.16.1/core/rawdb/eradb/eradb_test.go (about)

     1  // Copyright 2025 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum 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 go-ethereum 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 go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package eradb
    18  
    19  import (
    20  	"sync"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/core/types"
    24  	"github.com/ethereum/go-ethereum/rlp"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestEraDatabase(t *testing.T) {
    30  	db, err := New("testdata")
    31  	require.NoError(t, err)
    32  	defer db.Close()
    33  
    34  	r, err := db.GetRawBody(175881)
    35  	require.NoError(t, err)
    36  	var body *types.Body
    37  	err = rlp.DecodeBytes(r, &body)
    38  	require.NoError(t, err)
    39  	require.NotNil(t, body, "block body not found")
    40  	assert.Equal(t, 3, len(body.Transactions))
    41  
    42  	r, err = db.GetRawReceipts(175881)
    43  	require.NoError(t, err)
    44  	var receipts []*types.ReceiptForStorage
    45  	err = rlp.DecodeBytes(r, &receipts)
    46  	require.NoError(t, err)
    47  	require.NotNil(t, receipts, "receipts not found")
    48  	assert.Equal(t, 3, len(receipts), "receipts length mismatch")
    49  }
    50  
    51  func TestEraDatabaseConcurrentOpen(t *testing.T) {
    52  	db, err := New("testdata")
    53  	require.NoError(t, err)
    54  	defer db.Close()
    55  
    56  	const N = 25
    57  	var wg sync.WaitGroup
    58  	wg.Add(N)
    59  	for range N {
    60  		go func() {
    61  			defer wg.Done()
    62  			r, err := db.GetRawBody(1024)
    63  			if err != nil {
    64  				t.Error("err:", err)
    65  			}
    66  			if len(r) == 0 {
    67  				t.Error("empty body")
    68  			}
    69  		}()
    70  	}
    71  	wg.Wait()
    72  }
    73  
    74  func TestEraDatabaseConcurrentOpenClose(t *testing.T) {
    75  	db, err := New("testdata")
    76  	require.NoError(t, err)
    77  	defer db.Close()
    78  
    79  	const N = 10
    80  	var wg sync.WaitGroup
    81  	wg.Add(N)
    82  	for range N {
    83  		go func() {
    84  			defer wg.Done()
    85  			r, err := db.GetRawBody(1024)
    86  			if err == errClosed {
    87  				return
    88  			}
    89  			if err != nil {
    90  				t.Error("err:", err)
    91  			}
    92  			if len(r) == 0 {
    93  				t.Error("empty body")
    94  			}
    95  		}()
    96  	}
    97  	wg.Add(1)
    98  	go func() {
    99  		defer wg.Done()
   100  		db.Close()
   101  	}()
   102  	wg.Wait()
   103  }