throttle_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package ircclient
  2. import (
  3. "bytes"
  4. "log"
  5. "os"
  6. "strings"
  7. "testing"
  8. "time"
  9. )
  10. func TestFloodTrack(t *testing.T) {
  11. var Flood FloodTrack
  12. Flood.Init(3, 1)
  13. if Flood.Full() {
  14. t.Error("Expected Flood to be empty")
  15. }
  16. for x := 1; x < 3; x++ {
  17. Flood.Save()
  18. if Flood.Pos != x {
  19. t.Errorf("Expected Flood Pos to be %d", x)
  20. }
  21. if Flood.Full() {
  22. t.Error("Expected Flood to be empty")
  23. }
  24. }
  25. Flood.Save()
  26. if Flood.Pos != 3 {
  27. t.Error("Expected Flood Pos to be 3")
  28. }
  29. if !Flood.Full() {
  30. t.Error("Expected Flood to be full")
  31. }
  32. time.Sleep(time.Second)
  33. Flood.Expire()
  34. if Flood.Pos != 0 {
  35. t.Error("Expected Flood Pos to be 0")
  36. }
  37. }
  38. func TestThrottleOnly(t *testing.T) {
  39. // eat log output
  40. var logbuffer bytes.Buffer
  41. log.SetOutput(&logbuffer)
  42. defer func() {
  43. log.SetOutput(os.Stderr)
  44. }()
  45. var buff ThrottleBuffer
  46. buff.Init()
  47. buff.push("#chat", "msg1")
  48. if !buff.Life_sucks {
  49. t.Error("Flood control should be enabled here.")
  50. }
  51. buff.push("#chat", "msg2")
  52. buff.push("#chat", "msg3")
  53. if !buff.Life_sucks {
  54. t.Error("Flood control should be enabled here.")
  55. }
  56. buff.delete("#chat")
  57. if buff.Life_sucks {
  58. t.Error("Flood control should not be enabled here.")
  59. }
  60. }
  61. func TestThrottleDelete(t *testing.T) {
  62. // eat log output
  63. var logbuff bytes.Buffer
  64. log.SetOutput(&logbuff)
  65. defer func() {
  66. log.SetOutput(os.Stderr)
  67. }()
  68. var expect, line string
  69. var buff ThrottleBuffer
  70. buff.Init()
  71. buff.push("#chat", "msg1")
  72. buff.push("#chat", "msg2")
  73. if !buff.Life_sucks {
  74. t.Error("Flood control should be enabled here.")
  75. }
  76. buff.push("#test", "test1")
  77. buff.push("#test", "test2")
  78. buff.push("#another", "msg3")
  79. if !buff.Life_sucks {
  80. t.Error("Flood control should be enabled here.")
  81. }
  82. expect = "#chat,#test,#another"
  83. line = strings.Join(buff.targets, ",")
  84. if line != expect {
  85. t.Errorf("Got %s, Expected %s", line, expect)
  86. }
  87. if buff.last != 0 {
  88. t.Errorf("Expected last=0, got %d", buff.last)
  89. }
  90. line = buff.pop()
  91. expect = "msg1"
  92. if line != expect {
  93. t.Errorf("Got %s, Expected %s", line, expect)
  94. }
  95. if buff.last != 1 {
  96. t.Errorf("Expected last=1, got %d", buff.last)
  97. }
  98. line = buff.pop()
  99. expect = "test1"
  100. if line != expect {
  101. t.Errorf("Got %s, Expected %s", line, expect)
  102. }
  103. if buff.last != 2 {
  104. t.Errorf("Expected last=2, got %d", buff.last)
  105. }
  106. buff.delete("#chat")
  107. expect = "#test,#another"
  108. line = strings.Join(buff.targets, ",")
  109. if line != expect {
  110. t.Errorf("Got %s, Expected %s", line, expect)
  111. }
  112. if buff.last != 1 {
  113. t.Errorf("Expected last=1, got %d", buff.last)
  114. }
  115. buff.delete("missing")
  116. if buff.last != 1 {
  117. t.Errorf("Expected last=1, got %d", buff.last)
  118. }
  119. }
  120. func TestThrottlePopDelete(t *testing.T) {
  121. // eat log output
  122. var logbuff bytes.Buffer
  123. log.SetOutput(&logbuff)
  124. defer func() {
  125. log.SetOutput(os.Stderr)
  126. }()
  127. var buff ThrottleBuffer
  128. buff.Init()
  129. buff.push("#chat", "m1")
  130. buff.push("#chat", "m2")
  131. buff.push("#chat", "m3")
  132. buff.push("#test", "t1")
  133. buff.push("#other", "o1")
  134. buff.push("#other", "o2")
  135. buff.push("#other", "o3")
  136. // When we pop from #test, it needs to be deleted.
  137. // Because it has just one item.
  138. var line, expect string
  139. expect = "#chat,#test,#other"
  140. line = strings.Join(buff.targets, ",")
  141. if line != expect {
  142. t.Errorf("Got %s, Expected %s", line, expect)
  143. }
  144. if buff.last != 0 {
  145. t.Errorf("Expected last=0, got %d", buff.last)
  146. }
  147. line = buff.pop()
  148. expect = "m1"
  149. if line != expect {
  150. t.Errorf("Got %s, Expected %s", line, expect)
  151. }
  152. if buff.last != 1 {
  153. t.Errorf("Expected last=1, got %d", buff.last)
  154. }
  155. line = buff.pop()
  156. expect = "t1"
  157. if line != expect {
  158. t.Errorf("Got %s, Expected %s", line, expect)
  159. }
  160. expect = "#chat,#other"
  161. line = strings.Join(buff.targets, ",")
  162. if line != expect {
  163. t.Errorf("Got %s, Expected %s", line, expect)
  164. }
  165. if buff.last != 1 {
  166. t.Errorf("Expected last=1, got %d", buff.last)
  167. }
  168. line = buff.pop()
  169. expect = "o1"
  170. if line != expect {
  171. t.Errorf("Got %s, Expected %s", line, expect)
  172. }
  173. if buff.last != 0 {
  174. t.Errorf("Expected last=0, got %d", buff.last)
  175. }
  176. }
  177. func TestThrottleBuffer(t *testing.T) {
  178. // eat log output
  179. var logbuff bytes.Buffer
  180. log.SetOutput(&logbuff)
  181. defer func() {
  182. log.SetOutput(os.Stderr)
  183. }()
  184. var buff ThrottleBuffer
  185. buff.Init()
  186. buff.push("#chat", "msg1")
  187. if !buff.Life_sucks {
  188. t.Error("Flood control should be enabled here.")
  189. }
  190. buff.push("#chat", "msg2")
  191. buff.push("user", "msg3")
  192. // verify output order
  193. // #chat [0] "msg1"
  194. // user [0] "msg3"
  195. // #chat [1] "msg2"
  196. var str string
  197. var expect string
  198. for _, expect = range []string{"msg1", "msg3", "msg2"} {
  199. str = buff.pop()
  200. if str != expect {
  201. t.Errorf("Expected %s, got %s", expect, str)
  202. }
  203. }
  204. if buff.Life_sucks {
  205. t.Error("Flood control should not be enabled here.")
  206. }
  207. // verify deleting 1st item works
  208. buff.push("#chat", "msg1")
  209. if !buff.Life_sucks {
  210. t.Error("Flood control should be enabled here.")
  211. }
  212. buff.push("#chat", "msg2")
  213. buff.push("user", "msg3")
  214. buff.delete("#chat")
  215. str = buff.pop()
  216. expect = "msg3"
  217. if str != expect {
  218. t.Errorf("Expected %s, got %s", expect, str)
  219. }
  220. if buff.Life_sucks {
  221. t.Error("Flood control should not be enabled here.")
  222. }
  223. // verify deleting 2nd item works
  224. buff.push("#chat", "txt1")
  225. buff.push("user", "txt2")
  226. buff.delete("user")
  227. str = buff.pop()
  228. expect = "txt1"
  229. if str != expect {
  230. t.Errorf("Expected %s, got %s", expect, str)
  231. }
  232. if buff.Life_sucks {
  233. t.Error("Flood control should not be enabled here.")
  234. }
  235. }