memorystoragemanager_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package storage
  11. import (
  12. "testing"
  13. "devt.de/krotik/eliasdb/storage/file"
  14. )
  15. func TestMemoryStorageManager(t *testing.T) {
  16. var ret string
  17. msm := NewMemoryStorageManager("test")
  18. // Simple tests
  19. if msm.Name() != "test" {
  20. t.Error("Unexpected name")
  21. return
  22. }
  23. if err := msm.Fetch(5, &ret); err != ErrSlotNotFound {
  24. t.Error("Unexpected fetch result:", err)
  25. return
  26. }
  27. msm.SetRoot(5, 1)
  28. if msm.Root(5) != 1 {
  29. t.Error("Unexpected root", msm.Root(5))
  30. return
  31. }
  32. // Standard tests
  33. loc, _ := msm.Insert("MyString")
  34. msm.Fetch(loc, &ret)
  35. if ret != "MyString" {
  36. t.Error("Unexpected fetch result:", ret)
  37. return
  38. }
  39. if res, _ := msm.FetchCached(loc); res != "MyString" {
  40. t.Error("Unexpected fetchcached result:", res)
  41. return
  42. }
  43. msm.Update(loc, "MyOtherString")
  44. if res, _ := msm.FetchCached(loc); res != "MyOtherString" {
  45. t.Error("Unexpected fetchcached result:", res)
  46. return
  47. }
  48. if s := msm.String(); s != "MemoryStorageManager test\n"+
  49. "1 - MyOtherString\n" {
  50. t.Error("Unexpected string representation:", s)
  51. }
  52. msm.Free(loc)
  53. if res, _ := msm.FetchCached(loc); res != nil {
  54. t.Error("Unexpected fetchcached result:", res)
  55. return
  56. }
  57. // Error cases
  58. msm.AccessMap[loc] = AccessNotInCache
  59. if _, err := msm.FetchCached(loc); err != ErrNotInCache {
  60. t.Error("Unexpected fetchcached result:", err)
  61. return
  62. }
  63. msm.AccessMap[loc] = AccessCacheAndFetchSeriousError
  64. if _, err := msm.FetchCached(loc); err != file.ErrAlreadyInUse {
  65. t.Error("Unexpected fetchcached result:", err)
  66. return
  67. }
  68. if err := msm.Fetch(loc, &ret); err != file.ErrAlreadyInUse {
  69. t.Error("Unexpected fetch result:", err)
  70. return
  71. }
  72. msm.AccessMap[loc] = AccessFetchError
  73. if err := msm.Fetch(loc, &ret); err != ErrSlotNotFound {
  74. t.Error("Unexpected fetch result:", err)
  75. return
  76. }
  77. msm.AccessMap[loc] = AccessUpdateError
  78. if err := msm.Update(loc, ""); err != ErrSlotNotFound {
  79. t.Error("Unexpected update result:", err)
  80. return
  81. }
  82. msm.AccessMap[loc] = AccessFreeError
  83. if err := msm.Free(loc); err != ErrSlotNotFound {
  84. t.Error("Unexpected free result:", err)
  85. return
  86. }
  87. msm.AccessMap[msm.LocCount] = AccessInsertError
  88. if _, err := msm.Insert(""); err != file.ErrAlreadyInUse {
  89. t.Error("Unexpected insert result:", err)
  90. return
  91. }
  92. // Dummy calls
  93. msm.Flush()
  94. msm.Rollback()
  95. msm.Close()
  96. }