provide DelayedStart() (proof-of-concept): parameterized handler (factory)

This commit is contained in:
Helmut Merz 2023-08-11 17:27:26 +02:00
parent 2286aef4ce
commit a67c31e0b5
4 changed files with 16 additions and 2 deletions

View file

@ -2,6 +2,7 @@ package core
import ( import (
"fmt" "fmt"
"time"
lib "git.sr.ht/~cco/go-scopes" lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/config" "git.sr.ht/~cco/go-scopes/config"
@ -16,6 +17,17 @@ func Start(ctx lib.Context) {
lib.RunCtx(ctx, Listen) lib.RunCtx(ctx, Listen)
} }
func DelayedStart(starter lib.StartProc, delay time.Duration) lib.StartProc {
if starter == nil {
starter = Start
}
return func(ctx lib.Context) {
logging.Debug(ctx).Int("delay", int(delay)).Msg("core.DelayedStart")
time.Sleep(delay * time.Millisecond)
starter(ctx)
}
}
func Listen(ctx lib.Context) { func Listen(ctx lib.Context) {
step := ctx.Config().Step() step := ctx.Config().Step()
for step(ctx) { for step(ctx) {

View file

@ -7,6 +7,7 @@ import (
lib "git.sr.ht/~cco/go-scopes" lib "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/core" "git.sr.ht/~cco/go-scopes/core"
"git.sr.ht/~cco/go-scopes/core/message" "git.sr.ht/~cco/go-scopes/core/message"
"git.sr.ht/~cco/go-scopes/logging"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -44,6 +45,7 @@ func Serve(ctx lib.Context) {
} }
srv := &http.Server{Addr: cfg.Addr, Handler: r} srv := &http.Server{Addr: cfg.Addr, Handler: r}
ctx.WithState(&ServerState{server: srv}) ctx.WithState(&ServerState{server: srv})
logging.Debug(ctx).Str("addr", cfg.Addr).Msg("server.Serve")
lib.RunCtx(ctx, func(ctx lib.Context) { lib.RunCtx(ctx, func(ctx lib.Context) {
srv.ListenAndServe() srv.ListenAndServe()
}) })

View file

@ -35,7 +35,7 @@ func Config() lib.Config {
server_c.AddRoute("/api", server.MsgHandler("demo", nil, "test-receiver")) server_c.AddRoute("/api", server.MsgHandler("demo", nil, "test-receiver"))
test_client := &client.Cfg{ test_client := &client.Cfg{
BaseCfg: b("test-client", core.Start), BaseCfg: b("test-client", core.DelayedStart(core.Start, 50)),
Url: ovr("http://localhost:8123/api", SERVER_URL), Url: ovr("http://localhost:8123/api", SERVER_URL),
} }
test_client.AddAction("demo", action.Base(client.Send)) test_client.AddAction("demo", action.Base(client.Send))

View file

@ -19,7 +19,7 @@ func TestScopesApp(tb *tbase.T) {
t.Run("send", SendTest) t.Run("send", SendTest)
t.Run("client", ClientTest) t.Run("client", ClientTest)
t.TearDownApp("") t.TearDownApp("")
t.AssertEqual(t.LogCheck("", true), 16) t.AssertEqual(t.LogCheck("", true), 18)
} }
func AppTest(t *testing.T) { func AppTest(t *testing.T) {