server_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  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 server
  15. import (
  16. "fmt"
  17. "testing"
  18. "time"
  19. "github.com/stretchr/testify/require"
  20. "github.com/lf-edge/ekuiper/internal/topo/rule"
  21. "github.com/lf-edge/ekuiper/pkg/api"
  22. "github.com/lf-edge/ekuiper/pkg/cast"
  23. )
  24. func TestHandleScheduleRule(t *testing.T) {
  25. defer func() {
  26. cast.SetTimeZone(cast.GetConfiguredTimeZone().String())
  27. }()
  28. err := cast.SetTimeZone("UTC")
  29. require.NoError(t, err)
  30. now, err := time.Parse("2006-01-02 15:04:05", "2006-01-02 15:04:05")
  31. require.NoError(t, err)
  32. now = now.In(cast.GetConfiguredTimeZone())
  33. testcases := []struct {
  34. state string
  35. begin string
  36. end string
  37. action scheduleRuleAction
  38. }{
  39. {
  40. state: "Running",
  41. begin: "2006-01-02 15:04:01",
  42. end: "2006-01-02 15:04:06",
  43. action: scheduleRuleActionDoNothing,
  44. },
  45. {
  46. state: rule.RuleWait,
  47. begin: "2006-01-02 15:04:01",
  48. end: "2006-01-02 15:04:06",
  49. action: scheduleRuleActionStart,
  50. },
  51. {
  52. state: rule.RuleTerminated,
  53. begin: "2006-01-02 15:04:01",
  54. end: "2006-01-02 15:04:04",
  55. action: scheduleRuleActionDoNothing,
  56. },
  57. {
  58. state: rule.RuleStarted,
  59. begin: "2006-01-02 15:04:01",
  60. end: "2006-01-02 15:04:04",
  61. action: scheduleRuleActionStop,
  62. },
  63. }
  64. for i, tc := range testcases {
  65. r := &api.Rule{
  66. Triggered: true,
  67. Options: &api.RuleOption{
  68. Cron: "",
  69. Duration: "",
  70. CronDatetimeRange: []api.DatetimeRange{
  71. {
  72. Begin: tc.begin,
  73. End: tc.end,
  74. },
  75. },
  76. },
  77. }
  78. scheduleRuleSignal := handleScheduleRule(now, r, tc.state)
  79. require.Equal(t, tc.action, scheduleRuleSignal, fmt.Sprintf("case %v", i))
  80. }
  81. }
  82. func TestRunScheduleRuleChecker(t *testing.T) {
  83. exit := make(chan struct{})
  84. go runScheduleRuleCheckerByInterval(3*time.Second, exit)
  85. time.Sleep(1 * time.Second)
  86. exit <- struct{}{}
  87. }
  88. func TestHandleScheduleRuleState(t *testing.T) {
  89. defer func() {
  90. cast.SetTimeZone(cast.GetConfiguredTimeZone().String())
  91. }()
  92. err := cast.SetTimeZone("UTC")
  93. require.NoError(t, err)
  94. r := &api.Rule{}
  95. r.Options = &api.RuleOption{}
  96. now, err := time.Parse("2006-01-02 15:04:05", "2006-01-02 15:04:05")
  97. require.NoError(t, err)
  98. require.NoError(t, handleScheduleRuleState(now, r, rule.RuleStarted))
  99. require.NoError(t, handleScheduleRuleState(now, r, rule.RuleWait))
  100. r.Options.CronDatetimeRange = []api.DatetimeRange{
  101. {
  102. Begin: "2006-01-02 15:04:01",
  103. End: "2006-01-02 15:04:06",
  104. },
  105. }
  106. require.NoError(t, handleScheduleRuleState(now, r, rule.RuleStarted))
  107. require.NoError(t, handleScheduleRuleState(now, r, rule.RuleWait))
  108. r.Options.CronDatetimeRange = []api.DatetimeRange{
  109. {
  110. Begin: "2006-01-02 15:04:01",
  111. End: "2006-01-02 15:04:02",
  112. },
  113. }
  114. require.NoError(t, handleScheduleRuleState(now, r, rule.RuleStarted))
  115. require.NoError(t, handleScheduleRuleState(now, r, rule.RuleWait))
  116. }