reporter_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2018 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 protocols
  17. import (
  18. "io/ioutil"
  19. "os"
  20. "path/filepath"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/log"
  24. )
  25. //TestReporter tests that the metrics being collected for p2p accounting
  26. //are being persisted and available after restart of a node.
  27. //It simulates restarting by just recreating the DB as if the node had restarted.
  28. func TestReporter(t *testing.T) {
  29. //create a test directory
  30. dir, err := ioutil.TempDir("", "reporter-test")
  31. if err != nil {
  32. t.Fatal(err)
  33. }
  34. defer os.RemoveAll(dir)
  35. //setup the metrics
  36. log.Debug("Setting up metrics first time")
  37. reportInterval := 2 * time.Millisecond
  38. metrics := SetupAccountingMetrics(reportInterval, filepath.Join(dir, "test.db"))
  39. log.Debug("Done.")
  40. //do some metrics
  41. mBalanceCredit.Inc(12)
  42. mBytesCredit.Inc(34)
  43. mMsgDebit.Inc(9)
  44. //give the reporter time to write the metrics to DB
  45. time.Sleep(20 * time.Millisecond)
  46. //set the metrics to nil - this effectively simulates the node having shut down...
  47. mBalanceCredit = nil
  48. mBytesCredit = nil
  49. mMsgDebit = nil
  50. //close the DB also, or we can't create a new one
  51. metrics.Close()
  52. //setup the metrics again
  53. log.Debug("Setting up metrics second time")
  54. metrics = SetupAccountingMetrics(reportInterval, filepath.Join(dir, "test.db"))
  55. defer metrics.Close()
  56. log.Debug("Done.")
  57. //now check the metrics, they should have the same value as before "shutdown"
  58. if mBalanceCredit.Count() != 12 {
  59. t.Fatalf("Expected counter to be %d, but is %d", 12, mBalanceCredit.Count())
  60. }
  61. if mBytesCredit.Count() != 34 {
  62. t.Fatalf("Expected counter to be %d, but is %d", 23, mBytesCredit.Count())
  63. }
  64. if mMsgDebit.Count() != 9 {
  65. t.Fatalf("Expected counter to be %d, but is %d", 9, mMsgDebit.Count())
  66. }
  67. }