From a2dfb148f210c7c3d0acca67facb73d30ee62ba8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Wed, 14 Feb 2024 22:07:35 +0800 Subject: [PATCH 1/2] refactor(crontab/v2): Refactor v2 version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Flc゛ --- crontab/v2/job.go | 6 +++++ crontab/v2/null_jog.go | 1 + crontab/v2/server.go | 27 +++++++++++++++++++ crontab/v2/server_test.go | 55 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 89 insertions(+) create mode 100644 crontab/v2/job.go create mode 100644 crontab/v2/null_jog.go create mode 100644 crontab/v2/server.go create mode 100644 crontab/v2/server_test.go diff --git a/crontab/v2/job.go b/crontab/v2/job.go new file mode 100644 index 00000000..97616d65 --- /dev/null +++ b/crontab/v2/job.go @@ -0,0 +1,6 @@ +package v2 //nolint:typecheck + +type Job interface { + Exp() string // Expression + Run() // Handler +} diff --git a/crontab/v2/null_jog.go b/crontab/v2/null_jog.go new file mode 100644 index 00000000..5ec3cc8e --- /dev/null +++ b/crontab/v2/null_jog.go @@ -0,0 +1 @@ +package v2 diff --git a/crontab/v2/server.go b/crontab/v2/server.go new file mode 100644 index 00000000..60bcfe8a --- /dev/null +++ b/crontab/v2/server.go @@ -0,0 +1,27 @@ +package v2 + +import ( + "context" + + "github.com/robfig/cron/v3" +) + +type Server struct { + *cron.Cron +} + +func NewServer(c *cron.Cron) *Server { + return &Server{ + Cron: c, + } +} + +func (s *Server) Start(context.Context) error { + s.Cron.Run() + return nil +} + +func (s *Server) Stop(context.Context) error { + s.Cron.Stop() + return nil +} diff --git a/crontab/v2/server_test.go b/crontab/v2/server_test.go new file mode 100644 index 00000000..65195c23 --- /dev/null +++ b/crontab/v2/server_test.go @@ -0,0 +1,55 @@ +package v2 + +import ( + "context" + "testing" + "time" + + "github.com/robfig/cron/v3" +) + +type mockJob struct{} + +func RegisterMockJob(srv *Server) { + n := newMockJob() + _, _ = srv.AddJob(n.Exp(), n) +} + +func newMockJob() *mockJob { + return &mockJob{} +} + +func (j *mockJob) Exp() string { + return "* * * * * *" +} + +func (j *mockJob) Run() { + data <- "Hello World!" +} + +var ( + ctx = context.Background() + data = make(chan string, 1) +) + +func TestCrontab(t *testing.T) { + srv := NewServer(cron.New( + cron.WithSeconds(), + )) + + RegisterMockJob(srv) + + go srv.Start(ctx) + defer srv.Stop(ctx) + + ctx, cancel := context.WithTimeout(ctx, time.Second*2) + defer cancel() + + select { + case <-ctx.Done(): + t.Error("Crontab test timeout") + return + case msg := <-data: + t.Log(msg) + } +} From f0862ead30ff460656909910755e61950cc38663 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Flc=E3=82=9B?= Date: Wed, 14 Feb 2024 22:11:04 +0800 Subject: [PATCH 2/2] refactor(crontab/v2): Refactor v2 version MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Flc゛ --- crontab/v2/server_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crontab/v2/server_test.go b/crontab/v2/server_test.go index 65195c23..b0d0adb9 100644 --- a/crontab/v2/server_test.go +++ b/crontab/v2/server_test.go @@ -39,8 +39,8 @@ func TestCrontab(t *testing.T) { RegisterMockJob(srv) - go srv.Start(ctx) - defer srv.Stop(ctx) + go srv.Start(ctx) //nolint:errcheck + defer srv.Stop(ctx) //nolint:errcheck ctx, cancel := context.WithTimeout(ctx, time.Second*2) defer cancel()