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