-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(crontab/v2): Refactor v2 version (#95)
* refactor(crontab/v2): Refactor v2 version Signed-off-by: Flc゛ <[email protected]> * refactor(crontab/v2): Refactor v2 version Signed-off-by: Flc゛ <[email protected]> --------- Signed-off-by: Flc゛ <[email protected]>
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package v2 //nolint:typecheck | ||
|
||
type Job interface { | ||
Exp() string // Expression | ||
Run() // Handler | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) //nolint:errcheck | ||
defer srv.Stop(ctx) //nolint:errcheck | ||
|
||
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) | ||
} | ||
} |