From f5375254f90053bd9b688d36f758aca309ec3735 Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Tue, 26 Jul 2016 17:39:51 -0400 Subject: Adding migration support to LDAP from other account types (#3655) --- model/job.go | 6 ++++++ model/job_test.go | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) (limited to 'model') diff --git a/model/job.go b/model/job.go index b6c68dce4..229d5efd3 100644 --- a/model/job.go +++ b/model/job.go @@ -84,6 +84,12 @@ func (task *ScheduledTask) Cancel() { removeTaskByName(task.Name) } +// Executes the task immediatly. A recurring task will be run regularally after interval. +func (task *ScheduledTask) Execute() { + task.function() + task.timer.Reset(task.Interval) +} + func (task *ScheduledTask) String() string { return fmt.Sprintf( "%s\nInterval: %s\nRecurring: %t\n", diff --git a/model/job_test.go b/model/job_test.go index 2a307de1e..8908fed58 100644 --- a/model/job_test.go +++ b/model/job_test.go @@ -126,3 +126,63 @@ func TestGetAllTasks(t *testing.T) { } } } + +func TestExecuteTask(t *testing.T) { + TASK_NAME := "Test Task" + TASK_TIME := time.Second * 5 + + testValue := 0 + testFunc := func() { + testValue += 1 + } + + task := CreateTask(TASK_NAME, testFunc, TASK_TIME) + if testValue != 0 { + t.Fatal("Unexpected execuition of task") + } + + task.Execute() + + if testValue != 1 { + t.Fatal("Task did not execute") + } + + time.Sleep(TASK_TIME + time.Second) + + if testValue != 2 { + t.Fatal("Task re-executed") + } +} + +func TestExecuteTaskRecurring(t *testing.T) { + TASK_NAME := "Test Recurring Task" + TASK_TIME := time.Second * 5 + + testValue := 0 + testFunc := func() { + testValue += 1 + } + + task := CreateRecurringTask(TASK_NAME, testFunc, TASK_TIME) + if testValue != 0 { + t.Fatal("Unexpected execuition of task") + } + + time.Sleep(time.Second * 3) + + task.Execute() + if testValue != 1 { + t.Fatal("Task did not execute") + } + + time.Sleep(time.Second * 3) + if testValue != 1 { + t.Fatal("Task should not have executed before 5 seconds") + } + + time.Sleep(time.Second * 3) + + if testValue != 2 { + t.Fatal("Task did not re-execute after forced execution") + } +} -- cgit v1.2.3-1-g7c22