reporter_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. //change metrics
  41. mBalanceCredit.Inc(12)
  42. mBytesCredit.Inc(34)
  43. mMsgDebit.Inc(9)
  44. //store expected metrics
  45. expectedBalanceCredit := mBalanceCredit.Count()
  46. expectedBytesCredit := mBytesCredit.Count()
  47. expectedMsgDebit := mMsgDebit.Count()
  48. //give the reporter time to write the metrics to DB
  49. time.Sleep(20 * time.Millisecond)
  50. //close the DB also, or we can't create a new one
  51. metrics.Close()
  52. //clear the metrics - this effectively simulates the node having shut down...
  53. mBalanceCredit.Clear()
  54. mBytesCredit.Clear()
  55. mMsgDebit.Clear()
  56. //setup the metrics again
  57. log.Debug("Setting up metrics second time")
  58. metrics = SetupAccountingMetrics(reportInterval, filepath.Join(dir, "test.db"))
  59. defer metrics.Close()
  60. log.Debug("Done.")
  61. //now check the metrics, they should have the same value as before "shutdown"
  62. if mBalanceCredit.Count() != expectedBalanceCredit {
  63. t.Fatalf("Expected counter to be %d, but is %d", expectedBalanceCredit, mBalanceCredit.Count())
  64. }
  65. if mBytesCredit.Count() != expectedBytesCredit {
  66. t.Fatalf("Expected counter to be %d, but is %d", expectedBytesCredit, mBytesCredit.Count())
  67. }
  68. if mMsgDebit.Count() != expectedMsgDebit {
  69. t.Fatalf("Expected counter to be %d, but is %d", expectedMsgDebit, mMsgDebit.Count())
  70. }
  71. }