github.com/cuiweixie/go-ethereum@v1.8.2-0.20180303084001-66cd41af1e38/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  	// Define a verifier that ensures a NoopA is before it and NoopB after
    71  	verifier := func(ctx *ServiceContext) (Service, error) {
    72  		var objA *NoopServiceA
    73  		if ctx.Service(&objA) != nil {
    74  			return nil, fmt.Errorf("former service not found")
    75  		}
    76  		var objB *NoopServiceB
    77  		if err := ctx.Service(&objB); err != ErrServiceUnknown {
    78  			return nil, fmt.Errorf("latters lookup error mismatch: have %v, want %v", err, ErrServiceUnknown)
    79  		}
    80  		return new(NoopService), nil
    81  	}
    82  	// Register the collection of services
    83  	if err := stack.Register(NewNoopServiceA); err != nil {
    84  		t.Fatalf("former failed to register service: %v", err)
    85  	}
    86  	if err := stack.Register(verifier); err != nil {
    87  		t.Fatalf("failed to register service verifier: %v", err)
    88  	}
    89  	if err := stack.Register(NewNoopServiceB); err != nil {
    90  		t.Fatalf("latter failed to register service: %v", err)
    91  	}
    92  	// Start the protocol stack and ensure services are constructed in order
    93  	if err := stack.Start(); err != nil {
    94  		t.Fatalf("failed to start stack: %v", err)
    95  	}
    96  	defer stack.Stop()
    97  }