Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

history_test.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Copyright (c) 2018 Shivaram Lingamneni <slingamn@cs.stanford.edu>
  2. // released under the MIT license
  3. package history
  4. import (
  5. "reflect"
  6. "testing"
  7. "time"
  8. )
  9. const (
  10. timeFormat = "2006-01-02 15:04:05Z"
  11. )
  12. func TestEmptyBuffer(t *testing.T) {
  13. pastTime := easyParse(timeFormat)
  14. buf := NewHistoryBuffer(0)
  15. if buf.Enabled() {
  16. t.Error("the buffer of size 0 must be considered disabled")
  17. }
  18. buf.Add(Item{
  19. Nick: "testnick",
  20. })
  21. since, complete := buf.Between(pastTime, time.Now(), false, 0)
  22. if len(since) != 0 {
  23. t.Error("shouldn't be able to add to disabled buf")
  24. }
  25. if complete {
  26. t.Error("the empty/disabled buffer should report results as incomplete")
  27. }
  28. buf.Resize(1)
  29. if !buf.Enabled() {
  30. t.Error("the buffer of size 1 must be considered enabled")
  31. }
  32. since, complete = buf.Between(pastTime, time.Now(), false, 0)
  33. assertEqual(complete, true, t)
  34. assertEqual(len(since), 0, t)
  35. buf.Add(Item{
  36. Nick: "testnick",
  37. })
  38. since, complete = buf.Between(pastTime, time.Now(), false, 0)
  39. if len(since) != 1 {
  40. t.Error("should be able to store items in a nonempty buffer")
  41. }
  42. if !complete {
  43. t.Error("results should be complete")
  44. }
  45. if since[0].Nick != "testnick" {
  46. t.Error("retrived junk data")
  47. }
  48. buf.Add(Item{
  49. Nick: "testnick2",
  50. })
  51. since, complete = buf.Between(pastTime, time.Now(), false, 0)
  52. if len(since) != 1 {
  53. t.Error("expect exactly 1 item")
  54. }
  55. if complete {
  56. t.Error("results must be marked incomplete")
  57. }
  58. if since[0].Nick != "testnick2" {
  59. t.Error("retrieved junk data")
  60. }
  61. matchAll := func(item Item) bool { return true }
  62. assertEqual(toNicks(buf.Match(matchAll, false, 0)), []string{"testnick2"}, t)
  63. }
  64. func toNicks(items []Item) (result []string) {
  65. result = make([]string, len(items))
  66. for i, item := range items {
  67. result[i] = item.Nick
  68. }
  69. return
  70. }
  71. func easyParse(timestamp string) time.Time {
  72. result, err := time.Parse(timeFormat, timestamp)
  73. if err != nil {
  74. panic(err)
  75. }
  76. return result
  77. }
  78. func assertEqual(supplied, expected interface{}, t *testing.T) {
  79. if !reflect.DeepEqual(supplied, expected) {
  80. t.Errorf("expected %v but got %v", expected, supplied)
  81. }
  82. }
  83. func TestBuffer(t *testing.T) {
  84. start := easyParse("2006-01-01 00:00:00Z")
  85. buf := NewHistoryBuffer(3)
  86. buf.Add(Item{
  87. Nick: "testnick0",
  88. Time: easyParse("2006-01-01 15:04:05Z"),
  89. })
  90. buf.Add(Item{
  91. Nick: "testnick1",
  92. Time: easyParse("2006-01-02 15:04:05Z"),
  93. })
  94. buf.Add(Item{
  95. Nick: "testnick2",
  96. Time: easyParse("2006-01-03 15:04:05Z"),
  97. })
  98. since, complete := buf.Between(start, time.Now(), false, 0)
  99. assertEqual(complete, true, t)
  100. assertEqual(toNicks(since), []string{"testnick0", "testnick1", "testnick2"}, t)
  101. // add another item, evicting the first
  102. buf.Add(Item{
  103. Nick: "testnick3",
  104. Time: easyParse("2006-01-04 15:04:05Z"),
  105. })
  106. since, complete = buf.Between(start, time.Now(), false, 0)
  107. assertEqual(complete, false, t)
  108. assertEqual(toNicks(since), []string{"testnick1", "testnick2", "testnick3"}, t)
  109. // now exclude the time of the discarded entry; results should be complete again
  110. since, complete = buf.Between(easyParse("2006-01-02 00:00:00Z"), time.Now(), false, 0)
  111. assertEqual(complete, true, t)
  112. assertEqual(toNicks(since), []string{"testnick1", "testnick2", "testnick3"}, t)
  113. since, complete = buf.Between(easyParse("2006-01-02 00:00:00Z"), easyParse("2006-01-03 00:00:00Z"), false, 0)
  114. assertEqual(complete, true, t)
  115. assertEqual(toNicks(since), []string{"testnick1"}, t)
  116. // shrink the buffer, cutting off testnick1
  117. buf.Resize(2)
  118. since, complete = buf.Between(easyParse("2006-01-02 00:00:00Z"), time.Now(), false, 0)
  119. assertEqual(complete, false, t)
  120. assertEqual(toNicks(since), []string{"testnick2", "testnick3"}, t)
  121. buf.Resize(5)
  122. buf.Add(Item{
  123. Nick: "testnick4",
  124. Time: easyParse("2006-01-05 15:04:05Z"),
  125. })
  126. buf.Add(Item{
  127. Nick: "testnick5",
  128. Time: easyParse("2006-01-06 15:04:05Z"),
  129. })
  130. buf.Add(Item{
  131. Nick: "testnick6",
  132. Time: easyParse("2006-01-07 15:04:05Z"),
  133. })
  134. since, complete = buf.Between(easyParse("2006-01-03 00:00:00Z"), time.Now(), false, 0)
  135. assertEqual(complete, true, t)
  136. assertEqual(toNicks(since), []string{"testnick2", "testnick3", "testnick4", "testnick5", "testnick6"}, t)
  137. // test ascending order
  138. since, _ = buf.Between(easyParse("2006-01-03 00:00:00Z"), time.Now(), true, 2)
  139. assertEqual(toNicks(since), []string{"testnick2", "testnick3"}, t)
  140. }