Fold H2's new tests into existing ones: go/ at 327 unit tests (from 331), TypeScript at 48 (from 49)
The eight Go test functions H2 added become subtests of existing tests (catalogue's state test, the bundle's state test, the views test, the process state test, the testbus bus test, the seal byte-for-byte test), and four more are folded beside them (two seal, two process watch); the two TypeScript seal tests become one, and two more TypeScript tests are folded into their neighbours. No assertion is lost. Builds: design 47 H2 §819
This commit is contained in:
@@ -2,10 +2,6 @@ package process
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go/jetstream"
|
||||
|
||||
@@ -64,54 +60,3 @@ func (f *pruneKV) Purge(ctx context.Context, key string, _ ...jetstream.KVDelete
|
||||
type putEntry struct{ fakeEntry }
|
||||
|
||||
func (putEntry) Operation() jetstream.KeyValueOp { return jetstream.KeyValuePut }
|
||||
|
||||
// Design 47 H2 §812 (H-A-380, H-A-386): a process's write that takes its state past prune-mark prunes it, oldest first
|
||||
// by revision, to prune-target, in one journal line, reading the figures only at the bind and after the prune; a key
|
||||
// kept under _keep/<key> survives, and no reader counts the keep.
|
||||
func TestAProcessWritePastItsMarkPrunesToItsTargetAndAKeptKeySurvives(t *testing.T) {
|
||||
var lines []string
|
||||
sayPrune = func(line string) { lines = append(lines, line) }
|
||||
t.Cleanup(func() { sayPrune = func(string) {} })
|
||||
kv := &pruneKV{fakeKV: newFakeKV(), cap: 10_000}
|
||||
s, err := stateOn(kv).Pruned(stdio.Prune{Mark: 70, Target: 50})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value := strings.Repeat("x", 300)
|
||||
if _, err := s.Put("k00", value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Keep("k00", time.Now().Add(time.Hour)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Keep("k01", time.Now().Add(-time.Hour)); err == nil {
|
||||
t.Fatal("a keep until a time past was taken")
|
||||
}
|
||||
for i := 1; i < 18; i++ {
|
||||
if _, err := s.Put(fmt.Sprintf("k%02d", i), value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(lines) == 0 && kv.held()*100 > 70*10_000 {
|
||||
t.Fatalf("the state holds %d of 10000 bytes after k%02d and was not pruned", kv.held(), i)
|
||||
}
|
||||
}
|
||||
if len(lines) != 1 || !strings.Contains(lines[0], "pruned the state mesh-mail's sends") {
|
||||
t.Fatalf("the prunes said %q, want one line", lines)
|
||||
}
|
||||
if kv.statuss != 2 {
|
||||
t.Fatalf("the figures were read %d times, want 2: at the bind and after the prune", kv.statuss)
|
||||
}
|
||||
if held := kv.held(); held*100 > 50*10_000 {
|
||||
t.Fatalf("the prune left %d of 10000 bytes, above its target of 50 %%", held)
|
||||
}
|
||||
keys, err := s.Keys()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if keys[0] != "k00" || strings.Contains(strings.Join(keys, " "), "_keep") || keys[1] == "k01" || keys[len(keys)-1] != "k17" {
|
||||
t.Fatalf("after the prune the readers see %v: want k00 kept, the oldest others gone, k17 there, no keep", keys)
|
||||
}
|
||||
if e, _ := s.Get(stdio.KeepKey("k00")); e == nil {
|
||||
t.Fatal("the keep itself was pruned")
|
||||
}
|
||||
}
|
||||
|
||||
+67
-18
@@ -3,6 +3,7 @@ package process
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
@@ -250,6 +251,56 @@ func TestStatePutWritesAKeyWhole(t *testing.T) {
|
||||
t.Errorf("seat %q, ask %q was taken", bad[0], bad[1])
|
||||
}
|
||||
}
|
||||
// Design 47 H2 §812 (H-A-380, H-A-386): a process's write that takes its state past prune-mark prunes it, oldest first
|
||||
// by revision, to prune-target, in one journal line, reading the figures only at the bind and after the prune; a key
|
||||
// kept under _keep/<key> survives, and no reader counts the keep.
|
||||
t.Run("a write past its mark prunes to its target and a kept key survives", func(t *testing.T) {
|
||||
var lines []string
|
||||
sayPrune = func(line string) { lines = append(lines, line) }
|
||||
t.Cleanup(func() { sayPrune = func(string) {} })
|
||||
kv := &pruneKV{fakeKV: newFakeKV(), cap: 10_000}
|
||||
s, err := stateOn(kv).Pruned(stdio.Prune{Mark: 70, Target: 50})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
value := strings.Repeat("x", 300)
|
||||
if _, err := s.Put("k00", value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Keep("k00", time.Now().Add(time.Hour)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.Keep("k01", time.Now().Add(-time.Hour)); err == nil {
|
||||
t.Fatal("a keep until a time past was taken")
|
||||
}
|
||||
for i := 1; i < 18; i++ {
|
||||
if _, err := s.Put(fmt.Sprintf("k%02d", i), value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(lines) == 0 && kv.held()*100 > 70*10_000 {
|
||||
t.Fatalf("the state holds %d of 10000 bytes after k%02d and was not pruned", kv.held(), i)
|
||||
}
|
||||
}
|
||||
if len(lines) != 1 || !strings.Contains(lines[0], "pruned the state mesh-mail's sends") {
|
||||
t.Fatalf("the prunes said %q, want one line", lines)
|
||||
}
|
||||
if kv.statuss != 2 {
|
||||
t.Fatalf("the figures were read %d times, want 2: at the bind and after the prune", kv.statuss)
|
||||
}
|
||||
if held := kv.held(); held*100 > 50*10_000 {
|
||||
t.Fatalf("the prune left %d of 10000 bytes, above its target of 50 %%", held)
|
||||
}
|
||||
keys, err := s.Keys()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if keys[0] != "k00" || strings.Contains(strings.Join(keys, " "), "_keep") || keys[1] == "k01" || keys[len(keys)-1] != "k17" {
|
||||
t.Fatalf("after the prune the readers see %v: want k00 kept, the oldest others gone, k17 there, no keep", keys)
|
||||
}
|
||||
if e, _ := s.Get(stdio.KeepKey("k00")); e == nil {
|
||||
t.Fatal("the keep itself was pruned")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 1840/S1: given a state, when its keys are listed, then they come sorted, and none is an empty list, not an error.
|
||||
@@ -321,25 +372,23 @@ func TestStateWatchHandsCurrentValuesThenChanges(t *testing.T) {
|
||||
t.Fatalf("no change %+v", want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1840/S2: a key narrowing reaches the bus as a subject pattern: `*` one name, `**` the rest, empty every key.
|
||||
func TestStateWatchNarrowing(t *testing.T) {
|
||||
for key, want := range map[string]string{"": ">", "a.b": "a.b", "a.*": "a.*", "a.**": "a.>", "**": ">"} {
|
||||
if got := watchPattern(key); got != want {
|
||||
t.Errorf("watchPattern(%q) = %q, want %q", key, got, want)
|
||||
// 1840/S2: a key narrowing reaches the bus as a subject pattern: `*` one name, `**` the rest, empty every key.
|
||||
t.Run("narrowing", func(t *testing.T) {
|
||||
for key, want := range map[string]string{"": ">", "a.b": "a.b", "a.*": "a.*", "a.**": "a.>", "**": ">"} {
|
||||
if got := watchPattern(key); got != want {
|
||||
t.Errorf("watchPattern(%q) = %q, want %q", key, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1840/S2: given a handler that refuses a current value, then Watch returns its error.
|
||||
func TestStateWatchReturnsAHandlersErrorOnCurrentValues(t *testing.T) {
|
||||
kv := newFakeKV()
|
||||
kv.initial = []jetstream.KeyValueEntry{watchEntry{fakeEntry{key: "a", value: []byte(`1`), rev: 1}, jetstream.KeyValuePut}}
|
||||
boom := errors.New("boom")
|
||||
if err := stateOn(kv).Watch("", func(stdio.StateChange) error { return boom }); !errors.Is(err, boom) {
|
||||
t.Fatalf("Watch: %v, want the handler's error", err)
|
||||
}
|
||||
})
|
||||
// 1840/S2: given a handler that refuses a current value, then Watch returns its error.
|
||||
t.Run("a handler's error on the current values is returned", func(t *testing.T) {
|
||||
kv := newFakeKV()
|
||||
kv.initial = []jetstream.KeyValueEntry{watchEntry{fakeEntry{key: "a", value: []byte(`1`), rev: 1}, jetstream.KeyValuePut}}
|
||||
boom := errors.New("boom")
|
||||
if err := stateOn(kv).Watch("", func(stdio.StateChange) error { return boom }); !errors.Is(err, boom) {
|
||||
t.Fatalf("Watch: %v, want the handler's error", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 1840/S2: given a connection that closes before the current values arrive, then Watch returns an error.
|
||||
|
||||
Reference in New Issue
Block a user