From b480e0daf07dd5c51c0d0d6703d28e029afc8e9d Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Tue, 14 Feb 2023 16:36:16 +0800 Subject: [PATCH] feat: update basic send function --- main.go | 38 ++++++++++++++++++++++++++++++++++++++ message/message_test.go | 3 ++- service/service.go | 10 ++++++---- service/service_test.go | 22 ++++++++++++++++++++++ 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 service/service_test.go diff --git a/main.go b/main.go index 7905807..c1296fc 100644 --- a/main.go +++ b/main.go @@ -1,5 +1,43 @@ package main +import ( + "log" + "sendmsg/config" + "sendmsg/global" + "sendmsg/message" + "sendmsg/model" + "sendmsg/service" + "time" +) + +func init() { + global.GLO_CONF_CH = make(chan model.Config) + _conf, err := config.InitConfig() + if err != nil { + log.Fatalf("[init] Failed to initialize config: %v", err) + } + global.GLO_CONF = _conf +} func main() { + //test + go func() { + var count int + for { + if count > 5 { + return + } + count++ + var s = service.Service{Body: message.Body{ + Title: "test title", + Content: "this is content, time is " + time.Now().Format("2006-01-02 15:04:05"), + }} + s.Run() + time.Sleep(time.Second * 2) + } + }() + for { + _conf := <-global.GLO_CONF_CH + global.GLO_CONF = _conf + } } diff --git a/message/message_test.go b/message/message_test.go index b85fc8e..e2b3ee2 100644 --- a/message/message_test.go +++ b/message/message_test.go @@ -5,6 +5,7 @@ import ( "sendmsg/config" "sendmsg/global" "testing" + "time" ) func TestSend(t *testing.T) { @@ -20,6 +21,6 @@ func TestSend(t *testing.T) { } m.Send(Body{ Title: "Hello", - Content: "World", + Content: "World " + time.Now().Format("2006-01-02 15:04:05"), }) } diff --git a/service/service.go b/service/service.go index 8d28d73..ca47142 100644 --- a/service/service.go +++ b/service/service.go @@ -5,11 +5,13 @@ import ( "sendmsg/message" ) -type Support struct{} - -func (s Support) Run() { - log.Printf("[support] start excute...") +type Service struct { + Body message.Body +} +func (s Service) Run() { + log.Printf("[service] start excute...") + send(s.Body) } func send(body message.Body) { diff --git a/service/service_test.go b/service/service_test.go new file mode 100644 index 0000000..c96b8ee --- /dev/null +++ b/service/service_test.go @@ -0,0 +1,22 @@ +package service + +import ( + "sendmsg/config" + "sendmsg/global" + "sendmsg/message" + "testing" + "time" +) + +func TestService(t *testing.T) { + conf, err := config.InitConfig() + if err != nil { + t.Fatalf("Failed to initialize config: %v", err) + } + global.GLO_CONF = conf + var s = Service{Body: message.Body{ + Title: "test title", + Content: "this is content, time is " + time.Now().Format("2006-01-02 15:04:05"), + }} + s.Run() +}