batcher_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package rafthttp
  15. import (
  16. "testing"
  17. "time"
  18. )
  19. func TestBatcherNum(t *testing.T) {
  20. n := 100
  21. largeD := time.Minute
  22. tests := []struct {
  23. n int
  24. wnotbatch int
  25. }{
  26. {n - 1, 0},
  27. {n, 1},
  28. {n + 1, 1},
  29. {2*n + 1, 2},
  30. {3*n + 1, 3},
  31. }
  32. for i, tt := range tests {
  33. b := NewBatcher(n, largeD)
  34. notbatched := 0
  35. for j := 0; j < tt.n; j++ {
  36. if !b.ShouldBatch(time.Now()) {
  37. notbatched++
  38. }
  39. }
  40. if notbatched != tt.wnotbatch {
  41. t.Errorf("#%d: notbatched = %d, want %d", i, notbatched, tt.wnotbatch)
  42. }
  43. }
  44. }
  45. func TestBatcherTime(t *testing.T) {
  46. largeN := 10000
  47. tests := []struct {
  48. nms int
  49. wnotbatch int
  50. }{
  51. {0, 0},
  52. {1, 1},
  53. {2, 2},
  54. {3, 3},
  55. }
  56. for i, tt := range tests {
  57. b := NewBatcher(largeN, time.Millisecond)
  58. baseT := b.batchedT
  59. notbatched := 0
  60. for j := 0; j < tt.nms+1; j++ {
  61. if !b.ShouldBatch(baseT.Add(time.Duration(j) * time.Millisecond)) {
  62. notbatched++
  63. }
  64. }
  65. if notbatched != tt.wnotbatch {
  66. t.Errorf("#%d: notbatched = %d, want %d", i, notbatched, tt.wnotbatch)
  67. }
  68. }
  69. }