mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 07:10:55 +00:00
add api for testing
This commit is contained in:
parent
70cca5e298
commit
b838683731
@ -212,6 +212,11 @@ func DeleteTask(taskname string) {
|
|||||||
task.DeleteTask(taskname)
|
task.DeleteTask(taskname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClearTask clear all tasks
|
||||||
|
func ClearTask() {
|
||||||
|
task.ClearTask()
|
||||||
|
}
|
||||||
|
|
||||||
// MapSorter sort map for tasker
|
// MapSorter sort map for tasker
|
||||||
type MapSorter task.MapSorter
|
type MapSorter task.MapSorter
|
||||||
|
|
||||||
|
@ -22,6 +22,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
|
defer ClearTask()
|
||||||
|
|
||||||
tk := NewTask("taska", "0/30 * * * * *", func() error { fmt.Println("hello world"); return nil })
|
tk := NewTask("taska", "0/30 * * * * *", func() error { fmt.Println("hello world"); return nil })
|
||||||
err := tk.Run()
|
err := tk.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -34,6 +36,8 @@ func TestParse(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestSpec(t *testing.T) {
|
func TestSpec(t *testing.T) {
|
||||||
|
defer ClearTask()
|
||||||
|
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
tk1 := NewTask("tk1", "0 12 * * * *", func() error { fmt.Println("tk1"); return nil })
|
tk1 := NewTask("tk1", "0 12 * * * *", func() error { fmt.Println("tk1"); return nil })
|
||||||
|
@ -36,7 +36,7 @@ type taskManager struct {
|
|||||||
taskLock sync.RWMutex
|
taskLock sync.RWMutex
|
||||||
stop chan bool
|
stop chan bool
|
||||||
changed chan bool
|
changed chan bool
|
||||||
isstart bool
|
started bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTaskManager()*taskManager{
|
func newTaskManager()*taskManager{
|
||||||
@ -45,7 +45,7 @@ func newTaskManager()*taskManager{
|
|||||||
taskLock: sync.RWMutex{},
|
taskLock: sync.RWMutex{},
|
||||||
stop: make(chan bool),
|
stop: make(chan bool),
|
||||||
changed: make(chan bool),
|
changed: make(chan bool),
|
||||||
isstart: false,
|
started: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -431,16 +431,21 @@ func DeleteTask(taskName string) {
|
|||||||
globalTaskManager.DeleteTask(taskName)
|
globalTaskManager.DeleteTask(taskName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ClearTask clear all tasks
|
||||||
|
func ClearTask() {
|
||||||
|
globalTaskManager.ClearTask()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// StartTask start all tasks
|
// StartTask start all tasks
|
||||||
func (m *taskManager) StartTask() {
|
func (m *taskManager) StartTask() {
|
||||||
m.taskLock.Lock()
|
m.taskLock.Lock()
|
||||||
defer m.taskLock.Unlock()
|
defer m.taskLock.Unlock()
|
||||||
if m.isstart {
|
if m.started {
|
||||||
// If already started, no need to start another goroutine.
|
// If already started, no need to start another goroutine.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.isstart = true
|
m.started = true
|
||||||
|
|
||||||
registerCommands()
|
registerCommands()
|
||||||
go m.run()
|
go m.run()
|
||||||
@ -488,8 +493,8 @@ func(m *taskManager) run() {
|
|||||||
continue
|
continue
|
||||||
case <-m.stop:
|
case <-m.stop:
|
||||||
m.taskLock.Lock()
|
m.taskLock.Lock()
|
||||||
if m.isstart {
|
if m.started {
|
||||||
m.isstart = false
|
m.started = false
|
||||||
}
|
}
|
||||||
m.taskLock.Unlock()
|
m.taskLock.Unlock()
|
||||||
return
|
return
|
||||||
@ -510,7 +515,7 @@ func (m *taskManager)AddTask(taskname string, t Tasker) {
|
|||||||
m.taskLock.Lock()
|
m.taskLock.Lock()
|
||||||
t.SetNext(nil, time.Now().Local())
|
t.SetNext(nil, time.Now().Local())
|
||||||
m.adminTaskList[taskname] = t
|
m.adminTaskList[taskname] = t
|
||||||
if m.isstart {
|
if m.started {
|
||||||
isChanged = true
|
isChanged = true
|
||||||
}
|
}
|
||||||
m.taskLock.Unlock()
|
m.taskLock.Unlock()
|
||||||
@ -529,16 +534,29 @@ func(m *taskManager) DeleteTask(taskname string) {
|
|||||||
|
|
||||||
m.taskLock.Lock()
|
m.taskLock.Lock()
|
||||||
delete(m.adminTaskList, taskname)
|
delete(m.adminTaskList, taskname)
|
||||||
if m.isstart {
|
if m.started {
|
||||||
isChanged = true
|
isChanged = true
|
||||||
}
|
}
|
||||||
m.taskLock.Unlock()
|
m.taskLock.Unlock()
|
||||||
|
|
||||||
if isChanged {
|
if isChanged {
|
||||||
|
go func() {
|
||||||
m.changed <- true
|
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
|
// MapSorter sort map for tasker
|
||||||
type MapSorter struct {
|
type MapSorter struct {
|
||||||
Keys []string
|
Keys []string
|
||||||
|
@ -26,6 +26,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
|
m := newTaskManager()
|
||||||
|
defer m.ClearTask()
|
||||||
tk := NewTask("taska", "0/30 * * * * *", func(ctx context.Context) error {
|
tk := NewTask("taska", "0/30 * * * * *", func(ctx context.Context) error {
|
||||||
fmt.Println("hello world")
|
fmt.Println("hello world")
|
||||||
return nil
|
return nil
|
||||||
@ -34,14 +36,15 @@ func TestParse(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
AddTask("taska", tk)
|
m.AddTask("taska", tk)
|
||||||
StartTask()
|
m.StartTask()
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
StopTask()
|
m.StopTask()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestModifyTaskListAfterRunning(t *testing.T) {
|
func TestModifyTaskListAfterRunning(t *testing.T) {
|
||||||
m := newTaskManager()
|
m := newTaskManager()
|
||||||
|
defer m.ClearTask()
|
||||||
tk := NewTask("taskb", "0/30 * * * * *", func(ctx context.Context) error {
|
tk := NewTask("taskb", "0/30 * * * * *", func(ctx context.Context) error {
|
||||||
fmt.Println("hello world")
|
fmt.Println("hello world")
|
||||||
return nil
|
return nil
|
||||||
@ -65,6 +68,7 @@ func TestModifyTaskListAfterRunning(t *testing.T) {
|
|||||||
|
|
||||||
func TestSpec(t *testing.T) {
|
func TestSpec(t *testing.T) {
|
||||||
m := newTaskManager()
|
m := newTaskManager()
|
||||||
|
defer m.ClearTask()
|
||||||
wg := &sync.WaitGroup{}
|
wg := &sync.WaitGroup{}
|
||||||
wg.Add(2)
|
wg.Add(2)
|
||||||
tk1 := NewTask("tk1", "0 12 * * * *", func(ctx context.Context) error { fmt.Println("tk1"); return nil })
|
tk1 := NewTask("tk1", "0 12 * * * *", func(ctx context.Context) error { fmt.Println("tk1"); return nil })
|
||||||
|
Loading…
Reference in New Issue
Block a user