datatype2.tcl 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. set testmodule [file normalize tests/modules/datatype2.so]
  2. start_server {tags {"modules"}} {
  3. r module load $testmodule
  4. test "datatype2: test mem alloc and free" {
  5. r flushall
  6. r select 0
  7. assert_equal 3 [r mem.alloc k1 3]
  8. assert_equal 2 [r mem.alloc k2 2]
  9. r select 1
  10. assert_equal 1 [r mem.alloc k1 1]
  11. assert_equal 5 [r mem.alloc k2 5]
  12. r select 0
  13. assert_equal 1 [r mem.free k1]
  14. assert_equal 1 [r mem.free k2]
  15. r select 1
  16. assert_equal 1 [r mem.free k1]
  17. assert_equal 1 [r mem.free k2]
  18. }
  19. test "datatype2: test del and unlink" {
  20. r flushall
  21. assert_equal 100 [r mem.alloc k1 100]
  22. assert_equal 60 [r mem.alloc k2 60]
  23. assert_equal 1 [r unlink k1]
  24. assert_equal 1 [r del k2]
  25. }
  26. test "datatype2: test read and write" {
  27. r flushall
  28. assert_equal 3 [r mem.alloc k1 3]
  29. set data datatype2
  30. assert_equal [string length $data] [r mem.write k1 0 $data]
  31. assert_equal $data [r mem.read k1 0]
  32. }
  33. test "datatype2: test rdb save and load" {
  34. r flushall
  35. r select 0
  36. set data k1
  37. assert_equal 3 [r mem.alloc k1 3]
  38. assert_equal [string length $data] [r mem.write k1 1 $data]
  39. set data k2
  40. assert_equal 2 [r mem.alloc k2 2]
  41. assert_equal [string length $data] [r mem.write k2 0 $data]
  42. r select 1
  43. set data k3
  44. assert_equal 3 [r mem.alloc k3 3]
  45. assert_equal [string length $data] [r mem.write k3 1 $data]
  46. set data k4
  47. assert_equal 2 [r mem.alloc k4 2]
  48. assert_equal [string length $data] [r mem.write k4 0 $data]
  49. r bgsave
  50. waitForBgsave r
  51. r debug reload
  52. r select 0
  53. assert_equal k1 [r mem.read k1 1]
  54. assert_equal k2 [r mem.read k2 0]
  55. r select 1
  56. assert_equal k3 [r mem.read k3 1]
  57. assert_equal k4 [r mem.read k4 0]
  58. }
  59. test "datatype2: test aof rewrite" {
  60. r flushall
  61. r select 0
  62. set data k1
  63. assert_equal 3 [r mem.alloc k1 3]
  64. assert_equal [string length $data] [r mem.write k1 1 $data]
  65. set data k2
  66. assert_equal 2 [r mem.alloc k2 2]
  67. assert_equal [string length $data] [r mem.write k2 0 $data]
  68. r select 1
  69. set data k3
  70. assert_equal 3 [r mem.alloc k3 3]
  71. assert_equal [string length $data] [r mem.write k3 1 $data]
  72. set data k4
  73. assert_equal 2 [r mem.alloc k4 2]
  74. assert_equal [string length $data] [r mem.write k4 0 $data]
  75. r bgrewriteaof
  76. waitForBgrewriteaof r
  77. r debug loadaof
  78. r select 0
  79. assert_equal k1 [r mem.read k1 1]
  80. assert_equal k2 [r mem.read k2 0]
  81. r select 1
  82. assert_equal k3 [r mem.read k3 1]
  83. assert_equal k4 [r mem.read k4 0]
  84. }
  85. test "datatype2: test copy" {
  86. r flushall
  87. r select 0
  88. set data k1
  89. assert_equal 3 [r mem.alloc k1 3]
  90. assert_equal [string length $data] [r mem.write k1 1 $data]
  91. assert_equal $data [r mem.read k1 1]
  92. set data k2
  93. assert_equal 2 [r mem.alloc k2 2]
  94. assert_equal [string length $data] [r mem.write k2 0 $data]
  95. assert_equal $data [r mem.read k2 0]
  96. r select 1
  97. set data k3
  98. assert_equal 3 [r mem.alloc k3 3]
  99. assert_equal [string length $data] [r mem.write k3 1 $data]
  100. set data k4
  101. assert_equal 2 [r mem.alloc k4 2]
  102. assert_equal [string length $data] [r mem.write k4 0 $data]
  103. assert_equal {total 5 used 2} [r mem.usage 0]
  104. assert_equal {total 5 used 2} [r mem.usage 1]
  105. r select 0
  106. assert_equal 1 [r copy k1 k3]
  107. assert_equal k1 [r mem.read k3 1]
  108. assert_equal {total 8 used 3} [r mem.usage 0]
  109. assert_equal 1 [r copy k2 k1 db 1]
  110. r select 1
  111. assert_equal k2 [r mem.read k1 0]
  112. assert_equal {total 8 used 3} [r mem.usage 0]
  113. assert_equal {total 7 used 3} [r mem.usage 1]
  114. }
  115. test "datatype2: test swapdb" {
  116. r flushall
  117. r select 0
  118. set data k1
  119. assert_equal 5 [r mem.alloc k1 5]
  120. assert_equal [string length $data] [r mem.write k1 1 $data]
  121. assert_equal $data [r mem.read k1 1]
  122. set data k2
  123. assert_equal 4 [r mem.alloc k2 4]
  124. assert_equal [string length $data] [r mem.write k2 0 $data]
  125. assert_equal $data [r mem.read k2 0]
  126. r select 1
  127. set data k1
  128. assert_equal 3 [r mem.alloc k3 3]
  129. assert_equal [string length $data] [r mem.write k3 1 $data]
  130. set data k2
  131. assert_equal 2 [r mem.alloc k4 2]
  132. assert_equal [string length $data] [r mem.write k4 0 $data]
  133. assert_equal {total 9 used 2} [r mem.usage 0]
  134. assert_equal {total 5 used 2} [r mem.usage 1]
  135. assert_equal OK [r swapdb 0 1]
  136. assert_equal {total 9 used 2} [r mem.usage 1]
  137. assert_equal {total 5 used 2} [r mem.usage 0]
  138. }
  139. test "datatype2: test digest" {
  140. r flushall
  141. r select 0
  142. set data k1
  143. assert_equal 3 [r mem.alloc k1 3]
  144. assert_equal [string length $data] [r mem.write k1 1 $data]
  145. assert_equal $data [r mem.read k1 1]
  146. set data k2
  147. assert_equal 2 [r mem.alloc k2 2]
  148. assert_equal [string length $data] [r mem.write k2 0 $data]
  149. assert_equal $data [r mem.read k2 0]
  150. r select 1
  151. set data k1
  152. assert_equal 3 [r mem.alloc k1 3]
  153. assert_equal [string length $data] [r mem.write k1 1 $data]
  154. assert_equal $data [r mem.read k1 1]
  155. set data k2
  156. assert_equal 2 [r mem.alloc k2 2]
  157. assert_equal [string length $data] [r mem.write k2 0 $data]
  158. assert_equal $data [r mem.read k2 0]
  159. r select 0
  160. set digest0 [r debug digest]
  161. r select 1
  162. set digest1 [r debug digest]
  163. assert_equal $digest0 $digest1
  164. }
  165. test "datatype2: test memusage" {
  166. r flushall
  167. set data k1
  168. assert_equal 3 [r mem.alloc k1 3]
  169. assert_equal [string length $data] [r mem.write k1 1 $data]
  170. assert_equal $data [r mem.read k1 1]
  171. set data k2
  172. assert_equal 3 [r mem.alloc k2 3]
  173. assert_equal [string length $data] [r mem.write k2 0 $data]
  174. assert_equal $data [r mem.read k2 0]
  175. assert_equal [r memory usage k1] [r memory usage k2]
  176. }
  177. }