github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/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 // TODO: move to MemMapFS 31 // Create a temporary folder and ensure no database is contained within 32 dir, err := ioutil.TempDir("", "") 33 if err != nil { 34 t.Fatalf("failed to create temporary data directory: %v", err) 35 } 36 defer os.RemoveAll(dir) 37 38 if _, err := os.Stat(filepath.Join(dir, "database")); err == nil { 39 t.Fatalf("non-created database already exists") 40 } 41 // Request the opening/creation of a database and ensure it persists to disk 42 ctx := &ServiceContext{datadir: dir} 43 db, err := ctx.OpenDatabase("persistent", 0, 0) 44 if err != nil { 45 t.Fatalf("failed to open persistent database: %v", err) 46 } 47 db.Close() 48 49 if _, err := os.Stat(filepath.Join(dir, "persistent")); err != nil { 50 t.Fatalf("persistent database doesn't exists: %v", err) 51 } 52 // Request th opening/creation of an ephemeral database and ensure it's not persisted 53 ctx = &ServiceContext{datadir: ""} 54 db, err = ctx.OpenDatabase("ephemeral", 0, 0) 55 if err != nil { 56 t.Fatalf("failed to open ephemeral database: %v", err) 57 } 58 db.Close() 59 60 if _, err := os.Stat(filepath.Join(dir, "ephemeral")); err == nil { 61 t.Fatalf("ephemeral database exists") 62 } 63 } 64 65 // Tests that already constructed services can be retrieves by later ones. 66 func TestContextServices(t *testing.T) { 67 stack, err := New(testNodeConfig()) 68 if err != nil { 69 t.Fatalf("failed to create protocol stack: %v", err) 70 } 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 }