2020-10-19 15:30:15 +02:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
// See LICENSE.txt for license information.
|
|
|
|
|
|
|
|
package scheduler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCreateTask(t *testing.T) {
|
2020-10-22 13:34:42 +02:00
|
|
|
taskName := "Test Task"
|
|
|
|
taskTime := time.Millisecond * 200
|
|
|
|
taskWait := time.Millisecond * 100
|
2020-10-19 15:30:15 +02:00
|
|
|
|
|
|
|
executionCount := new(int32)
|
|
|
|
testFunc := func() {
|
|
|
|
atomic.AddInt32(executionCount, 1)
|
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
task := CreateTask(taskName, testFunc, taskTime)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-19 15:30:15 +02:00
|
|
|
assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
time.Sleep(taskTime + taskWait)
|
2020-10-19 15:30:15 +02:00
|
|
|
|
|
|
|
assert.EqualValues(t, 1, atomic.LoadInt32(executionCount))
|
2020-10-22 13:34:42 +02:00
|
|
|
assert.Equal(t, taskName, task.Name)
|
|
|
|
assert.Equal(t, taskTime, task.Interval)
|
2020-10-19 15:30:15 +02:00
|
|
|
assert.False(t, task.Recurring)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateRecurringTask(t *testing.T) {
|
2020-10-22 13:34:42 +02:00
|
|
|
taskName := "Test Recurring Task"
|
2022-04-14 00:36:20 +02:00
|
|
|
taskTime := time.Millisecond * 500
|
|
|
|
taskWait := time.Millisecond * 200
|
2020-10-19 15:30:15 +02:00
|
|
|
|
|
|
|
executionCount := new(int32)
|
|
|
|
testFunc := func() {
|
|
|
|
atomic.AddInt32(executionCount, 1)
|
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
task := CreateRecurringTask(taskName, testFunc, taskTime)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-19 15:30:15 +02:00
|
|
|
assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
time.Sleep(taskTime + taskWait)
|
2020-10-19 15:30:15 +02:00
|
|
|
|
|
|
|
assert.EqualValues(t, 1, atomic.LoadInt32(executionCount))
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
time.Sleep(taskTime)
|
2020-10-19 15:30:15 +02:00
|
|
|
|
|
|
|
assert.EqualValues(t, 2, atomic.LoadInt32(executionCount))
|
2020-10-22 13:34:42 +02:00
|
|
|
assert.Equal(t, taskName, task.Name)
|
|
|
|
assert.Equal(t, taskTime, task.Interval)
|
2020-10-19 15:30:15 +02:00
|
|
|
assert.True(t, task.Recurring)
|
|
|
|
|
|
|
|
task.Cancel()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCancelTask(t *testing.T) {
|
2020-10-22 13:34:42 +02:00
|
|
|
taskName := "Test Task"
|
|
|
|
taskTime := time.Millisecond * 100
|
|
|
|
taskWait := time.Millisecond * 100
|
2020-10-19 15:30:15 +02:00
|
|
|
|
|
|
|
executionCount := new(int32)
|
|
|
|
testFunc := func() {
|
|
|
|
atomic.AddInt32(executionCount, 1)
|
|
|
|
}
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
task := CreateTask(taskName, testFunc, taskTime)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-19 15:30:15 +02:00
|
|
|
assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
|
|
|
|
task.Cancel()
|
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
time.Sleep(taskTime + taskWait)
|
2020-10-19 15:30:15 +02:00
|
|
|
assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
|
|
|
|
}
|