pre_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/robfig/cron/v3"
  5. "strconv"
  6. "testing"
  7. "time"
  8. )
  9. func TestMonth(T *testing.T) {
  10. // 创建2023年12月31日的时间对象
  11. date := time.Date(2023, time.August, 30, 0, 0, 0, 0, time.UTC)
  12. next := date.AddDate(0, -1, -1)
  13. month := int(next.Month())
  14. monthStr := strconv.Itoa(month)
  15. year := next.Year()
  16. yearStr := strconv.Itoa(year)
  17. //预处理索引名称
  18. PreBiddingIndex := "bidding_" + yearStr + monthStr
  19. fmt.Sprintf(PreBiddingIndex)
  20. }
  21. func TestCronExpression(t *testing.T) {
  22. // 模拟指定时间
  23. //testTime := time.Date(2022, time.January, 31, 23, 59, 59, 0, time.UTC)
  24. //c := cron.New(cron.WithLocation(testTime.Local().Location()))
  25. local, _ := time.LoadLocation("Asia/Shanghai")
  26. c := cron.New(cron.WithLocation(local), cron.WithSeconds())
  27. // 定义一个任务函数
  28. task := func() {
  29. fmt.Println("222222")
  30. }
  31. // 创建一个cron表达式
  32. spec := "* * * * * *"
  33. //spec := "59 59 23 * * *"
  34. // 将任务和cron表达式添加到调度器中,并使用测试时间
  35. _, err := c.AddFunc(spec, task)
  36. if err != nil {
  37. t.Fatalf("添加定时任务失败:%v", err)
  38. }
  39. // 启动调度器
  40. c.Start()
  41. defer c.Stop()
  42. select {}
  43. }
  44. func TestDeleteIndex(t *testing.T) {
  45. data := map[string]string{
  46. "aa": "aaa",
  47. "bb": "bbb",
  48. "cc": "ccc",
  49. }
  50. for _, v := range data {
  51. if v == "aaa" {
  52. return
  53. }
  54. fmt.Println(v)
  55. }
  56. }