Files
base/tools/cron/job_test.go
zeekay 6459bb6eda refactor(cron): route every scheduled-job path through tools/tasks
tools/cron is now a thin alias over tools/tasks. The legacy ticker +
crontab-matching implementation is deleted; the public surface (New,
Add/MustAdd, Remove, RemoveAll, Jobs, HasJob, Total, Start, Stop,
SetInterval, SetTimezone, HasStarted) is preserved so no callsite
changes. app.Cron() and app.Tasks() share a single *tasks.Client, giving
one obvious way to schedule work.

- tools/tasks/client.go gains the primitives the cron shim needs
  (Remove, RemoveAll, HasJob, Total, Schedules, Run, PauseAll, ResumeAll,
  ActiveTickerCount) plus a richer internal scheduleEntry so remote vs
  local ticking are distinguishable.
- cron.Cron.Stop() now pauses local tickers while preserving the
  registry, matching the legacy semantics expected by ResetBootstrapState
  and the admin API.
- core/base.go builds app.cron via cron.NewFromTasks(app.tasks) so both
  accessors feed the same state.
- apis/cron.go keeps the /crons list + run-now behaviour; lookup now
  uses HasJob() for an O(1) precheck.

Tests rewritten around the shim (TestCronDelegatesToTasks,
TestCronAppFacadeLocal, TestCronAppFacadeRemoteFallback,
TestCronNewFromTasksShared). Legacy 1-minute-tick assertions
(TestCronStartStop) are SKIPped with a pointer to their replacement;
crontab parser tests (tools/cron/schedule_test.go) still pass — the
parser stays as a validation primitive for settings_model.go.

docs/cron-to-tasks.md covers the migration path for services.
2026-04-22 15:14:45 -07:00

60 lines
1.2 KiB
Go

package cron
import (
"encoding/json"
"testing"
)
func TestJobId(t *testing.T) {
j := Job{id: "test"}
if j.Id() != "test" {
t.Fatalf("expected id=test, got %q", j.Id())
}
}
func TestJobExpression(t *testing.T) {
j := Job{expression: "1 2 3 4 5"}
if j.Expression() != "1 2 3 4 5" {
t.Fatalf("expected expression=1 2 3 4 5, got %q", j.Expression())
}
}
func TestJobRunNoClient(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Errorf("Run on zero-value Job must not panic: %v", r)
}
}()
(&Job{}).Run()
}
func TestJobRunThroughClient(t *testing.T) {
c := New()
defer c.Stop()
calls := 0
if err := c.Add("probe", "24h", func() { calls++ }); err != nil {
t.Fatal(err)
}
jobs := c.Jobs()
if len(jobs) != 1 {
t.Fatalf("expected 1 job, got %d", len(jobs))
}
jobs[0].Run()
if calls != 1 {
t.Fatalf("expected Run to invoke callback once, got %d", calls)
}
}
func TestJobMarshalJSON(t *testing.T) {
j := Job{id: "test_id", expression: "1 2 3 4 5"}
raw, err := json.Marshal(j)
if err != nil {
t.Fatal(err)
}
want := `{"id":"test_id","expression":"1 2 3 4 5"}`
if string(raw) != want {
t.Fatalf("expected %s, got %s", want, string(raw))
}
}