service_test.go 3.4 KB

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