108 lines
1.9 KiB
Go
108 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/474420502/requests"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func TestTaskCasePushPop(t *testing.T) {
|
|
queue := NewQueue()
|
|
queue.Push(&Task{})
|
|
queue.Push(&Task{})
|
|
if queue.queue.Size() != 2 {
|
|
t.Error("size != 2")
|
|
}
|
|
|
|
task := queue.Pop()
|
|
if task == nil {
|
|
t.Error(task == nil)
|
|
}
|
|
if queue.queue.Size() != 1 {
|
|
t.Error("size != 1")
|
|
}
|
|
|
|
task = queue.PopBlock()
|
|
if task == nil {
|
|
t.Error(task == nil)
|
|
}
|
|
if queue.queue.Size() != 0 {
|
|
t.Error("size != 1")
|
|
}
|
|
}
|
|
|
|
func TestTaskCaseBlock(t *testing.T) {
|
|
queue := NewQueue()
|
|
wait := &sync.WaitGroup{}
|
|
wait.Add(1)
|
|
|
|
go func(wait *sync.WaitGroup, queue *Queue) {
|
|
defer wait.Done()
|
|
time.Sleep(time.Millisecond * 500)
|
|
queue.Push(&Task{})
|
|
time.Sleep(time.Millisecond * 500)
|
|
queue.Push(&Task{})
|
|
}(wait, queue)
|
|
|
|
if queue.PopBlock() == nil {
|
|
t.Error("value is error")
|
|
}
|
|
if queue.queue.Size() != 0 {
|
|
t.Error("queue errro")
|
|
}
|
|
first := time.Now()
|
|
|
|
if queue.PopBlock() == nil {
|
|
t.Error("value is error")
|
|
}
|
|
if queue.queue.Size() != 0 {
|
|
t.Error("queue errro")
|
|
}
|
|
|
|
if d := time.Now().Sub(first).Milliseconds(); d < 500 {
|
|
t.Errorf("time duration is error %d", d)
|
|
}
|
|
|
|
wait.Wait()
|
|
}
|
|
|
|
func TestTaskPostPushQueue(t *testing.T) {
|
|
temp := requests.NewSession().Post("http://localhost" + RemoteAddress + "/queue")
|
|
task := &Task{
|
|
Name: "test",
|
|
Type: 1,
|
|
CreateAt: time.Now(),
|
|
Info: gin.H{
|
|
"data": "test",
|
|
},
|
|
}
|
|
temp.SetBodyAuto(task)
|
|
resp, err := temp.Execute()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
log.Println(string(resp.Content()))
|
|
}
|
|
|
|
func TestBoss(t *testing.T) {
|
|
temp := requests.NewSession().Post("http://localhost" + RemoteAddress + "/queue")
|
|
task := &Task{
|
|
Name: "boss",
|
|
Type: 1,
|
|
CreateAt: time.Now(),
|
|
Info: gin.H{
|
|
"data": "boss",
|
|
},
|
|
}
|
|
temp.SetBodyAuto(task)
|
|
resp, err := temp.Execute()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
log.Println(string(resp.Content()))
|
|
}
|