您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

history_test.go 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package history
  4. import (
  5. "reflect"
  6. "strconv"
  7. "testing"
  8. "time"
  9. )
  10. const (
  11. timeFormat = "2006-01-02 15:04:05Z"
  12. )
  13. func betweenTimestamps(buf *Buffer, start, end time.Time, limit int) (result []Item, complete bool) {
  14. result, complete, _ = buf.betweenHelper(Selector{Time: start}, Selector{Time: end}, time.Time{}, nil, limit)
  15. return
  16. }
  17. func TestEmptyBuffer(t *testing.T) {
  18. pastTime := easyParse(timeFormat)
  19. buf := NewHistoryBuffer(0, 0)
  20. buf.Add(Item{
  21. Nick: "testnick",
  22. })
  23. since, complete := betweenTimestamps(buf, pastTime, time.Now(), 0)
  24. if len(since) != 0 {
  25. t.Error("shouldn't be able to add to disabled buf")
  26. }
  27. if complete {
  28. t.Error("the empty/disabled buffer should report results as incomplete")
  29. }
  30. buf.Resize(1, 0)
  31. since, complete = betweenTimestamps(buf, pastTime, time.Now(), 0)
  32. assertEqual(complete, true, t)
  33. assertEqual(len(since), 0, t)
  34. buf.Add(Item{
  35. Nick: "testnick",
  36. })
  37. since, complete = betweenTimestamps(buf, pastTime, time.Now(), 0)
  38. if len(since) != 1 {
  39. t.Error("should be able to store items in a nonempty buffer")
  40. }
  41. if !complete {
  42. t.Error("results should be complete")
  43. }
  44. if since[0].Nick != "testnick" {
  45. t.Error("retrived junk data")
  46. }
  47. buf.Add(Item{
  48. Nick: "testnick2",
  49. })
  50. since, complete = betweenTimestamps(buf, pastTime, time.Now(), 0)
  51. if len(since) != 1 {
  52. t.Error("expect exactly 1 item")
  53. }
  54. if complete {
  55. t.Error("results must be marked incomplete")
  56. }
  57. if since[0].Nick != "testnick2" {
  58. t.Error("retrieved junk data")
  59. }
  60. assertEqual(toNicks(buf.latest(0)), []string{"testnick2"}, t)
  61. }
  62. func toNicks(items []Item) (result []string) {
  63. result = make([]string, len(items))
  64. for i, item := range items {
  65. result[i] = item.Nick
  66. }
  67. return
  68. }
  69. func easyParse(timestamp string) time.Time {
  70. result, err := time.Parse(timeFormat, timestamp)
  71. if err != nil {
  72. panic(err)
  73. }
  74. return result
  75. }
  76. func easyItem(nick string, timestamp string) (result Item) {
  77. result.Message.Time = easyParse(timestamp)
  78. result.Nick = nick
  79. return
  80. }
  81. func assertEqual(supplied, expected interface{}, t *testing.T) {
  82. if !reflect.DeepEqual(supplied, expected) {
  83. t.Errorf("expected %v but got %v", expected, supplied)
  84. }
  85. }
  86. func TestBuffer(t *testing.T) {
  87. start := easyParse("2006-01-01 00:00:00Z")
  88. buf := NewHistoryBuffer(3, 0)
  89. buf.Add(easyItem("testnick0", "2006-01-01 15:04:05Z"))
  90. buf.Add(easyItem("testnick1", "2006-01-02 15:04:05Z"))
  91. buf.Add(easyItem("testnick2", "2006-01-03 15:04:05Z"))
  92. since, complete := betweenTimestamps(buf, start, time.Now(), 0)
  93. assertEqual(complete, true, t)
  94. assertEqual(toNicks(since), []string{"testnick0", "testnick1", "testnick2"}, t)
  95. // add another item, evicting the first
  96. buf.Add(easyItem("testnick3", "2006-01-04 15:04:05Z"))
  97. since, complete = betweenTimestamps(buf, start, time.Now(), 0)
  98. assertEqual(complete, false, t)
  99. assertEqual(toNicks(since), []string{"testnick1", "testnick2", "testnick3"}, t)
  100. // now exclude the time of the discarded entry; results should be complete again
  101. since, complete = betweenTimestamps(buf, easyParse("2006-01-02 00:00:00Z"), time.Now(), 0)
  102. assertEqual(complete, true, t)
  103. assertEqual(toNicks(since), []string{"testnick1", "testnick2", "testnick3"}, t)
  104. since, complete = betweenTimestamps(buf, easyParse("2006-01-02 00:00:00Z"), easyParse("2006-01-03 00:00:00Z"), 0)
  105. assertEqual(complete, true, t)
  106. assertEqual(toNicks(since), []string{"testnick1"}, t)
  107. // shrink the buffer, cutting off testnick1
  108. buf.Resize(2, 0)
  109. since, complete = betweenTimestamps(buf, easyParse("2006-01-02 00:00:00Z"), time.Now(), 0)
  110. assertEqual(complete, false, t)
  111. assertEqual(toNicks(since), []string{"testnick2", "testnick3"}, t)
  112. buf.Resize(5, 0)
  113. buf.Add(easyItem("testnick4", "2006-01-05 15:04:05Z"))
  114. buf.Add(easyItem("testnick5", "2006-01-06 15:04:05Z"))
  115. buf.Add(easyItem("testnick6", "2006-01-07 15:04:05Z"))
  116. since, complete = betweenTimestamps(buf, easyParse("2006-01-03 00:00:00Z"), time.Now(), 0)
  117. assertEqual(complete, true, t)
  118. assertEqual(toNicks(since), []string{"testnick2", "testnick3", "testnick4", "testnick5", "testnick6"}, t)
  119. // test ascending order
  120. since, _ = betweenTimestamps(buf, easyParse("2006-01-03 00:00:00Z"), time.Time{}, 2)
  121. assertEqual(toNicks(since), []string{"testnick2", "testnick3"}, t)
  122. }
  123. func autoItem(id int, t time.Time) (result Item) {
  124. result.Message.Time = t
  125. result.Nick = strconv.Itoa(id)
  126. result.Message.Msgid = result.Nick
  127. return
  128. }
  129. func atoi(s string) int {
  130. result, err := strconv.Atoi(s)
  131. if err != nil {
  132. panic(err)
  133. }
  134. return result
  135. }
  136. func TestAutoresize(t *testing.T) {
  137. now := easyParse("2006-01-01 00:00:00Z")
  138. nowFunc := func() time.Time {
  139. return now
  140. }
  141. buf := NewHistoryBuffer(128, time.Hour)
  142. buf.nowFunc = nowFunc
  143. // add items slowly (one every 10 minutes): the buffer should not expand
  144. // beyond initialAutoSize
  145. id := 0
  146. for i := 0; i < 72; i += 1 {
  147. buf.Add(autoItem(id, now))
  148. if initialAutoSize < buf.length() {
  149. t.Errorf("buffer incorrectly resized above %d to %d", initialAutoSize, buf.length())
  150. }
  151. now = now.Add(time.Minute * 10)
  152. id += 1
  153. }
  154. items := buf.latest(0)
  155. assertEqual(len(items), initialAutoSize, t)
  156. assertEqual(atoi(items[0].Nick), 40, t)
  157. assertEqual(atoi(items[len(items)-1].Nick), 71, t)
  158. // dump 100 items in very fast:
  159. for i := 0; i < 100; i += 1 {
  160. buf.Add(autoItem(id, now))
  161. now = now.Add(time.Second)
  162. id += 1
  163. }
  164. // ok, 5 items from the first batch are still in the 1-hour window;
  165. // we should overwrite until only those 5 are left, then start expanding
  166. // the buffer so that it retains those 5 and the 100 new items
  167. items = buf.latest(0)
  168. assertEqual(len(items), 105, t)
  169. assertEqual(atoi(items[0].Nick), 67, t)
  170. assertEqual(atoi(items[len(items)-1].Nick), 171, t)
  171. // another 100 items very fast:
  172. for i := 0; i < 100; i += 1 {
  173. buf.Add(autoItem(id, now))
  174. now = now.Add(time.Second)
  175. id += 1
  176. }
  177. // should fill up to the maximum size of 128 and start overwriting
  178. items = buf.latest(0)
  179. assertEqual(len(items), 128, t)
  180. assertEqual(atoi(items[0].Nick), 144, t)
  181. assertEqual(atoi(items[len(items)-1].Nick), 271, t)
  182. }
  183. // regression test for #702
  184. func TestEnabledByResize(t *testing.T) {
  185. now := easyParse("2006-01-01 00:00:00Z")
  186. // empty/disabled autoresizing buffer
  187. buf := NewHistoryBuffer(0, time.Hour)
  188. // enable the buffer as during a rehash
  189. buf.Resize(128, time.Hour)
  190. // add an item and test that it is stored and retrievable
  191. buf.Add(autoItem(0, now))
  192. items := buf.latest(0)
  193. assertEqual(len(items), 1, t)
  194. assertEqual(atoi(items[0].Nick), 0, t)
  195. }
  196. func TestDisabledByResize(t *testing.T) {
  197. now := easyParse("2006-01-01 00:00:00Z")
  198. // enabled autoresizing buffer
  199. buf := NewHistoryBuffer(128, time.Hour)
  200. buf.Add(autoItem(0, now))
  201. items := buf.latest(0)
  202. assertEqual(len(items), 1, t)
  203. assertEqual(atoi(items[0].Nick), 0, t)
  204. // disable as during a rehash, confirm that nothing can be retrieved
  205. buf.Resize(0, time.Hour)
  206. items = buf.latest(0)
  207. assertEqual(len(items), 0, t)
  208. }
  209. func BenchmarkInsert(b *testing.B) {
  210. buf := NewHistoryBuffer(1024, 0)
  211. b.ResetTimer()
  212. for i := 0; i < b.N; i++ {
  213. buf.Add(Item{})
  214. }
  215. }
  216. func BenchmarkMatch(b *testing.B) {
  217. buf := NewHistoryBuffer(1024, 0)
  218. var now time.Time
  219. for i := 0; i < 1024; i += 1 {
  220. buf.Add(autoItem(i, now))
  221. now = now.Add(time.Second)
  222. }
  223. b.ResetTimer()
  224. for i := 0; i < b.N; i++ {
  225. buf.lookup("512")
  226. }
  227. }