pod_backoff_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package queue
  14. import (
  15. "fmt"
  16. "testing"
  17. "time"
  18. ktypes "k8s.io/apimachinery/pkg/types"
  19. "k8s.io/apimachinery/pkg/util/clock"
  20. )
  21. func TestBackoffPod(t *testing.T) {
  22. timestamp := time.Now()
  23. bpm := NewPodBackoffMap(1*time.Second, 10*time.Second, clock.NewFakeClock(timestamp))
  24. tests := []struct {
  25. podID ktypes.NamespacedName
  26. expectedDuration time.Duration
  27. advanceClock time.Duration
  28. }{
  29. {
  30. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  31. expectedDuration: 1 * time.Second,
  32. },
  33. {
  34. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  35. expectedDuration: 2 * time.Second,
  36. },
  37. {
  38. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  39. expectedDuration: 4 * time.Second,
  40. },
  41. {
  42. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  43. expectedDuration: 8 * time.Second,
  44. },
  45. {
  46. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  47. expectedDuration: 10 * time.Second,
  48. },
  49. {
  50. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  51. expectedDuration: 10 * time.Second,
  52. },
  53. {
  54. podID: ktypes.NamespacedName{Namespace: "default", Name: "bar"},
  55. expectedDuration: 1 * time.Second,
  56. },
  57. }
  58. for i, test := range tests {
  59. t.Run(fmt.Sprintf("step %d", i), func(t *testing.T) {
  60. bpm.BackoffPod(test.podID)
  61. backoff, ok := bpm.GetBackoffTime(test.podID)
  62. if !ok {
  63. t.Errorf("%v should be backed off", test.podID)
  64. }
  65. duration := backoff.Sub(timestamp)
  66. if duration != test.expectedDuration {
  67. t.Errorf("expected: %s, got %s for pod %s", test.expectedDuration.String(), duration.String(), test.podID)
  68. }
  69. })
  70. }
  71. }
  72. func TestClearPodBackoff(t *testing.T) {
  73. bpm := NewPodBackoffMap(1*time.Second, 60*time.Second, clock.NewFakeClock(time.Now()))
  74. // Clear backoff on an not existed pod
  75. bpm.clearPodBackoff(ktypes.NamespacedName{Namespace: "ns", Name: "not-existed"})
  76. // Backoff twice for pod foo
  77. podID := ktypes.NamespacedName{Namespace: "ns", Name: "foo"}
  78. bpm.BackoffPod(podID)
  79. bpm.BackoffPod(podID)
  80. if duration := bpm.calculateBackoffDuration(podID); duration != 2*time.Second {
  81. t.Errorf("Expected backoff of 1s for pod %s, got %s", podID, duration.String())
  82. }
  83. // Clear backoff for pod foo
  84. bpm.clearPodBackoff(podID)
  85. // Backoff once for pod foo
  86. bpm.BackoffPod(podID)
  87. if duration := bpm.calculateBackoffDuration(podID); duration != 1*time.Second {
  88. t.Errorf("Expected backoff of 1s for pod %s, got %s", podID, duration.String())
  89. }
  90. }