basic Send() functionality

This commit is contained in:
Helmut Merz 2023-06-06 09:53:27 +02:00
parent 35799f8d6c
commit a02061b6c5
7 changed files with 51 additions and 19 deletions

View file

@ -5,6 +5,7 @@ import (
"strings" "strings"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/core"
) )
type Cfg struct { type Cfg struct {
@ -37,10 +38,16 @@ func (cfg *Base) Starter() lib.StartProc {
} }
func (cfg *Base) Step() lib.StepProc { func (cfg *Base) Step() lib.StepProc {
if cfg.step == nil {
return core.Step
}
return cfg.step return cfg.step
} }
func (cfg *Base) MessageHandler() lib.MessageHandler { func (cfg *Base) MessageHandler() lib.MessageHandler {
if cfg.msgHandler == nil {
return core.HandleMessage
}
return cfg.msgHandler return cfg.msgHandler
} }

View file

@ -53,16 +53,9 @@ func (ctx *context) Mailbox() chan lib.Message {
return ctx.mailbox 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 { func (ctx *context) HandleMsg(msg lib.Message) bool {
fmt.Println("HandleMsg ", msg) fmt.Println("context.HandleMsg ", msg)
// return ctx.Config().MessageHandler()(ctx, msg) return ctx.Config().MessageHandler()(ctx, msg)
return true
} }
func (ctx *context) WaitGroup() *sync.WaitGroup { func (ctx *context) WaitGroup() *sync.WaitGroup {

View file

@ -1,6 +1,10 @@
package core package core
import "git.sr.ht/~cco/go-scopes/lib" import (
"fmt"
"git.sr.ht/~cco/go-scopes/lib"
)
func Start(ctx lib.Context) { func Start(ctx lib.Context) {
Listen(ctx) Listen(ctx)
@ -12,10 +16,19 @@ func Listen(ctx lib.Context) {
} }
} }
func Step(ctx lib.Context) bool { func Step(ctx lib.Context) (loop bool) {
return true 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 { func HandleMessage(ctx lib.Context, msg lib.Message) bool {
fmt.Println("HandleMessage", msg)
return true return true
} }

View file

@ -40,7 +40,6 @@ type Context interface {
State() ContextState State() ContextState
WithState(ContextState) Context WithState(ContextState) Context
Mailbox() chan Message Mailbox() chan Message
Send(Address, Message)
HandleMsg(Message) bool HandleMsg(Message) bool
WaitGroup() *sync.WaitGroup WaitGroup() *sync.WaitGroup
Stop() Stop()
@ -48,6 +47,7 @@ type Context interface {
type Address interface { type Address interface {
Service() string Service() string
Send(Context, Message)
} }
type Message interface { type Message interface {

View file

@ -30,8 +30,20 @@ func (addr *address) Service() string {
return addr.service 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{ return &address{
service: srv, 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
}
}

View file

@ -50,7 +50,7 @@ func (t *T) TearDownApp() {
// give actors time to recieve all messages: // give actors time to recieve all messages:
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
//t.Check() //t.Check()
t.Ctx.Services()["testing"].Mailbox() <- message.Quit message.SimpleAddress("testing").Send(t.Ctx, message.Quit)
fmt.Println("teardown ", t.Ctx.WaitGroup()) fmt.Println("teardown ", t.Ctx.WaitGroup())
t.Ctx.WaitGroup().Wait() t.Ctx.WaitGroup().Wait()
//t.AssertNoUncheckedMessages() //t.AssertNoUncheckedMessages()

View file

@ -1,24 +1,24 @@
package scopes_test package scopes_test
import ( import (
"fmt"
tbase "testing" tbase "testing"
"git.sr.ht/~cco/go-scopes/app" "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/testing"
"git.sr.ht/~cco/go-scopes/tests/etc" "git.sr.ht/~cco/go-scopes/tests/etc"
) )
func TestConfig(tb *tbase.T) { func TestConfig(tb *tbase.T) {
t := testing.SetUpApp(tb, etc.Config()) t := testing.SetUpApp(tb, etc.Config())
t.Run("testing", TestingTest) t.Run("app", AppTest)
t.Run("config", ConfigTest) t.Run("config", ConfigTest)
t.Run("send", SendTest)
t.TearDownApp() t.TearDownApp()
} }
func TestingTest(t *testing.T) { func AppTest(t *testing.T) {
ctx := t.Ctx ctx := t.Ctx
fmt.Println(ctx.Services())
t.AssertEqual(len(ctx.Services()), 2) t.AssertEqual(len(ctx.Services()), 2)
} }
@ -30,3 +30,10 @@ func ConfigTest(t *testing.T) {
confCtx := t.Ctx.Services()["config"] confCtx := t.Ctx.Services()["config"]
t.AssertEqual(confCtx.Config().Name(), "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)
}