From fa178d7c26da611e806d7c73e3b4424595ecf392 Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 21 Jun 2023 13:50:50 +0500 Subject: [PATCH 1/2] Add app.IterateComponents method. This method helps to create debugging HTTP handlers in Heart --- app/app.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/app.go b/app/app.go index 21f2f52c..2295cdbf 100644 --- a/app/app.go +++ b/app/app.go @@ -262,6 +262,14 @@ func (app *App) Start(ctx context.Context) (err error) { return } +func (app *App) IterateComponents(fn func(Component)) { + app.mu.RLock() + defer app.mu.RUnlock() + for _, s := range app.components { + fn(s) + } +} + func stackAllGoroutines() []byte { buf := make([]byte, 1024) for { From 4eb2245669887ca51304a72a942eaa22fd5ede31 Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 21 Jun 2023 17:54:38 +0500 Subject: [PATCH 2/2] IterateComponents: Add test and comment --- app/app.go | 1 + app/app_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/app/app.go b/app/app.go index 2295cdbf..5aeda7f3 100644 --- a/app/app.go +++ b/app/app.go @@ -262,6 +262,7 @@ func (app *App) Start(ctx context.Context) (err error) { return } +// IterateComponents iterates over all registered components. It's safe for concurrent use. func (app *App) IterateComponents(fn func(Component)) { app.mu.RLock() defer app.mu.RUnlock() diff --git a/app/app_test.go b/app/app_test.go index 0c122b24..5882feab 100644 --- a/app/app_test.go +++ b/app/app_test.go @@ -55,6 +55,21 @@ func TestAppServiceRegistry(t *testing.T) { }) } +func TestApp_IterateComponents(t *testing.T) { + app := new(App) + + app.Register(newTestService(testTypeRunnable, "c1", nil, nil)) + app.Register(newTestService(testTypeRunnable, "r1", nil, nil)) + app.Register(newTestService(testTypeComponent, "s1", nil, nil)) + + var got []string + app.IterateComponents(func(s Component) { + got = append(got, s.Name()) + }) + + assert.ElementsMatch(t, []string{"c1", "r1", "s1"}, got) +} + func TestAppStart(t *testing.T) { t.Run("SuccessStartStop", func(t *testing.T) { app := new(App)