getItems edited

This commit is contained in:
geek 2017-04-27 17:48:05 +09:00
parent 6b8b6f6189
commit 53ce7418af
2 changed files with 11 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package queue
import (
"container/heap"
"loafle.com/overflow/agent_api/observer"
"log"
"sync"
"time"
)
@ -37,6 +38,7 @@ func (lq LoafleQueue) Swap(i, j int) {
}
func (lq *LoafleQueue) getItems() {
resultItems := make([]*Item, 0)
for {
@ -44,7 +46,7 @@ func (lq *LoafleQueue) getItems() {
if lq.Len() > 0 {
lq.size = lq.Len()
resultItems := make([]*Item, 0)
lq.mtx.Lock()
for i := 0; i < lq.size; i++ {
item := heap.Pop(lq).(*Item)
@ -54,6 +56,8 @@ func (lq *LoafleQueue) getItems() {
lq.senderChanel <- resultItems
lq.size = lq.Len()
//log.Println("result length: ", len(resultItems))
resultItems = nil
}
}

View File

@ -12,7 +12,7 @@ import (
func TestNewQueue(t *testing.T) {
senderChanel := make(chan interface{}, 0)
lq := NewQueue(messages.QUEUE_DATA, 5, senderChanel)
lq := NewQueue(messages.QUEUE_DATA, 3, senderChanel)
log.Println(lq)
@ -26,6 +26,11 @@ func TestNewQueue(t *testing.T) {
observer.Notify(messages.QUEUE_DATA, "data7777")
observer.Notify(messages.QUEUE_DATA, "data8888")
observer.Notify(messages.QUEUE_DATA, "data9999")
go GetItemTest(senderChanel)
time.Sleep(time.Second * 3)
observer.Notify(messages.QUEUE_DATA, "data1111")
observer.Notify(messages.QUEUE_DATA, "data2222")
observer.Notify(messages.QUEUE_DATA, "data3333")
@ -34,8 +39,6 @@ func TestNewQueue(t *testing.T) {
observer.Notify(messages.QUEUE_DATA, "data6666")
observer.Notify(messages.QUEUE_DATA, "data7777")
go GetItemTest(senderChanel)
time.Sleep(time.Second * 10)
lq.Close()