You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

history_test.go 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 easyItem(nick string, timestamp string) (result Item) {
  79. result.Message.Time = easyParse(timestamp)
  80. result.Nick = nick
  81. return
  82. }
  83. func assertEqual(supplied, expected interface{}, t *testing.T) {
  84. if !reflect.DeepEqual(supplied, expected) {
  85. t.Errorf("expected %v but got %v", expected, supplied)
  86. }
  87. }
  88. func TestBuffer(t *testing.T) {
  89. start := easyParse("2006-01-01 00:00:00Z")
  90. buf := NewHistoryBuffer(3)
  91. buf.Add(easyItem("testnick0", "2006-01-01 15:04:05Z"))
  92. buf.Add(easyItem("testnick1", "2006-01-02 15:04:05Z"))
  93. buf.Add(easyItem("testnick2", "2006-01-03 15:04:05Z"))
  94. since, complete := buf.Between(start, time.Now(), false, 0)
  95. assertEqual(complete, true, t)
  96. assertEqual(toNicks(since), []string{"testnick0", "testnick1", "testnick2"}, t)
  97. // add another item, evicting the first
  98. buf.Add(easyItem("testnick3", "2006-01-04 15:04:05Z"))
  99. since, complete = buf.Between(start, time.Now(), false, 0)
  100. assertEqual(complete, false, t)
  101. assertEqual(toNicks(since), []string{"testnick1", "testnick2", "testnick3"}, t)
  102. // now exclude the time of the discarded entry; results should be complete again
  103. since, complete = buf.Between(easyParse("2006-01-02 00:00:00Z"), time.Now(), false, 0)
  104. assertEqual(complete, true, t)
  105. assertEqual(toNicks(since), []string{"testnick1", "testnick2", "testnick3"}, t)
  106. since, complete = buf.Between(easyParse("2006-01-02 00:00:00Z"), easyParse("2006-01-03 00:00:00Z"), false, 0)
  107. assertEqual(complete, true, t)
  108. assertEqual(toNicks(since), []string{"testnick1"}, t)
  109. // shrink the buffer, cutting off testnick1
  110. buf.Resize(2)
  111. since, complete = buf.Between(easyParse("2006-01-02 00:00:00Z"), time.Now(), false, 0)
  112. assertEqual(complete, false, t)
  113. assertEqual(toNicks(since), []string{"testnick2", "testnick3"}, t)
  114. buf.Resize(5)
  115. buf.Add(easyItem("testnick4", "2006-01-05 15:04:05Z"))
  116. buf.Add(easyItem("testnick5", "2006-01-06 15:04:05Z"))
  117. buf.Add(easyItem("testnick6", "2006-01-07 15:04:05Z"))
  118. since, complete = buf.Between(easyParse("2006-01-03 00:00:00Z"), time.Now(), false, 0)
  119. assertEqual(complete, true, t)
  120. assertEqual(toNicks(since), []string{"testnick2", "testnick3", "testnick4", "testnick5", "testnick6"}, t)
  121. // test ascending order
  122. since, _ = buf.Between(easyParse("2006-01-03 00:00:00Z"), time.Now(), true, 2)
  123. assertEqual(toNicks(since), []string{"testnick2", "testnick3"}, t)
  124. }