validator
API
validator
packageAPI reference for the validator
package.
Imports
(9)
T
type
Mode
Mode defines validator behaviour.
v1/validator/validator.go:16-16
type Mode int
S
struct
Validator
Validator periodically compares cache and storage values.
v1/validator/validator.go:25-32
type Validator struct
Fields
| Name | Type | Description |
|---|---|---|
| cache | cache.Cache[T] | |
| store | adapter.Store[T] | |
| mode | Mode | |
| interval | time.Duration | |
| mismatches | uint64 | |
| digester | Digester[T] |
Uses
F
function
New
New creates a new Validator.
Parameters
Returns
*Validator[T]
v1/validator/validator.go:35-43
func New[T any](c cache.Cache[T], s adapter.Store[T], mode Mode, interval time.Duration) *Validator[T]
{
return &Validator[T]{
cache: c,
store: s,
mode: mode,
interval: interval,
digester: JSONDigester[T]{},
}
}
Uses
I
interface
Digester
Digester provides value serialization and hashing.
v1/validator/validator.go:111-113
type Digester interface
Methods
S
struct
JSONDigester
JSONDigester serializes values using JSON and hashes them with SHA256.
v1/validator/validator.go:116-116
type JSONDigester struct
F
function
TestValidatorAutoHeal
Parameters
t
v1/validator/validator_test.go:12-29
func TestValidatorAutoHeal(t *testing.T)
{
ctx := context.Background()
c := cache.NewInMemory[string]()
s := adapter.NewInMemoryStore[string]()
_ = s.Set(ctx, "k", "v1")
if err := c.Set(ctx, "k", "v0", 0); err != nil {
t.Fatalf("unexpected error: %v", err)
}
v := New[string](c, s, ModeAutoHeal, time.Millisecond)
go v.Run(ctx)
time.Sleep(5 * time.Millisecond)
if val, ok, err := c.Get(ctx, "k"); err != nil || !ok || val != "v1" {
t.Fatalf("expected cache healed to v1, got %v err %v", val, err)
}
if m := v.Metrics(); m == 0 {
t.Fatalf("expected mismatch metrics > 0")
}
}
S
struct
mockStore
v1/validator/validator_test.go:31-31
type mockStore struct
Methods
Get
Method
Parameters
ctx
context.Context
key
string
Returns
string
bool
error
func (*mockStore) Get(ctx context.Context, key string) (string, bool, error)
{
m.getCalled = true
return "", false, nil
}
Set
Method
Parameters
Returns
error
func (*mockStore) Set(ctx context.Context, key string, value string) error
{
return nil
}
Keys
Method
Parameters
ctx
context.Context
Returns
[]string
error
func (*mockStore) Keys(ctx context.Context) ([]string, error)
{
return []string{"k"}, nil
}
Fields
| Name | Type | Description |
|---|---|---|
| getCalled | bool |
F
function
TestValidatorScanCanceledContext
Parameters
t
v1/validator/validator_test.go:46-56
func TestValidatorScanCanceledContext(t *testing.T)
{
ctx, cancel := context.WithCancel(context.Background())
cancel()
c := cache.NewInMemory[string]()
s := &mockStore{}
v := New[string](c, s, ModeNoop, time.Millisecond)
v.scan(ctx)
if s.getCalled {
t.Fatalf("expected early exit on canceled context")
}
}
F
function
TestValidatorAlertMode
Parameters
t
v1/validator/validator_test.go:58-73
func TestValidatorAlertMode(t *testing.T)
{
ctx := context.Background()
c := cache.NewInMemory[string]()
s := adapter.NewInMemoryStore[string]()
_ = s.Set(ctx, "k", "v1")
_ = c.Set(ctx, "k", "v0", 0)
v := New[string](c, s, ModeAlert, time.Millisecond)
go v.Run(ctx)
time.Sleep(5 * time.Millisecond)
if val, _, _ := c.Get(ctx, "k"); val != "v0" {
t.Fatalf("cache should not heal in alert mode")
}
if m := v.Metrics(); m == 0 {
t.Fatalf("expected mismatch metrics")
}
}
F
function
TestValidatorScanAutoHeal
Parameters
t
v1/validator/validator_test.go:75-86
func TestValidatorScanAutoHeal(t *testing.T)
{
ctx := context.Background()
c := cache.NewInMemory[string]()
s := adapter.NewInMemoryStore[string]()
_ = s.Set(ctx, "k", "v1")
_ = c.Set(ctx, "k", "v0", 0)
v := New[string](c, s, ModeAutoHeal, time.Millisecond)
v.scan(ctx)
if val, ok, err := c.Get(ctx, "k"); err != nil || !ok || val != "v1" {
t.Fatalf("expected cache restored to v1, got %v err %v", val, err)
}
}
S
struct
mockDigester
v1/validator/validator_test.go:88-88
type mockDigester struct
Methods
Digest
Method
Parameters
v
string
Returns
string
error
func (*mockDigester) Digest(v string) (string, error)
{
m.calls++
return "mock", nil
}
Fields
| Name | Type | Description |
|---|---|---|
| calls | int |
F
function
TestValidatorSetDigester
Parameters
t
v1/validator/validator_test.go:95-108
func TestValidatorSetDigester(t *testing.T)
{
ctx := context.Background()
c := cache.NewInMemory[string]()
s := adapter.NewInMemoryStore[string]()
_ = s.Set(ctx, "k", "v1")
_ = c.Set(ctx, "k", "v1", 0)
v := New[string](c, s, ModeNoop, time.Millisecond)
md := &mockDigester{}
v.SetDigester(md)
v.scan(ctx)
if md.calls != 2 {
t.Fatalf("expected custom digester to be used twice, got %d", md.calls)
}
}