go-blockchain/metrics_test.go
Claude e296ab0651
Some checks failed
Security Scan / security (push) Successful in 13s
Test / Test (push) Failing after 33s
feat: atomic Metrics collector + concurrent safety test
Metrics tracks: blocks_processed, aliases_found, sync_errors, last_block_time.
All counters use atomic.Uint64 — concurrent-safe without mutex.

Tests: sequential counting + 100-goroutine concurrent stress test.

Co-Authored-By: Charon <charon@lethean.io>
2026-04-02 04:52:50 +01:00

47 lines
926 B
Go

package blockchain
import (
"testing"
"dappco.re/go/core/blockchain/chain"
store "dappco.re/go/core/store"
)
func TestMetrics_RecordBlock_Good(t *testing.T) {
s, _ := store.New(t.TempDir() + "/test.db")
defer s.Close()
m := NewMetrics(chain.New(s))
m.RecordBlock()
m.RecordBlock()
m.RecordBlock()
snap := m.Snapshot()
if snap["blocks_processed"] != 3 {
t.Errorf("blocks: got %d, want 3", snap["blocks_processed"])
}
}
func TestMetrics_Concurrent_Ugly(t *testing.T) {
s, _ := store.New(t.TempDir() + "/test.db")
defer s.Close()
m := NewMetrics(chain.New(s))
done := make(chan struct{})
for i := 0; i < 100; i++ {
go func() {
m.RecordBlock()
m.RecordAlias()
m.RecordSyncError()
done <- struct{}{}
}()
}
for i := 0; i < 100; i++ {
<-done
}
snap := m.Snapshot()
if snap["blocks_processed"] != 100 {
t.Errorf("concurrent blocks: got %d, want 100", snap["blocks_processed"])
}
}