main

mattermost/focalboard

Last updated at: 29/12/2023 09:40

scheduler_test.go

TLDR

This file contains tests for the scheduler package in the Demo Projects project. The tests cover the CreateTask, CreateRecurringTask, and CancelTask functions.

Methods

TestCreateTask

This method tests the CreateTask function. It creates a task with a given name, interval, and test function. It then asserts that the execution count is initially zero and increments the count when the test function is called. After a certain amount of time has passed, it asserts that the execution count is incremented, the task name and interval are correct, and the task is not recurring.

TestCreateRecurringTask

This method tests the CreateRecurringTask function. Similar to the previous method, it creates a recurring task and asserts the execution count, increments the count after a certain amount of time, and cancels the task. It also asserts that the task is recurring.

TestCancelTask

This method tests the CancelTask function. It creates a task, cancels it, and confirms that the execution count is not incremented after a certain amount of time.

// 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) {
	taskName := "Test Task"
	taskTime := time.Millisecond * 200
	taskWait := time.Millisecond * 100

	executionCount := new(int32)
	testFunc := func() {
		atomic.AddInt32(executionCount, 1)
	}

	task := CreateTask(taskName, testFunc, taskTime)

	assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))

	time.Sleep(taskTime + taskWait)

	assert.EqualValues(t, 1, atomic.LoadInt32(executionCount))
	assert.Equal(t, taskName, task.Name)
	assert.Equal(t, taskTime, task.Interval)
	assert.False(t, task.Recurring)
}

func TestCreateRecurringTask(t *testing.T) {
	taskName := "Test Recurring Task"
	taskTime := time.Millisecond * 500
	taskWait := time.Millisecond * 200

	executionCount := new(int32)
	testFunc := func() {
		atomic.AddInt32(executionCount, 1)
	}

	task := CreateRecurringTask(taskName, testFunc, taskTime)

	assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))

	time.Sleep(taskTime + taskWait)

	assert.EqualValues(t, 1, atomic.LoadInt32(executionCount))

	time.Sleep(taskTime)

	assert.EqualValues(t, 2, atomic.LoadInt32(executionCount))
	assert.Equal(t, taskName, task.Name)
	assert.Equal(t, taskTime, task.Interval)
	assert.True(t, task.Recurring)

	task.Cancel()
}

func TestCancelTask(t *testing.T) {
	taskName := "Test Task"
	taskTime := time.Millisecond * 100
	taskWait := time.Millisecond * 100

	executionCount := new(int32)
	testFunc := func() {
		atomic.AddInt32(executionCount, 1)
	}

	task := CreateTask(taskName, testFunc, taskTime)

	assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
	task.Cancel()

	time.Sleep(taskTime + taskWait)
	assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
}