summaryrefslogtreecommitdiffstats
path: root/model/scheduled_task_test.go
blob: 5af43b1eff86bc9b6cf3372675104d26cf8e85ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"testing"
	"time"
)

func TestCreateTask(t *testing.T) {
	TASK_NAME := "Test Task"
	TASK_TIME := time.Second * 3

	testValue := 0
	testFunc := func() {
		testValue = 1
	}

	task := CreateTask(TASK_NAME, testFunc, TASK_TIME)
	if testValue != 0 {
		t.Fatal("Unexpected execuition of task")
	}

	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")
	}
}

func TestCreateRecurringTask(t *testing.T) {
	TASK_NAME := "Test Recurring Task"
	TASK_TIME := time.Second * 3

	testValue := 0
	testFunc := func() {
		testValue += 1
	}

	task := CreateRecurringTask(TASK_NAME, testFunc, TASK_TIME)
	if testValue != 0 {
		t.Fatal("Unexpected execuition of task")
	}

	time.Sleep(TASK_TIME + time.Second)

	if testValue != 1 {
		t.Fatal("Task did not execute")
	}

	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")
	}

	task.Cancel()
}

func TestCancelTask(t *testing.T) {
	TASK_NAME := "Test Task"
	TASK_TIME := time.Second * 3

	testValue := 0
	testFunc := func() {
		testValue = 1
	}

	task := CreateTask(TASK_NAME, testFunc, TASK_TIME)
	if testValue != 0 {
		t.Fatal("Unexpected execuition of task")
	}
	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")
	}
}