memorystoragemanager_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.(*ManagerError).Type != 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.(*ManagerError).Type != ErrNotInCache {
  60. t.Error("Unexpected fetchcached result:", err)
  61. return
  62. }
  63. msm.AccessMap[loc] = AccessCacheAndFetchSeriousError
  64. _, err := msm.FetchCached(loc)
  65. if sfe, ok := err.(*file.StorageFileError); !ok || sfe.Type != file.ErrAlreadyInUse {
  66. t.Error("Unexpected fetchcached result:", err)
  67. return
  68. }
  69. err = msm.Fetch(loc, &ret)
  70. if sfe, ok := err.(*file.StorageFileError); !ok || sfe.Type != file.ErrAlreadyInUse {
  71. t.Error("Unexpected fetch result:", err)
  72. return
  73. }
  74. msm.AccessMap[loc] = AccessFetchError
  75. if err := msm.Fetch(loc, &ret); err.(*ManagerError).Type != ErrSlotNotFound {
  76. t.Error("Unexpected fetch result:", err)
  77. return
  78. }
  79. msm.AccessMap[loc] = AccessUpdateError
  80. if err := msm.Update(loc, ""); err.(*ManagerError).Type != ErrSlotNotFound {
  81. t.Error("Unexpected update result:", err)
  82. return
  83. }
  84. msm.AccessMap[loc] = AccessFreeError
  85. if err := msm.Free(loc); err.(*ManagerError).Type != ErrSlotNotFound {
  86. t.Error("Unexpected free result:", err)
  87. return
  88. }
  89. msm.AccessMap[msm.LocCount] = AccessInsertError
  90. _, err = msm.Insert("")
  91. if sfe, ok := err.(*file.StorageFileError); !ok || sfe.Type != file.ErrAlreadyInUse {
  92. t.Error("Unexpected insert result:", err)
  93. return
  94. }
  95. // Dummy calls
  96. msm.Flush()
  97. msm.Rollback()
  98. msm.Close()
  99. }