scaling_trigger_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2019 Yunion
  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 models
  15. import (
  16. "testing"
  17. "time"
  18. "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/util/bitmap"
  20. )
  21. func TestSTimer_Update(t *testing.T) {
  22. loc := time.UTC
  23. ins := []struct {
  24. Timer *STimer
  25. UpdateTimes int
  26. }{
  27. {
  28. Timer: &STimer{
  29. Hour: 7,
  30. Minute: 21,
  31. Type: compute.TIMER_TYPE_DAY,
  32. StartTime: time.Date(2020, 4, 9, 7, 18, 8, 0, loc),
  33. NextTime: time.Date(2020, 4, 9, 7, 21, 0, 0, loc),
  34. EndTime: time.Date(2020, 4, 11, 7, 18, 8, 0, loc),
  35. },
  36. UpdateTimes: 2,
  37. },
  38. {
  39. Timer: &STimer{
  40. Hour: 7,
  41. Minute: 21,
  42. WeekDays: uint8(bitmap.IntArray2Uint([]int{1, 5, 7})),
  43. Type: compute.TIMER_TYPE_WEEK,
  44. StartTime: time.Date(2020, 4, 9, 7, 18, 8, 0, loc),
  45. NextTime: time.Date(2020, 4, 9, 7, 21, 0, 0, loc),
  46. EndTime: time.Date(2020, 4, 14, 7, 18, 8, 0, loc),
  47. },
  48. UpdateTimes: 4,
  49. },
  50. {
  51. Timer: &STimer{
  52. Hour: 7,
  53. Minute: 21,
  54. MonthDays: bitmap.IntArray2Uint([]int{1, 10, 30}),
  55. Type: compute.TIMER_TYPE_MONTH,
  56. StartTime: time.Date(2020, 4, 9, 7, 18, 8, 0, loc),
  57. NextTime: time.Date(2020, 4, 9, 7, 21, 0, 0, loc),
  58. EndTime: time.Date(2020, 5, 2, 7, 18, 8, 0, loc),
  59. },
  60. UpdateTimes: 4,
  61. },
  62. }
  63. wants := [][]time.Time{
  64. {
  65. time.Date(2020, 4, 10, 7, 21, 0, 0, loc),
  66. {},
  67. },
  68. {
  69. time.Date(2020, 4, 10, 7, 21, 0, 0, loc),
  70. time.Date(2020, 4, 12, 7, 21, 0, 0, loc),
  71. time.Date(2020, 4, 13, 7, 21, 0, 0, loc),
  72. {},
  73. },
  74. {
  75. time.Date(2020, 4, 10, 7, 21, 0, 0, loc),
  76. time.Date(2020, 4, 30, 7, 21, 0, 0, loc),
  77. time.Date(2020, 5, 1, 7, 21, 0, 0, loc),
  78. {},
  79. },
  80. }
  81. wrapper := func(timer *STimer) time.Time {
  82. fakeNow := timer.NextTime
  83. if !fakeNow.IsZero() {
  84. fakeNow = fakeNow.Add(time.Minute)
  85. }
  86. timer.Update(fakeNow)
  87. if timer.IsExpired {
  88. return time.Time{}
  89. }
  90. return timer.NextTime
  91. }
  92. for i := range ins {
  93. for updateTimes := 0; updateTimes < ins[i].UpdateTimes; updateTimes++ {
  94. out := wrapper(ins[i].Timer)
  95. if out.IsZero() && wants[i][updateTimes].IsZero() {
  96. continue
  97. }
  98. if out.Equal(wants[i][updateTimes]) {
  99. continue
  100. }
  101. t.Fatalf("index: %d, updateTimes: %d, want: %s, real: %s", i, updateTimes, wants[i][updateTimes], out)
  102. }
  103. }
  104. }