Files
mesh-delivery/modules/mesh-delivery-checker/internal/check/fakes_test.go
T
jschoubben e7888024a8
mesh/delivery merged: source.main moved to novox/mesh-delivery@1f6f4eda3208
mesh/delivery-group group fix/lost-check merged: every member is merged
mesh/check checking e7888024a826 on 769815ac0fea | novox/mesh-delivery#103 in group fix/lost-check
Checker: delete a check's asks key at its end, so the state asks holds only the checks that run
Re-review of fix/lost-check: writing an empty list at the end kept one key
for every verdict key ever checked. Tests: every row of the lost-ask table
proves the key gone at the end; one row more (deaf after its build module
replaced it, asked again by hand); counts unchanged.
2026-10-28 20:39:50 +01:00

71 lines
1.6 KiB
Go

package check
import (
"context"
"sort"
"sync"
)
// memKV is a bucket in memory with the revisions a real one gives.
type memKV struct {
mu sync.Mutex
vals map[string][]byte
revs map[string]uint64
next uint64
// creates counts the creates that succeeded, by key.
creates map[string]int
}
func newMemKV() *memKV {
return &memKV{vals: map[string][]byte{}, revs: map[string]uint64{}, creates: map[string]int{}}
}
func (m *memKV) Get(_ context.Context, key string) ([]byte, uint64, bool, error) {
m.mu.Lock()
defer m.mu.Unlock()
v, ok := m.vals[key]
return v, m.revs[key], ok, nil
}
func (m *memKV) Create(_ context.Context, key string, value []byte) (uint64, error) {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.vals[key]; ok {
return 0, ErrConflict
}
m.next++
m.vals[key], m.revs[key] = append([]byte(nil), value...), m.next
m.creates[key]++
return m.next, nil
}
func (m *memKV) Update(_ context.Context, key string, value []byte, revision uint64) (uint64, error) {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.vals[key]; !ok || m.revs[key] != revision {
return 0, ErrConflict
}
m.next++
m.vals[key], m.revs[key] = append([]byte(nil), value...), m.next
return m.next, nil
}
func (m *memKV) Keys(context.Context) ([]string, error) {
m.mu.Lock()
defer m.mu.Unlock()
keys := make([]string, 0, len(m.vals))
for k := range m.vals {
keys = append(keys, k)
}
sort.Strings(keys)
return keys, nil
}
func (m *memKV) Delete(_ context.Context, key string) error {
m.mu.Lock()
defer m.mu.Unlock()
delete(m.vals, key)
delete(m.revs, key)
return nil
}