From 618339b0868a182ed0a40fbdc6e5593e467283d9 Mon Sep 17 00:00:00 2001 From: geek Date: Fri, 28 Apr 2017 12:37:33 +0900 Subject: [PATCH] event test --- event_sender.go | 36 +++++++++++++++++++++++++++++++++++- event_sender_test.go | 8 +++++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/event_sender.go b/event_sender.go index 59a7929..714f341 100644 --- a/event_sender.go +++ b/event_sender.go @@ -8,6 +8,14 @@ import ( q "loafle.com/overflow/queue_go" "log" "reflect" + "loafle.com/overflow/agent_api/observer" + "sync" +) + + +var ( + instance *EventSender + once sync.Once ) const ( @@ -24,12 +32,38 @@ type Data struct { FinishedAt uint64 } + +func GetInstance() *EventSender { + once.Do(func() { + instance = &EventSender{} + }) + return instance +} + +func init() { + ch := make(chan interface{}, 0) + observer.Add(messages.CONFIGMANAGER_LOADED, ch) + handleInit(ch) +} + +func handleInit(ch chan interface{}) { + es := GetInstance() + go func() { + data := <-ch + log.Println("handleInit", data) + //ds.gconf = data.(cm.ConfigManager).GetGlobalConfig() + es.start() + close(ch) + }() +} + type EventSender struct { + once sync.Once lq *q.LoafleQueue sc chan interface{} } -func (es *EventSender) Start() { +func (es *EventSender) start() { es.sc = make(chan interface{}, 0) es.lq = q.NewQueue(messages.QUEUE_EVENT, DEFAULT_INTERVAL, es.sc) diff --git a/event_sender_test.go b/event_sender_test.go index a9b1061..6611db5 100644 --- a/event_sender_test.go +++ b/event_sender_test.go @@ -11,11 +11,13 @@ import ( func TestEventSender_Start(t *testing.T) { - es := &EventSender{} - es.Start() + observer.Notify("CONFIGMANAGER_LOADED", nil) + + time.Sleep(time.Second * 5) + testNotify() - time.Sleep(time.Second * 1000) + time.Sleep(time.Second * 100) }