1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-27 09:31:30 +00:00
Beego/task/task_test.go

118 lines
2.7 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package task
2020-07-22 14:50:08 +00:00
import (
2020-09-06 07:21:07 +00:00
"context"
2020-07-22 14:50:08 +00:00
"errors"
"fmt"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestParse(t *testing.T) {
2020-10-05 02:33:23 +00:00
m := newTaskManager()
defer m.ClearTask()
2020-09-06 07:21:07 +00:00
tk := NewTask("taska", "0/30 * * * * *", func(ctx context.Context) error {
fmt.Println("hello world")
return nil
})
2020-08-30 16:18:59 +00:00
err := tk.Run(nil)
2020-07-22 14:50:08 +00:00
if err != nil {
t.Fatal(err)
}
2020-10-05 02:33:23 +00:00
m.AddTask("taska", tk)
m.StartTask()
2020-10-04 17:31:27 +00:00
time.Sleep(3 * time.Second)
2020-10-05 02:33:23 +00:00
m.StopTask()
2020-10-04 17:31:27 +00:00
}
func TestModifyTaskListAfterRunning(t *testing.T) {
2020-10-05 02:13:29 +00:00
m := newTaskManager()
2020-10-05 02:33:23 +00:00
defer m.ClearTask()
2020-10-05 02:13:29 +00:00
tk := NewTask("taskb", "0/30 * * * * *", func(ctx context.Context) error {
2020-10-04 17:31:27 +00:00
fmt.Println("hello world")
return nil
})
err := tk.Run(nil)
if err != nil {
t.Fatal(err)
}
2020-10-05 02:13:29 +00:00
m.AddTask("taskb", tk)
m.StartTask()
go func() {
m.DeleteTask("taskb")
}()
go func() {
m.AddTask("taskb1", tk)
}()
2020-10-04 17:31:27 +00:00
time.Sleep(3 * time.Second)
2020-10-05 02:13:29 +00:00
m.StopTask()
2020-07-22 14:50:08 +00:00
}
func TestSpec(t *testing.T) {
2020-10-05 02:13:29 +00:00
m := newTaskManager()
2020-10-05 02:33:23 +00:00
defer m.ClearTask()
2020-07-22 14:50:08 +00:00
wg := &sync.WaitGroup{}
wg.Add(2)
2020-09-06 07:21:07 +00:00
tk1 := NewTask("tk1", "0 12 * * * *", func(ctx context.Context) error { fmt.Println("tk1"); return nil })
tk2 := NewTask("tk2", "0,10,20 * * * * *", func(ctx context.Context) error { fmt.Println("tk2"); wg.Done(); return nil })
tk3 := NewTask("tk3", "0 10 * * * *", func(ctx context.Context) error { fmt.Println("tk3"); wg.Done(); return nil })
2020-07-22 14:50:08 +00:00
2020-10-05 02:13:29 +00:00
m.AddTask("tk1", tk1)
m.AddTask("tk2", tk2)
m.AddTask("tk3", tk3)
m.StartTask()
defer m.StopTask()
2020-07-22 14:50:08 +00:00
select {
case <-time.After(200 * time.Second):
t.FailNow()
case <-wait(wg):
}
}
func TestTask_Run(t *testing.T) {
cnt := -1
2020-09-06 07:21:07 +00:00
task := func(ctx context.Context) error {
2020-07-22 15:00:06 +00:00
cnt++
2020-07-22 14:50:08 +00:00
fmt.Printf("Hello, world! %d \n", cnt)
return errors.New(fmt.Sprintf("Hello, world! %d", cnt))
}
tk := NewTask("taska", "0/30 * * * * *", task)
2020-07-22 15:00:06 +00:00
for i := 0; i < 200; i++ {
2020-08-30 16:18:59 +00:00
e := tk.Run(nil)
2020-07-22 14:50:08 +00:00
assert.NotNil(t, e)
}
l := tk.Errlist
assert.Equal(t, 100, len(l))
assert.Equal(t, "Hello, world! 100", l[0].errinfo)
assert.Equal(t, "Hello, world! 101", l[1].errinfo)
}
func wait(wg *sync.WaitGroup) chan bool {
ch := make(chan bool)
go func() {
wg.Wait()
ch <- true
}()
return ch
}