summaryrefslogtreecommitdiffstats
path: root/model/scheduled_task_test.go
diff options
context:
space:
mode:
authorChris <ccbrown112@gmail.com>2018-02-23 12:56:32 -0600
committerGitHub <noreply@github.com>2018-02-23 12:56:32 -0600
commit55261a2b155c4e3d699fc6bd4255f05fb8258157 (patch)
treeed6f7006c90389108438270d2a837a0db9b28a2d /model/scheduled_task_test.go
parent5fb4b1bc935bd273990779e1d4a680a563784df8 (diff)
downloadchat-55261a2b155c4e3d699fc6bd4255f05fb8258157.tar.gz
chat-55261a2b155c4e3d699fc6bd4255f05fb8258157.tar.bz2
chat-55261a2b155c4e3d699fc6bd4255f05fb8258157.zip
fix scheduled task race (#8355)
Diffstat (limited to 'model/scheduled_task_test.go')
-rw-r--r--model/scheduled_task_test.go163
1 files changed, 25 insertions, 138 deletions
diff --git a/model/scheduled_task_test.go b/model/scheduled_task_test.go
index 5af43b1ef..9537a662a 100644
--- a/model/scheduled_task_test.go
+++ b/model/scheduled_task_test.go
@@ -4,185 +4,72 @@
package model
import (
+ "sync/atomic"
"testing"
"time"
+
+ "github.com/stretchr/testify/assert"
)
func TestCreateTask(t *testing.T) {
TASK_NAME := "Test Task"
- TASK_TIME := time.Second * 3
+ TASK_TIME := time.Second * 2
- testValue := 0
+ executionCount := new(int32)
testFunc := func() {
- testValue = 1
+ atomic.AddInt32(executionCount, 1)
}
task := CreateTask(TASK_NAME, testFunc, TASK_TIME)
- if testValue != 0 {
- t.Fatal("Unexpected execuition of task")
- }
+ assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
time.Sleep(TASK_TIME + time.Second)
- if testValue != 1 {
- t.Fatal("Task did not execute")
- }
-
- if task.Name != TASK_NAME {
- t.Fatal("Bad name")
- }
-
- if task.Interval != TASK_TIME {
- t.Fatal("Bad interval")
- }
-
- if task.Recurring {
- t.Fatal("should not reccur")
- }
+ assert.EqualValues(t, 1, atomic.LoadInt32(executionCount))
+ assert.Equal(t, TASK_NAME, task.Name)
+ assert.Equal(t, TASK_TIME, task.Interval)
+ assert.False(t, task.Recurring)
}
func TestCreateRecurringTask(t *testing.T) {
TASK_NAME := "Test Recurring Task"
- TASK_TIME := time.Second * 3
+ TASK_TIME := time.Second * 2
- testValue := 0
+ executionCount := new(int32)
testFunc := func() {
- testValue += 1
+ atomic.AddInt32(executionCount, 1)
}
task := CreateRecurringTask(TASK_NAME, testFunc, TASK_TIME)
- if testValue != 0 {
- t.Fatal("Unexpected execuition of task")
- }
+ assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
time.Sleep(TASK_TIME + time.Second)
- if testValue != 1 {
- t.Fatal("Task did not execute")
- }
+ assert.EqualValues(t, 1, atomic.LoadInt32(executionCount))
time.Sleep(TASK_TIME)
- if testValue != 2 {
- t.Fatal("Task did not re-execute")
- }
-
- if task.Name != TASK_NAME {
- t.Fatal("Bad name")
- }
-
- if task.Interval != TASK_TIME {
- t.Fatal("Bad interval")
- }
-
- if !task.Recurring {
- t.Fatal("should reccur")
- }
+ assert.EqualValues(t, 2, atomic.LoadInt32(executionCount))
+ assert.Equal(t, TASK_NAME, task.Name)
+ assert.Equal(t, TASK_TIME, task.Interval)
+ assert.True(t, task.Recurring)
task.Cancel()
}
func TestCancelTask(t *testing.T) {
TASK_NAME := "Test Task"
- TASK_TIME := time.Second * 3
+ TASK_TIME := time.Second
- testValue := 0
+ executionCount := new(int32)
testFunc := func() {
- testValue = 1
+ atomic.AddInt32(executionCount, 1)
}
task := CreateTask(TASK_NAME, testFunc, TASK_TIME)
- if testValue != 0 {
- t.Fatal("Unexpected execuition of task")
- }
+ assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
task.Cancel()
time.Sleep(TASK_TIME + time.Second)
-
- if testValue != 0 {
- t.Fatal("Unexpected execuition of task")
- }
-}
-
-func TestGetAllTasks(t *testing.T) {
- doNothing := func() {}
-
- CreateTask("Task1", doNothing, time.Hour)
- CreateTask("Task2", doNothing, time.Second)
- CreateRecurringTask("Task3", doNothing, time.Second)
- task4 := CreateRecurringTask("Task4", doNothing, time.Second)
-
- task4.Cancel()
-
- time.Sleep(time.Second * 3)
-
- tasks := *GetAllTasks()
- if len(tasks) != 2 {
- t.Fatal("Wrong number of tasks got: ", len(tasks))
- }
- for _, task := range tasks {
- if task.Name != "Task1" && task.Name != "Task3" {
- t.Fatal("Wrong tasks")
- }
- }
-}
-
-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")
- }
+ assert.EqualValues(t, 0, atomic.LoadInt32(executionCount))
}