diff --git a/queue.go b/queue.go index 5a8a656..216dd70 100644 --- a/queue.go +++ b/queue.go @@ -2,8 +2,8 @@ package queue import ( "container/heap" - "github.com/prometheus/common/log" "loafle.com/overflow/agent_api/observer" + "log" "time" ) @@ -78,6 +78,20 @@ func (lq *LoafleQueue) notifyEventHandler(c chan interface{}) { } } +func (lq *LoafleQueue) StringType(tp EventType) string { + var tempType string + + if tp == DATA_TYPE { + tempType = "DATA_TYPE" + } else if tp == EVENT_TYPE { + tempType = "EVENT_TYPE" + } else { + log.Fatal("Event Type Error") + } + + return tempType +} + func (lq *LoafleQueue) Close() { if lq.eventChanel != nil { observer.Remove(lq.queueType, lq.eventChanel) @@ -86,23 +100,18 @@ func (lq *LoafleQueue) Close() { func NewLoafleQueue(eventType EventType, interval time.Duration) *LoafleQueue { items := make([]*item, 0) event := make(chan interface{}, 0) - var tempType string - - if eventType == DATA_TYPE { - tempType = "DATA_TYPE" - } else if eventType == EVENT_TYPE { - tempType = "EVENT_TYPE" - } else { - log.Fatal("Event Type Error") - } lq := &LoafleQueue{ - items: items, - queueType: tempType, + items: items, + //queueType: tempType, interval: interval, eventChanel: event, } + lq.queueType = lq.StringType(eventType) + + heap.Init(lq) + observer.Add(lq.queueType, lq.eventChanel) go lq.notifyEventHandler(event) diff --git a/queue_test.go b/queue_test.go new file mode 100644 index 0000000..2b047f5 --- /dev/null +++ b/queue_test.go @@ -0,0 +1,21 @@ +package queue + +import ( + "fmt" + "loafle.com/overflow/agent_api/observer" + "testing" +) + +func TestNewLoafleQueue(t *testing.T) { + lq := NewLoafleQueue(EVENT_TYPE, 5) + typeEvent := lq.StringType(EVENT_TYPE) + + observer.Notify(typeEvent, "data") + + re := lq.GetItems() + + for it, val := range re { + fmt.Println(val) + fmt.Println(it) + } +}