68 lines
1.1 KiB
Go
68 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
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()
|
|
}
|