blob_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 v1
  11. import (
  12. "fmt"
  13. "testing"
  14. "devt.de/krotik/eliasdb/storage"
  15. )
  16. func TestBlob(t *testing.T) {
  17. queryURL := "http://localhost" + TESTPORT + EndpointBlob
  18. // Test error message
  19. _, _, res := sendTestRequest(queryURL, "GET", nil)
  20. if res != "Need a partition and a specific data ID" {
  21. t.Error("Unexpected response:", res)
  22. return
  23. }
  24. _, _, res = sendTestRequest(queryURL, "POST", nil)
  25. if res != "Need a partition" {
  26. t.Error("Unexpected response:", res)
  27. return
  28. }
  29. _, _, res = sendTestRequest(queryURL, "PUT", nil)
  30. if res != "Need a partition and a specific data ID" {
  31. t.Error("Unexpected response:", res)
  32. return
  33. }
  34. _, _, res = sendTestRequest(queryURL, "DELETE", nil)
  35. if res != "Need a partition and a specific data ID" {
  36. t.Error("Unexpected response:", res)
  37. return
  38. }
  39. queryURL = "http://localhost" + TESTPORT + EndpointBlob + "mypart/"
  40. _, _, res = sendTestRequest(queryURL+"a", "GET", nil)
  41. if res != "Could not decode data ID: strconv.ParseUint: parsing \"a\": invalid syntax" {
  42. t.Error("Unexpected response:", res)
  43. return
  44. }
  45. _, _, res = sendTestRequest(queryURL+"a", "PUT", nil)
  46. if res != "Could not decode data ID: strconv.ParseUint: parsing \"a\": invalid syntax" {
  47. t.Error("Unexpected response:", res)
  48. return
  49. }
  50. _, _, res = sendTestRequest(queryURL+"a", "DELETE", nil)
  51. if res != "Could not decode data ID: strconv.ParseUint: parsing \"a\": invalid syntax" {
  52. t.Error("Unexpected response:", res)
  53. return
  54. }
  55. // Test normal storage
  56. st, _, res := sendTestRequest(queryURL, "POST", []byte{0x0b, 0x00, 0x00, 0x0b, 0x01, 0x0e, 0x05})
  57. if st != "200 OK" || res != `
  58. {
  59. "id": 1
  60. }`[1:] {
  61. t.Error("Unexpected response:", st, res)
  62. return
  63. }
  64. msm := gmMSM.StorageManager("mypart"+StorageSuffixBlob, false)
  65. msm.(*storage.MemoryStorageManager).AccessMap[2] = storage.AccessInsertError
  66. st, _, res = sendTestRequest(queryURL, "POST", []byte{0x0b, 0x00, 0x00, 0x0b, 0x01, 0x0e, 0x05})
  67. if st != "500 Internal Server Error" || res != "Record is already in-use (<memory> - )" {
  68. t.Error("Unexpected response:", st, res)
  69. return
  70. }
  71. delete(msm.(*storage.MemoryStorageManager).AccessMap, 2)
  72. // Simulate a change miss
  73. msm.(*storage.MemoryStorageManager).AccessMap[1] = storage.AccessNotInCache
  74. st, _, res = sendTestRequest(queryURL+"1", "GET", nil)
  75. if st != "200 OK" || fmt.Sprintf("%x", res) != "0b00000b010e05" {
  76. t.Error("Unexpected response:", st, fmt.Sprintf("%x", res))
  77. return
  78. }
  79. delete(msm.(*storage.MemoryStorageManager).AccessMap, 1)
  80. st, _, res = sendTestRequest(queryURL+"1", "GET", nil)
  81. if st != "200 OK" || fmt.Sprintf("%x", res) != "0b00000b010e05" {
  82. t.Error("Unexpected response:", st, fmt.Sprintf("%x", res))
  83. return
  84. }
  85. msm.(*storage.MemoryStorageManager).AccessMap[1] = storage.AccessUpdateError
  86. st, _, res = sendTestRequest(queryURL+"1", "PUT", []byte{0x0b, 0x0c})
  87. if st != "500 Internal Server Error" || res != "Slot not found (mystorage/mypart.blob - Location:1)" {
  88. t.Error("Unexpected response:", st, res)
  89. return
  90. }
  91. delete(msm.(*storage.MemoryStorageManager).AccessMap, 1)
  92. st, _, res = sendTestRequest(queryURL+"1", "PUT", []byte{0x0b, 0x0c})
  93. if st != "200 OK" {
  94. t.Error("Unexpected response:", st, fmt.Sprintf("%x", res))
  95. return
  96. }
  97. st, _, res = sendTestRequest(queryURL+"1", "GET", nil)
  98. if st != "200 OK" || fmt.Sprintf("%x", res) != "0b0c" {
  99. t.Error("Unexpected response:", st, fmt.Sprintf("%x", res))
  100. return
  101. }
  102. msm.(*storage.MemoryStorageManager).AccessMap[1] = storage.AccessFreeError
  103. st, _, res = sendTestRequest(queryURL+"1", "DELETE", nil)
  104. if st != "500 Internal Server Error" || res != "Slot not found (mystorage/mypart.blob - Location:1)" {
  105. t.Error("Unexpected response:", st, res)
  106. return
  107. }
  108. delete(msm.(*storage.MemoryStorageManager).AccessMap, 1)
  109. st, _, res = sendTestRequest(queryURL+"1", "DELETE", nil)
  110. if st != "200 OK" {
  111. t.Error("Unexpected response:", st, fmt.Sprintf("%x", res))
  112. return
  113. }
  114. st, _, res = sendTestRequest(queryURL+"1", "GET", nil)
  115. if st != "200 OK" || fmt.Sprintf("%x", res) != "" {
  116. t.Error("Unexpected response:", st, fmt.Sprintf("%x", res))
  117. return
  118. }
  119. st, _, res = sendTestRequest(queryURL+"2", "GET", nil)
  120. if st != "200 OK" || fmt.Sprintf("%x", res) != "" {
  121. t.Error("Unexpected response:", st, fmt.Sprintf("%x", res))
  122. return
  123. }
  124. }