2023-10-27 19:30:28 +00:00
|
|
|
package diagnostics_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ledgerwatch/erigon-lib/diagnostics"
|
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type testInfo struct {
|
|
|
|
count int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ti testInfo) Type() diagnostics.Type {
|
|
|
|
return diagnostics.TypeOf(ti)
|
|
|
|
}
|
|
|
|
|
|
|
|
type testProvider struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *testProvider) StartDiagnostics(ctx context.Context) error {
|
|
|
|
timer := time.NewTicker(1 * time.Second)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
var count int
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case <-timer.C:
|
2023-11-07 08:48:58 +00:00
|
|
|
diagnostics.Send(testInfo{count})
|
2023-10-27 19:30:28 +00:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestProviderRegistration(t *testing.T) {
|
|
|
|
|
|
|
|
// diagnostics provider
|
|
|
|
provider := &testProvider{}
|
|
|
|
diagnostics.RegisterProvider(provider, diagnostics.TypeOf(testInfo{}), log.Root())
|
|
|
|
|
|
|
|
// diagnostics receiver
|
|
|
|
ctx, ch, cancel := diagnostics.Context[testInfo](context.Background(), 1)
|
|
|
|
diagnostics.StartProviders(ctx, diagnostics.TypeOf(testInfo{}), log.Root())
|
|
|
|
|
|
|
|
for info := range ch {
|
|
|
|
if info.count == 3 {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-07 08:48:58 +00:00
|
|
|
func TestDelayedProviderRegistration(t *testing.T) {
|
|
|
|
|
|
|
|
time.AfterFunc(1*time.Second, func() {
|
|
|
|
// diagnostics provider
|
|
|
|
provider := &testProvider{}
|
|
|
|
diagnostics.RegisterProvider(provider, diagnostics.TypeOf(testInfo{}), log.Root())
|
|
|
|
})
|
|
|
|
|
|
|
|
// diagnostics receiver
|
|
|
|
ctx, ch, cancel := diagnostics.Context[testInfo](context.Background(), 1)
|
|
|
|
diagnostics.StartProviders(ctx, diagnostics.TypeOf(testInfo{}), log.Root())
|
|
|
|
|
|
|
|
for info := range ch {
|
|
|
|
if info.count == 3 {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-27 19:30:28 +00:00
|
|
|
func TestProviderFuncRegistration(t *testing.T) {
|
|
|
|
|
|
|
|
// diagnostics provider
|
|
|
|
diagnostics.RegisterProvider(diagnostics.ProviderFunc(func(ctx context.Context) error {
|
|
|
|
timer := time.NewTicker(1 * time.Second)
|
|
|
|
defer timer.Stop()
|
|
|
|
|
|
|
|
var count int
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case <-timer.C:
|
2023-11-07 08:48:58 +00:00
|
|
|
diagnostics.Send(testInfo{count})
|
2023-10-27 19:30:28 +00:00
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}), diagnostics.TypeOf(testInfo{}), log.Root())
|
|
|
|
|
|
|
|
// diagnostics receiver
|
|
|
|
ctx, ch, cancel := diagnostics.Context[testInfo](context.Background(), 1)
|
|
|
|
|
|
|
|
diagnostics.StartProviders(ctx, diagnostics.TypeOf(testInfo{}), log.Root())
|
|
|
|
|
|
|
|
for info := range ch {
|
|
|
|
if info.count == 3 {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|