erigon-pulse/erigon-lib/diagnostics/provider_test.go
Dmytro 9adf31b8eb
bytes transfet separated by capability and category (#8568)
Co-authored-by: Mark Holt <mark@distributed.vision>
2023-10-27 22:30:28 +03:00

88 lines
1.7 KiB
Go

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:
diagnostics.Send(ctx, testInfo{count})
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()
}
}
}
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:
diagnostics.Send(ctx, testInfo{count})
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()
}
}
}