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"
"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
}

View file

@ -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 {

View file

@ -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
}

View file

@ -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 {

View file

@ -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
}
}

View file

@ -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()

View file

@ -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)
}