server_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. "testing"
  17. "time"
  18. "github.com/stretchr/testify/require"
  19. "github.com/lf-edge/ekuiper/pkg/api"
  20. )
  21. func TestHandleScheduleRule(t *testing.T) {
  22. now, err := time.Parse("2006-01-02 15:04:05", "2006-01-02 15:04:05")
  23. require.NoError(t, err)
  24. testcases := []struct {
  25. state string
  26. begin string
  27. end string
  28. toStart bool
  29. toStop bool
  30. }{
  31. {
  32. state: "Running",
  33. begin: "2006-01-02 15:04:01",
  34. end: "2006-01-02 15:04:06",
  35. toStart: false,
  36. toStop: false,
  37. },
  38. {
  39. state: "Stopped",
  40. begin: "2006-01-02 15:04:01",
  41. end: "2006-01-02 15:04:06",
  42. toStart: true,
  43. toStop: false,
  44. },
  45. {
  46. state: "Stopped",
  47. begin: "2006-01-02 15:04:01",
  48. end: "2006-01-02 15:04:04",
  49. toStart: false,
  50. toStop: false,
  51. },
  52. {
  53. state: "Running",
  54. begin: "2006-01-02 15:04:01",
  55. end: "2006-01-02 15:04:04",
  56. toStart: false,
  57. toStop: true,
  58. },
  59. }
  60. for _, tc := range testcases {
  61. r := &api.Rule{
  62. Options: &api.RuleOption{
  63. Cron: "",
  64. Duration: "",
  65. CronDatetimeRange: []api.DatetimeRange{
  66. {
  67. Begin: tc.begin,
  68. End: tc.end,
  69. },
  70. },
  71. },
  72. }
  73. toStart, toStop := handleScheduleRule(now, r, tc.state)
  74. require.Equal(t, tc.toStart, toStart)
  75. require.Equal(t, tc.toStop, toStop)
  76. }
  77. }
  78. func TestRunScheduleRuleChecker(t *testing.T) {
  79. exit := make(chan struct{})
  80. go runScheduleRuleCheckerByInterval(3*time.Second, exit)
  81. time.Sleep(1 * time.Second)
  82. exit <- struct{}{}
  83. }
  84. func TestHandleScheduleRuleState(t *testing.T) {
  85. r := &api.Rule{}
  86. r.Options = &api.RuleOption{}
  87. now, err := time.Parse("2006-01-02 15:04:05", "2006-01-02 15:04:05")
  88. require.NoError(t, err)
  89. require.NoError(t, handleScheduleRuleState(now, r, "Running"))
  90. r.Options.CronDatetimeRange = []api.DatetimeRange{
  91. {
  92. Begin: "2006-01-02 15:04:01",
  93. End: "2006-01-02 15:04:06",
  94. },
  95. }
  96. require.NoError(t, handleScheduleRuleState(now, r, "Running"))
  97. r.Options.CronDatetimeRange = []api.DatetimeRange{
  98. {
  99. Begin: "2006-01-02 15:04:01",
  100. End: "2006-01-02 15:04:02",
  101. },
  102. }
  103. require.NoError(t, handleScheduleRuleState(now, r, "Running"))
  104. }