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