github.com/JFJun/bsc@v1.0.0/node/service_test.go (about)

     1  // Copyright 2015 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 node
    18  
    19  import (
    20  	"fmt"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"testing"
    25  )
    26  
    27  // Tests that databases are correctly created persistent or ephemeral based on
    28  // the configured service context.
    29  func TestContextDatabases(t *testing.T) {
    30  	// Create a temporary folder and ensure no database is contained within
    31  	dir, err := ioutil.TempDir("", "")
    32  	if err != nil {
    33  		t.Fatalf("failed to create temporary data directory: %v", err)
    34  	}
    35  	defer os.RemoveAll(dir)
    36  
    37  	if _, err := os.Stat(filepath.Join(dir, "database")); err == nil {
    38  		t.Fatalf("non-created database already exists")
    39  	}
    40  	// Request the opening/creation of a database and ensure it persists to disk
    41  	ctx := &ServiceContext{Config: Config{Name: "unit-test", DataDir: dir}}
    42  	db, err := ctx.OpenDatabase("persistent", 0, 0, "")
    43  	if err != nil {
    44  		t.Fatalf("failed to open persistent database: %v", err)
    45  	}
    46  	db.Close()
    47  
    48  	if _, err := os.Stat(filepath.Join(dir, "unit-test", "persistent")); err != nil {
    49  		t.Fatalf("persistent database doesn't exists: %v", err)
    50  	}
    51  	// Request th opening/creation of an ephemeral database and ensure it's not persisted
    52  	ctx = &ServiceContext{Config: Config{DataDir: ""}}
    53  	db, err = ctx.OpenDatabase("ephemeral", 0, 0, "")
    54  	if err != nil {
    55  		t.Fatalf("failed to open ephemeral database: %v", err)
    56  	}
    57  	db.Close()
    58  
    59  	if _, err := os.Stat(filepath.Join(dir, "ephemeral")); err == nil {
    60  		t.Fatalf("ephemeral database exists")
    61  	}
    62  }
    63  
    64  // Tests that already constructed services can be retrieves by later ones.
    65  func TestContextServices(t *testing.T) {
    66  	stack, err := New(testNodeConfig())
    67  	if err != nil {
    68  		t.Fatalf("failed to create protocol stack: %v", err)
    69  	}
    70  	defer stack.Close()
    71  	// Define a verifier that ensures a NoopA is before it and NoopB after
    72  	verifier := func(ctx *ServiceContext) (Service, error) {
    73  		var objA *NoopServiceA
    74  		if ctx.Service(&objA) != nil {
    75  			return nil, fmt.Errorf("former service not found")
    76  		}
    77  		var objB *NoopServiceB
    78  		if err := ctx.Service(&objB); err != ErrServiceUnknown {
    79  			return nil, fmt.Errorf("latters lookup error mismatch: have %v, want %v", err, ErrServiceUnknown)
    80  		}
    81  		return new(NoopService), nil
    82  	}
    83  	// Register the collection of services
    84  	if err := stack.Register(NewNoopServiceA); err != nil {
    85  		t.Fatalf("former failed to register service: %v", err)
    86  	}
    87  	if err := stack.Register(verifier); err != nil {
    88  		t.Fatalf("failed to register service verifier: %v", err)
    89  	}
    90  	if err := stack.Register(NewNoopServiceB); err != nil {
    91  		t.Fatalf("latter failed to register service: %v", err)
    92  	}
    93  	// Start the protocol stack and ensure services are constructed in order
    94  	if err := stack.Start(); err != nil {
    95  		t.Fatalf("failed to start stack: %v", err)
    96  	}
    97  	defer stack.Stop()
    98  }