X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/baf7a940be8927278e3b4b87e7366cebfc16532f..4c199e7d93b6a4529ed5956807911b5f6c5489a7:/replay_test.go diff --git a/replay_test.go b/replay_test.go index 8dd54ab..e6b8daa 100644 --- a/replay_test.go +++ b/replay_test.go @@ -13,20 +13,32 @@ import ( "github.com/stretchr/testify/require" ) -// replayMessages is the length of the synthetic log. -const replayMessages = 100000 +const ( + // replayMessages is the length of the synthetic log. + replayMessages = 100000 + + // replayShortMessages is the length of the synthetic log with -short. + // It is long enough for batches of the chat view. + replayShortMessages = 5000 +) // TestReplaySyntheticLog runs a synthetic log through the memory core with the // fake summarizer. After each message it waits for the builds, as a turn does. // It checks that each view stays within its sawtooth, that no view line holds a // whole long message or a part of an unbuilt one, and that a restart in the -// middle and at the end loads the same views byte for byte. +// middle and at the end loads the same views byte for byte. With -short, the +// log is shorter, and the test still requires a batch of the chat view. func TestReplaySyntheticLog(t *testing.T) { start := time.Now() + messages := replayMessages + if testing.Short() { + messages = replayShortMessages + } + root := t.TempDir() clock := newTestClock() random := rand.New(rand.NewPCG(1, 2)) - memory, err := openMemory(t.Context(), root, clock.Now, newFakeSummarizer()) + memory, err := openMemory(t.Context(), root, clock.Now, newFakeSummarizer(), discardLog()) require.NoError(t, err) defer func() { @@ -34,10 +46,11 @@ func TestReplaySyntheticLog(t *testing.T) { }() batches := 0 + low := viewChatHigh - long := make([]bool, replayMessages) - for id := range replayMessages { - if id == replayMessages/2 { + long := make([]bool, messages) + for id := range messages { + if id == messages/2 { memory = replayRequireRestart(t, memory, root, clock) } @@ -58,17 +71,18 @@ func TestReplaySyntheticLog(t *testing.T) { } memory = replayRequireRestart(t, memory, root, clock) - require.Equal(t, replayMessages, memory.log.Len()) - require.Greater(t, batches, 0) + require.Equal(t, messages, memory.log.Len()) + require.Positive(t, batches) require.NoError(t, memory.Wait(t.Context())) - t.Logf("%d messages, %d nodes, %d batches in %s", replayMessages, memory.tree.Count(), batches, time.Since(start).Round(time.Second)) + t.Logf("%d messages, %d nodes, %d batches in %s", messages, memory.tree.Count(), batches, time.Since(start).Round(time.Second)) t.Logf("chat view: %d lines, %d bytes, lowest after a batch %d bytes", len(memory.chat.lines), memory.chat.Size(), low) t.Logf("compaction view: %d lines, %d bytes", len(memory.cview.lines), memory.cview.Size()) runtime.GC() var stats runtime.MemStats + runtime.ReadMemStats(&stats) t.Logf("heap in use: %d KB", stats.HeapInuse/1024) } @@ -86,7 +100,7 @@ func replayRequireRestart(t *testing.T, memory *Memory, root string, clock *Test require.NoError(t, err) require.NoError(t, memory.Close()) - memory, err = openMemory(t.Context(), root, clock.Now, newFakeSummarizer()) + memory, err = openMemory(t.Context(), root, clock.Now, newFakeSummarizer(), discardLog()) require.NoError(t, err) require.NoError(t, memory.Wait(t.Context())) require.Equal(t, view, memory.View()) @@ -198,6 +212,7 @@ func replayRequireOnlySummaries(t *testing.T, memory *Memory, long []bool, full } require.Equal(t, head+strings.ReplaceAll(node.Text, "\n", " "), rendered[n+1]) + if line.level == 0 && long[line.index] { require.True(t, strings.HasPrefix(node.Text, "summary "), "message %d", line.index) }