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.

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