provide DelayedStart() (proof-of-concept): parameterized handler (factory)
This commit is contained in:
parent
2286aef4ce
commit
a67c31e0b5
4 changed files with 16 additions and 2 deletions
12
core/core.go
12
core/core.go
|
@ -2,6 +2,7 @@ package core
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/config"
|
||||
|
@ -16,6 +17,17 @@ func Start(ctx lib.Context) {
|
|||
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) {
|
||||
step := ctx.Config().Step()
|
||||
for step(ctx) {
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
lib "git.sr.ht/~cco/go-scopes"
|
||||
"git.sr.ht/~cco/go-scopes/core"
|
||||
"git.sr.ht/~cco/go-scopes/core/message"
|
||||
"git.sr.ht/~cco/go-scopes/logging"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
|
@ -44,6 +45,7 @@ func Serve(ctx lib.Context) {
|
|||
}
|
||||
srv := &http.Server{Addr: cfg.Addr, Handler: r}
|
||||
ctx.WithState(&ServerState{server: srv})
|
||||
logging.Debug(ctx).Str("addr", cfg.Addr).Msg("server.Serve")
|
||||
lib.RunCtx(ctx, func(ctx lib.Context) {
|
||||
srv.ListenAndServe()
|
||||
})
|
||||
|
|
|
@ -35,7 +35,7 @@ func Config() lib.Config {
|
|||
server_c.AddRoute("/api", server.MsgHandler("demo", nil, "test-receiver"))
|
||||
|
||||
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),
|
||||
}
|
||||
test_client.AddAction("demo", action.Base(client.Send))
|
||||
|
|
|
@ -19,7 +19,7 @@ func TestScopesApp(tb *tbase.T) {
|
|||
t.Run("send", SendTest)
|
||||
t.Run("client", ClientTest)
|
||||
t.TearDownApp("")
|
||||
t.AssertEqual(t.LogCheck("", true), 16)
|
||||
t.AssertEqual(t.LogCheck("", true), 18)
|
||||
}
|
||||
|
||||
func AppTest(t *testing.T) {
|
||||
|
|
Loading…
Add table
Reference in a new issue