github.com/klaytn/klaytn@v1.12.1/node/service_test.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of go-ethereum.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from node/service_test.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package node
    22  
    23  import (
    24  	"fmt"
    25  	"os"
    26  	"path/filepath"
    27  	"reflect"
    28  	"testing"
    29  
    30  	"github.com/klaytn/klaytn/accounts"
    31  	"github.com/klaytn/klaytn/event"
    32  	"github.com/klaytn/klaytn/storage/database"
    33  )
    34  
    35  // Tests that databases are correctly created persistent or ephemeral based on
    36  // the configured service context.
    37  func TestContextDatabases(t *testing.T) {
    38  	// Create a temporary folder and ensure no database is contained within
    39  	dir, err := os.MkdirTemp("", "")
    40  	if err != nil {
    41  		t.Fatalf("failed to create temporary data directory: %v", err)
    42  	}
    43  	defer os.RemoveAll(dir)
    44  
    45  	if _, err := os.Stat(filepath.Join(dir, "database")); err == nil {
    46  		t.Fatalf("non-created database already exists")
    47  	}
    48  	// Request the opening/creation of a database and ensure it persists to disk
    49  	ctx := NewServiceContext(&Config{Name: "unit-test", DataDir: dir}, map[reflect.Type]Service{}, &event.TypeMux{}, &accounts.Manager{})
    50  	dbc := &database.DBConfig{
    51  		Dir: "persistent", DBType: database.LevelDB,
    52  		LevelDBCacheSize: 0, OpenFilesLimit: 0,
    53  	}
    54  	db := ctx.OpenDatabase(dbc)
    55  	db.Close()
    56  
    57  	if _, err := os.Stat(filepath.Join(dir, "unit-test", "persistent")); err != nil {
    58  		t.Fatalf("persistent database doesn't exists: %v", err)
    59  	}
    60  	// Request th opening/creation of an ephemeral database and ensure it's not persisted
    61  	ctx = NewServiceContext(&Config{DataDir: ""}, map[reflect.Type]Service{}, &event.TypeMux{}, &accounts.Manager{})
    62  	dbc = &database.DBConfig{
    63  		Dir: "ephemeral", DBType: database.LevelDB,
    64  		LevelDBCacheSize: 0, OpenFilesLimit: 0,
    65  	}
    66  
    67  	db = ctx.OpenDatabase(dbc)
    68  	db.Close()
    69  
    70  	if _, err := os.Stat(filepath.Join(dir, "ephemeral")); err == nil {
    71  		t.Fatalf("ephemeral database exists")
    72  	}
    73  }
    74  
    75  // Tests that already constructed services can be retrieves by later ones.
    76  func TestContextServices(t *testing.T) {
    77  	stack, err := New(testNodeConfig())
    78  	if err != nil {
    79  		t.Fatalf("failed to create protocol stack: %v", err)
    80  	}
    81  	// Define a verifier that ensures a NoopA is before it and NoopB after
    82  	verifier := func(ctx *ServiceContext) (Service, error) {
    83  		var objA *NoopServiceA
    84  		if ctx.Service(&objA) != nil {
    85  			return nil, fmt.Errorf("former service not found")
    86  		}
    87  		var objB *NoopServiceB
    88  		if err := ctx.Service(&objB); err != ErrServiceUnknown {
    89  			return nil, fmt.Errorf("latters lookup error mismatch: have %v, want %v", err, ErrServiceUnknown)
    90  		}
    91  		return new(NoopService), nil
    92  	}
    93  	// Register the collection of services
    94  	if err := stack.Register(NewNoopServiceA); err != nil {
    95  		t.Fatalf("former failed to register service: %v", err)
    96  	}
    97  	if err := stack.Register(verifier); err != nil {
    98  		t.Fatalf("failed to register service verifier: %v", err)
    99  	}
   100  	if err := stack.Register(NewNoopServiceB); err != nil {
   101  		t.Fatalf("latter failed to register service: %v", err)
   102  	}
   103  	// Start the protocol stack and ensure services are constructed in order
   104  	if err := stack.Start(); err != nil {
   105  		t.Fatalf("failed to start stack: %v", err)
   106  	}
   107  	defer stack.Stop()
   108  }