You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.7 KiB

  1. package arbo
  2. import (
  3. "bytes"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. "time"
  9. qt "github.com/frankban/quicktest"
  10. "go.vocdoni.io/dvote/db"
  11. "go.vocdoni.io/dvote/db/badgerdb"
  12. )
  13. func checkRoots(c *qt.C, tree1, tree2 *Tree) {
  14. root1, err := tree1.Root()
  15. c.Assert(err, qt.IsNil)
  16. root2, err := tree2.Root()
  17. c.Assert(err, qt.IsNil)
  18. if !bytes.Equal(root2, root1) {
  19. dir := "err-dump"
  20. if _, err := os.Stat(dir); os.IsNotExist(err) {
  21. err := os.Mkdir(dir, os.ModePerm)
  22. c.Assert(err, qt.IsNil)
  23. }
  24. // store tree1
  25. storeTree(c, tree1, dir+"/tree1")
  26. // store tree2
  27. storeTree(c, tree2, dir+"/tree2")
  28. root1, err := tree1.Root()
  29. c.Assert(err, qt.IsNil)
  30. root2, err := tree2.Root()
  31. c.Assert(err, qt.IsNil)
  32. c.Check(root2, qt.DeepEquals, root1)
  33. }
  34. }
  35. func storeTree(c *qt.C, tree *Tree, path string) {
  36. dump, err := tree.Dump(nil)
  37. c.Assert(err, qt.IsNil)
  38. err = ioutil.WriteFile(path+"-"+time.Now().String()+".debug", dump, 0600)
  39. c.Assert(err, qt.IsNil)
  40. }
  41. // nolint:unused
  42. func readTree(c *qt.C, tree *Tree, path string) {
  43. b, err := ioutil.ReadFile(path) //nolint:gosec
  44. c.Assert(err, qt.IsNil)
  45. err = tree.ImportDump(b)
  46. c.Assert(err, qt.IsNil)
  47. }
  48. // nolint:unused
  49. func importDumpLoopAdd(tree *Tree, b []byte) error {
  50. r := bytes.NewReader(b)
  51. var err error
  52. for {
  53. l := make([]byte, 2)
  54. _, err = io.ReadFull(r, l)
  55. if err == io.EOF {
  56. break
  57. } else if err != nil {
  58. return err
  59. }
  60. k := make([]byte, l[0])
  61. _, err = io.ReadFull(r, k)
  62. if err != nil {
  63. return err
  64. }
  65. v := make([]byte, l[1])
  66. _, err = io.ReadFull(r, v)
  67. if err != nil {
  68. return err
  69. }
  70. err = tree.Add(k, v)
  71. if err != nil {
  72. return err
  73. }
  74. }
  75. return nil
  76. }
  77. func TestReadTreeDBG(t *testing.T) {
  78. t.Skip() // test just for debugging purposes, disabled by default
  79. c := qt.New(t)
  80. database1, err := badgerdb.New(db.Options{Path: c.TempDir()})
  81. c.Assert(err, qt.IsNil)
  82. tree1, err := NewTree(database1, 100, HashFunctionBlake2b)
  83. c.Assert(err, qt.IsNil)
  84. database2, err := badgerdb.New(db.Options{Path: c.TempDir()})
  85. c.Assert(err, qt.IsNil)
  86. tree2, err := NewTree(database2, 100, HashFunctionBlake2b)
  87. c.Assert(err, qt.IsNil)
  88. // tree1 is generated by a loop of .Add
  89. path := "err-dump/tree1-2021-06-03 16:45:54.104449306 +0200 CEST m=+0.073874545.debug"
  90. b, err := ioutil.ReadFile(path)
  91. c.Assert(err, qt.IsNil)
  92. err = importDumpLoopAdd(tree1, b)
  93. c.Assert(err, qt.IsNil)
  94. // tree2 is generated by .AddBatch
  95. path = "err-dump/tree2-2021-06-03 16:45:54.104525519 +0200 CEST m=+0.073950756.debug"
  96. readTree(c, tree2, path)
  97. // tree1.PrintGraphvizFirstNLevels(nil, 6)
  98. // tree2.PrintGraphvizFirstNLevels(nil, 6)
  99. root1, err := tree1.Root()
  100. c.Assert(err, qt.IsNil)
  101. root2, err := tree2.Root()
  102. c.Assert(err, qt.IsNil)
  103. c.Check(root2, qt.DeepEquals, root1)
  104. }