batcher_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package rafthttp
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestBatcherNum(t *testing.T) {
  7. n := 100
  8. largeD := time.Minute
  9. tests := []struct {
  10. n int
  11. wnotbatch int
  12. }{
  13. {n - 1, 0},
  14. {n, 1},
  15. {n + 1, 1},
  16. {2*n + 1, 2},
  17. {3*n + 1, 3},
  18. }
  19. for i, tt := range tests {
  20. b := NewBatcher(n, largeD)
  21. notbatched := 0
  22. for j := 0; j < tt.n; j++ {
  23. if !b.ShouldBatch(time.Now()) {
  24. notbatched++
  25. }
  26. }
  27. if notbatched != tt.wnotbatch {
  28. t.Errorf("#%d: notbatched = %d, want %d", i, notbatched, tt.wnotbatch)
  29. }
  30. }
  31. }
  32. func TestBatcherTime(t *testing.T) {
  33. largeN := 10000
  34. tests := []struct {
  35. nms int
  36. wnotbatch int
  37. }{
  38. {0, 0},
  39. {1, 1},
  40. {2, 2},
  41. {3, 3},
  42. }
  43. for i, tt := range tests {
  44. b := NewBatcher(largeN, time.Millisecond)
  45. baseT := b.batchedT
  46. notbatched := 0
  47. for j := 0; j < tt.nms+1; j++ {
  48. if !b.ShouldBatch(baseT.Add(time.Duration(j) * time.Millisecond)) {
  49. notbatched++
  50. }
  51. }
  52. if notbatched != tt.wnotbatch {
  53. t.Errorf("#%d: notbatched = %d, want %d", i, notbatched, tt.wnotbatch)
  54. }
  55. }
  56. }