diff --git a/app/app.go b/app/app.go index 3a4dbd0..5e49f2a 100644 --- a/app/app.go +++ b/app/app.go @@ -43,11 +43,11 @@ func step(ctx lib.Context, sig <-chan os.Signal) bool { case msg := <-ctx.Mailbox(): fmt.Println("message", msg) //ctx.LogDebug("dispatcherStep", m.Map{"msg": msg}) - if msg == "quit" { + if msg.Action() == "quit" { //ctx.LogInfo("Dispatcher stopped", m.Map{}) return false } - //ctx.HandleMsg(msg) + return ctx.HandleMsg(msg) } return true } diff --git a/lib/context/context.go b/lib/context/context.go index d0ec5b8..11b3a6e 100644 --- a/lib/context/context.go +++ b/lib/context/context.go @@ -2,6 +2,7 @@ package context import ( stdlib_context "context" + "fmt" "sync" "time" @@ -24,7 +25,7 @@ func (ctx *context) Config() lib.Config { return ctx.cfg } -func (ctx *context) Parent() Context { +func (ctx *context) Parent() lib.Context { return ctx.parent } @@ -52,6 +53,12 @@ func (ctx *context) Mailbox() chan lib.Message { return ctx.mailbox } +func (ctx *context) HandleMsg(msg lib.Message) bool { + fmt.Println("HandleMsg ", msg) + // return ctx.Config().MessageHandler()(ctx, msg) + return true +} + func (ctx *context) WaitGroup() *sync.WaitGroup { return ctx.parent.WaitGroup() } diff --git a/lib/lib.go b/lib/lib.go index 158e8e4..2f1ecf5 100644 --- a/lib/lib.go +++ b/lib/lib.go @@ -38,11 +38,14 @@ type Context interface { State() ContextState WithState(ContextState) Context Mailbox() chan Message + HandleMsg(Message) bool WaitGroup() *sync.WaitGroup Stop() } -type Message interface{} +type Message interface { + Action() string +} type Action interface { Context() Context diff --git a/lib/message/message.go b/lib/message/message.go new file mode 100644 index 0000000..7048aae --- /dev/null +++ b/lib/message/message.go @@ -0,0 +1,11 @@ +package message + +type strMessage string + +func (msg strMessage) Action() string { + return string(msg) +} + +func StrMessage(action string) strMessage { + return strMessage(action) +} diff --git a/testing/testing.go b/testing/testing.go index 39105cc..f8b210b 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -11,6 +11,7 @@ import ( "git.sr.ht/~cco/go-scopes/app" "git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib/context" + "git.sr.ht/~cco/go-scopes/lib/message" ) func Start(ctx lib.Context) { @@ -49,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() <- "quit" + t.Ctx.Services()["testing"].Mailbox() <- message.StrMessage("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 386cb34..7beb6eb 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -19,6 +19,7 @@ func TestConfig(tb *tbase.T) { func TestingTest(t *testing.T) { ctx := t.Ctx fmt.Println(ctx.Services()) + t.AssertEqual(len(ctx.Services()), 2) } func ConfigTest(t *testing.T) {