paging_util_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 paging
  11. import (
  12. "testing"
  13. "devt.de/krotik/eliasdb/storage/file"
  14. "devt.de/krotik/eliasdb/storage/paging/view"
  15. )
  16. func TestFreePhysicalSlotManagerScale(t *testing.T) {
  17. sf, err := file.NewDefaultStorageFile(DBDIR+"/test5", false)
  18. if err != nil {
  19. t.Error(err.Error())
  20. return
  21. }
  22. psf, err := NewPagedStorageFile(sf)
  23. if err != nil {
  24. t.Error(err)
  25. return
  26. }
  27. if pc, err := CountPages(psf, view.TypeDataPage); pc != 0 || err != nil {
  28. t.Error("Unexpected page count result:", pc, err)
  29. }
  30. for i := 0; i < 5; i++ {
  31. _, err := psf.AllocatePage(view.TypeDataPage)
  32. if err != nil {
  33. t.Error(err)
  34. }
  35. if pc, err := CountPages(psf, view.TypeDataPage); pc != i+1 || err != nil {
  36. t.Error("Unexpected page count result:", pc, err)
  37. }
  38. }
  39. if pc, err := CountPages(psf, view.TypeDataPage); pc != 5 || err != nil {
  40. t.Error("Unexpected page count result:", pc, err)
  41. }
  42. r, err := sf.Get(1)
  43. if err != nil {
  44. t.Error(err)
  45. return
  46. }
  47. pc, err := CountPages(psf, view.TypeDataPage)
  48. if sfe, ok := err.(*file.StorageFileError); pc != -1 || !ok || sfe.Type != file.ErrAlreadyInUse {
  49. t.Error("Unexpected page count result:", pc, err)
  50. return
  51. }
  52. sf.ReleaseInUse(r)
  53. r, err = sf.Get(3)
  54. if err != nil {
  55. t.Error(err)
  56. return
  57. }
  58. pc, err = CountPages(psf, view.TypeDataPage)
  59. if sfe, ok := err.(*file.StorageFileError); pc != -1 || !ok || sfe.Type != file.ErrAlreadyInUse {
  60. t.Error("Unexpected page count result:", pc, err)
  61. return
  62. }
  63. sf.ReleaseInUse(r)
  64. if err := psf.Close(); err != nil {
  65. t.Error(err)
  66. return
  67. }
  68. }