From a02061b6c58fd2782a8faccb36ea64bbcf4698ab Mon Sep 17 00:00:00 2001 From: Helmut Merz Date: Tue, 6 Jun 2023 09:53:27 +0200 Subject: [PATCH] basic Send() functionality --- config/config.go | 7 +++++++ lib/context/context.go | 11 ++--------- lib/core/core.go | 19 ++++++++++++++++--- lib/lib.go | 2 +- lib/message/message.go | 14 +++++++++++++- testing/testing.go | 2 +- tests/scopes_test.go | 15 +++++++++++---- 7 files changed, 51 insertions(+), 19 deletions(-) diff --git a/config/config.go b/config/config.go index 454e216..2ad90b0 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "strings" "git.sr.ht/~cco/go-scopes/lib" + "git.sr.ht/~cco/go-scopes/lib/core" ) type Cfg struct { @@ -37,10 +38,16 @@ func (cfg *Base) Starter() lib.StartProc { } func (cfg *Base) Step() lib.StepProc { + if cfg.step == nil { + return core.Step + } return cfg.step } func (cfg *Base) MessageHandler() lib.MessageHandler { + if cfg.msgHandler == nil { + return core.HandleMessage + } return cfg.msgHandler } diff --git a/lib/context/context.go b/lib/context/context.go index ce472f9..1a97c5f 100644 --- a/lib/context/context.go +++ b/lib/context/context.go @@ -53,16 +53,9 @@ func (ctx *context) Mailbox() chan lib.Message { return ctx.mailbox } -func (ctx *context) Send(rcvr lib.Address, msg lib.Message) { - if srv, ok := ctx.Services()[rcvr.Service()]; ok { - srv.Mailbox() <- msg - } -} - func (ctx *context) HandleMsg(msg lib.Message) bool { - fmt.Println("HandleMsg ", msg) - // return ctx.Config().MessageHandler()(ctx, msg) - return true + fmt.Println("context.HandleMsg ", msg) + return ctx.Config().MessageHandler()(ctx, msg) } func (ctx *context) WaitGroup() *sync.WaitGroup { diff --git a/lib/core/core.go b/lib/core/core.go index b9e4939..2514a84 100644 --- a/lib/core/core.go +++ b/lib/core/core.go @@ -1,6 +1,10 @@ package core -import "git.sr.ht/~cco/go-scopes/lib" +import ( + "fmt" + + "git.sr.ht/~cco/go-scopes/lib" +) func Start(ctx lib.Context) { Listen(ctx) @@ -12,10 +16,19 @@ func Listen(ctx lib.Context) { } } -func Step(ctx lib.Context) bool { - return true +func Step(ctx lib.Context) (loop bool) { + loop = true + //defer ctx.LogCatch("Step") + select { + case msg := <-ctx.Mailbox(): + loop = ctx.HandleMsg(msg) + case <-ctx.Done(): + loop = false + } + return } func HandleMessage(ctx lib.Context, msg lib.Message) bool { + fmt.Println("HandleMessage", msg) return true } diff --git a/lib/lib.go b/lib/lib.go index 918433b..28ddaca 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -40,7 +40,6 @@ type Context interface { State() ContextState WithState(ContextState) Context Mailbox() chan Message - Send(Address, Message) HandleMsg(Message) bool WaitGroup() *sync.WaitGroup Stop() @@ -48,6 +47,7 @@ type Context interface { type Address interface { Service() string + Send(Context, Message) } type Message interface { diff --git a/lib/message/message.go b/lib/message/message.go index e3a46f3..dc7a9bf 100644 --- a/lib/message/message.go +++ b/lib/message/message.go @@ -30,8 +30,20 @@ func (addr *address) Service() string { return addr.service } -func CreateAddress(srv string) *address { +func (addr *address) Send(ctx lib.Context, msg lib.Message) { + Send(ctx, addr, msg) +} + +func SimpleAddress(srv string) *address { return &address{ service: srv, } } + +// base functions + +func Send(ctx lib.Context, addr lib.Address, msg lib.Message) { + if srv, ok := ctx.Services()[addr.Service()]; ok { + srv.Mailbox() <- msg + } +} diff --git a/testing/testing.go b/testing/testing.go index 3217e4b..4d52bc6 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -50,7 +50,7 @@ func (t *T) TearDownApp() { // give actors time to recieve all messages: time.Sleep(100 * time.Millisecond) //t.Check() - t.Ctx.Services()["testing"].Mailbox() <- message.Quit + message.SimpleAddress("testing").Send(t.Ctx, message.Quit) fmt.Println("teardown ", t.Ctx.WaitGroup()) t.Ctx.WaitGroup().Wait() //t.AssertNoUncheckedMessages() diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 7beb6eb..39706f0 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -1,24 +1,24 @@ package scopes_test import ( - "fmt" tbase "testing" "git.sr.ht/~cco/go-scopes/app" + "git.sr.ht/~cco/go-scopes/lib/message" "git.sr.ht/~cco/go-scopes/testing" "git.sr.ht/~cco/go-scopes/tests/etc" ) func TestConfig(tb *tbase.T) { t := testing.SetUpApp(tb, etc.Config()) - t.Run("testing", TestingTest) + t.Run("app", AppTest) t.Run("config", ConfigTest) + t.Run("send", SendTest) t.TearDownApp() } -func TestingTest(t *testing.T) { +func AppTest(t *testing.T) { ctx := t.Ctx - fmt.Println(ctx.Services()) t.AssertEqual(len(ctx.Services()), 2) } @@ -30,3 +30,10 @@ func ConfigTest(t *testing.T) { confCtx := t.Ctx.Services()["config"] t.AssertEqual(confCtx.Config().Name(), "config") } + +func SendTest(t *testing.T) { + ctx := t.Ctx + rcvr := message.SimpleAddress("testing") + msg := message.StrMessage("demo") + rcvr.Send(ctx, msg) +}