1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 12:10:50 +00:00

add api for testing

This commit is contained in:
Anker Jam 2020-10-05 10:33:23 +08:00
parent 70cca5e298
commit b838683731
4 changed files with 43 additions and 12 deletions

View File

@ -212,6 +212,11 @@ func DeleteTask(taskname string) {
task.DeleteTask(taskname)
}
// ClearTask clear all tasks
func ClearTask() {
task.ClearTask()
}
// MapSorter sort map for tasker
type MapSorter task.MapSorter

View File

@ -22,6 +22,8 @@ import (
)
func TestParse(t *testing.T) {
defer ClearTask()
tk := NewTask("taska", "0/30 * * * * *", func() error { fmt.Println("hello world"); return nil })
err := tk.Run()
if err != nil {
@ -34,6 +36,8 @@ func TestParse(t *testing.T) {
}
func TestSpec(t *testing.T) {
defer ClearTask()
wg := &sync.WaitGroup{}
wg.Add(2)
tk1 := NewTask("tk1", "0 12 * * * *", func() error { fmt.Println("tk1"); return nil })

View File

@ -36,7 +36,7 @@ type taskManager struct {
taskLock sync.RWMutex
stop chan bool
changed chan bool
isstart bool
started bool
}
func newTaskManager()*taskManager{
@ -45,7 +45,7 @@ func newTaskManager()*taskManager{
taskLock: sync.RWMutex{},
stop: make(chan bool),
changed: make(chan bool),
isstart: false,
started: false,
}
}
@ -431,16 +431,21 @@ func DeleteTask(taskName string) {
globalTaskManager.DeleteTask(taskName)
}
// ClearTask clear all tasks
func ClearTask() {
globalTaskManager.ClearTask()
}
// StartTask start all tasks
func (m *taskManager) StartTask() {
m.taskLock.Lock()
defer m.taskLock.Unlock()
if m.isstart {
if m.started {
// If already started no need to start another goroutine.
return
}
m.isstart = true
m.started = true
registerCommands()
go m.run()
@ -488,8 +493,8 @@ func(m *taskManager) run() {
continue
case <-m.stop:
m.taskLock.Lock()
if m.isstart {
m.isstart = false
if m.started {
m.started = false
}
m.taskLock.Unlock()
return
@ -510,7 +515,7 @@ func (m *taskManager)AddTask(taskname string, t Tasker) {
m.taskLock.Lock()
t.SetNext(nil, time.Now().Local())
m.adminTaskList[taskname] = t
if m.isstart {
if m.started {
isChanged = true
}
m.taskLock.Unlock()
@ -529,16 +534,29 @@ func(m *taskManager) DeleteTask(taskname string) {
m.taskLock.Lock()
delete(m.adminTaskList, taskname)
if m.isstart {
if m.started {
isChanged = true
}
m.taskLock.Unlock()
if isChanged {
m.changed <- true
go func() {
m.changed <- true
}()
}
}
// ClearTask clear all tasks
func(m *taskManager) ClearTask() {
m.taskLock.Lock()
m.adminTaskList = make(map[string]Tasker)
m.taskLock.Unlock()
go func() {
m.changed <- true
}()
}
// MapSorter sort map for tasker
type MapSorter struct {
Keys []string

View File

@ -26,6 +26,8 @@ import (
)
func TestParse(t *testing.T) {
m := newTaskManager()
defer m.ClearTask()
tk := NewTask("taska", "0/30 * * * * *", func(ctx context.Context) error {
fmt.Println("hello world")
return nil
@ -34,14 +36,15 @@ func TestParse(t *testing.T) {
if err != nil {
t.Fatal(err)
}
AddTask("taska", tk)
StartTask()
m.AddTask("taska", tk)
m.StartTask()
time.Sleep(3 * time.Second)
StopTask()
m.StopTask()
}
func TestModifyTaskListAfterRunning(t *testing.T) {
m := newTaskManager()
defer m.ClearTask()
tk := NewTask("taskb", "0/30 * * * * *", func(ctx context.Context) error {
fmt.Println("hello world")
return nil
@ -65,6 +68,7 @@ func TestModifyTaskListAfterRunning(t *testing.T) {
func TestSpec(t *testing.T) {
m := newTaskManager()
defer m.ClearTask()
wg := &sync.WaitGroup{}
wg.Add(2)
tk1 := NewTask("tk1", "0 12 * * * *", func(ctx context.Context) error { fmt.Println("tk1"); return nil })