pod_backoff_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "testing"
  16. "time"
  17. ktypes "k8s.io/apimachinery/pkg/types"
  18. )
  19. func TestBackoffPod(t *testing.T) {
  20. bpm := NewPodBackoffMap(1*time.Second, 10*time.Second)
  21. tests := []struct {
  22. podID ktypes.NamespacedName
  23. expectedDuration time.Duration
  24. advanceClock time.Duration
  25. }{
  26. {
  27. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  28. expectedDuration: 1 * time.Second,
  29. },
  30. {
  31. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  32. expectedDuration: 2 * time.Second,
  33. },
  34. {
  35. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  36. expectedDuration: 4 * time.Second,
  37. },
  38. {
  39. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  40. expectedDuration: 8 * time.Second,
  41. },
  42. {
  43. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  44. expectedDuration: 10 * time.Second,
  45. },
  46. {
  47. podID: ktypes.NamespacedName{Namespace: "default", Name: "foo"},
  48. expectedDuration: 10 * time.Second,
  49. },
  50. {
  51. podID: ktypes.NamespacedName{Namespace: "default", Name: "bar"},
  52. expectedDuration: 1 * time.Second,
  53. },
  54. }
  55. for _, test := range tests {
  56. // Backoff the pod
  57. bpm.BackoffPod(test.podID)
  58. // Get backoff duration for the pod
  59. duration := bpm.calculateBackoffDuration(test.podID)
  60. if duration != test.expectedDuration {
  61. t.Errorf("expected: %s, got %s for pod %s", test.expectedDuration.String(), duration.String(), test.podID)
  62. }
  63. }
  64. }
  65. func TestClearPodBackoff(t *testing.T) {
  66. bpm := NewPodBackoffMap(1*time.Second, 60*time.Second)
  67. // Clear backoff on an not existed pod
  68. bpm.clearPodBackoff(ktypes.NamespacedName{Namespace: "ns", Name: "not-existed"})
  69. // Backoff twice for pod foo
  70. podID := ktypes.NamespacedName{Namespace: "ns", Name: "foo"}
  71. bpm.BackoffPod(podID)
  72. bpm.BackoffPod(podID)
  73. if duration := bpm.calculateBackoffDuration(podID); duration != 2*time.Second {
  74. t.Errorf("Expected backoff of 1s for pod %s, got %s", podID, duration.String())
  75. }
  76. // Clear backoff for pod foo
  77. bpm.clearPodBackoff(podID)
  78. // Backoff once for pod foo
  79. bpm.BackoffPod(podID)
  80. if duration := bpm.calculateBackoffDuration(podID); duration != 1*time.Second {
  81. t.Errorf("Expected backoff of 1s for pod %s, got %s", podID, duration.String())
  82. }
  83. }