X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/2532718d5288e1a2586fe5716cc41bb2a0210e16..33aa709aa4e59e05ada6940a1c055f5e5646a418:/syncer_test.go?ds=sidebyside diff --git a/syncer_test.go b/syncer_test.go index 28a2597..09b2bb4 100644 --- a/syncer_test.go +++ b/syncer_test.go @@ -15,12 +15,12 @@ import ( "github.com/stretchr/testify/require" ) -// childDirVariable names the chat directory of the writer child process of -// TestKilledWriterLosesNoLoggedMessage. -const childDirVariable = "ONECHAT_TEST_CHILD_DIR" +// syncerChildDirVariable names the chat directory of the writer child process +// of TestKilledWriterLosesNoLoggedMessage. +const syncerChildDirVariable = "ONECHAT_TEST_CHILD_DIR" func TestFlushWaitsForASyncThatStartsAfterTheWrite(t *testing.T) { - gate := newGatedSync() + gate := newSyncerGatedSync() s := newSyncer(gate.Sync) s.Written() gate.WaitStarted(t, 1) @@ -48,7 +48,7 @@ func TestFlushWaitsForASyncThatStartsAfterTheWrite(t *testing.T) { } func TestOneSyncCoversTheWritesDuringTheSyncBeforeIt(t *testing.T) { - gate := newGatedSync() + gate := newSyncerGatedSync() s := newSyncer(gate.Sync) s.Written() gate.WaitStarted(t, 1) @@ -65,7 +65,7 @@ func TestOneSyncCoversTheWritesDuringTheSyncBeforeIt(t *testing.T) { } func TestFlushWithoutWritesNeedsNoSync(t *testing.T) { - gate := newGatedSync() + gate := newSyncerGatedSync() s := newSyncer(gate.Sync) require.NoError(t, s.Flush(t.Context())) require.NoError(t, s.Close()) @@ -98,7 +98,7 @@ func TestMemoryFlushSyncsTheChat(t *testing.T) { func TestKilledWriterLosesNoLoggedMessage(t *testing.T) { root := t.TempDir() for round := range 3 { - acknowledged := runKilledWriter(t, root, 200+100*round) + acknowledged := syncerRunKilledWriter(t, root, 200+100*round) memory, err := openMemory(t.Context(), root, time.Now, newFakeSummarizer()) require.NoError(t, err) @@ -106,8 +106,8 @@ func TestKilledWriterLosesNoLoggedMessage(t *testing.T) { for id := range memory.log.Len() { message, err := memory.log.Message(id) require.NoError(t, err) - require.Equal(t, writerKind(id), message.Kind) - require.Equal(t, writerText(id), message.Text) + require.Equal(t, syncerWriterKind(id), message.Kind) + require.Equal(t, syncerWriterText(id), message.Text) } require.NoError(t, memory.Close()) @@ -115,10 +115,11 @@ func TestKilledWriterLosesNoLoggedMessage(t *testing.T) { } // TestWriterChild is the child process of TestKilledWriterLosesNoLoggedMessage. -// It appends messages to the chat in the directory of childDirVariable and -// prints the ID of each message after its Append returns, until it is killed. +// It appends messages to the chat in the directory of syncerChildDirVariable +// and prints the ID of each message after its Append returns, until it is +// killed. func TestWriterChild(t *testing.T) { - root := os.Getenv(childDirVariable) + root := os.Getenv(syncerChildDirVariable) if root == "" { t.Skip("runs only as a child process") } @@ -127,7 +128,7 @@ func TestWriterChild(t *testing.T) { require.NoError(t, err) for id := memory.log.Len(); ; id++ { - _, err = memory.Append(writerKind(id), writerText(id)) + _, err = memory.Append(syncerWriterKind(id), syncerWriterText(id)) require.NoError(t, err) _, err = fmt.Println(id) @@ -135,22 +136,15 @@ func TestWriterChild(t *testing.T) { } } -// gatedSync is a sync function that blocks until the test releases it. -type gatedSync struct { +// SyncerGatedSync is a sync function that blocks until the test releases it. +type SyncerGatedSync struct { mutex sync.Mutex count int started chan struct{} release chan struct{} } -func newGatedSync() *gatedSync { - return &gatedSync{ - started: make(chan struct{}, 100), - release: make(chan struct{}), - } -} - -func (g *gatedSync) Sync() error { +func (g *SyncerGatedSync) Sync() error { g.mutex.Lock() g.count++ g.mutex.Unlock() @@ -162,7 +156,7 @@ func (g *gatedSync) Sync() error { } // WaitStarted waits until sync number n has started. -func (g *gatedSync) WaitStarted(t *testing.T, n int) { +func (g *SyncerGatedSync) WaitStarted(t *testing.T, n int) { t.Helper() require.Eventually(t, func() bool { @@ -171,26 +165,33 @@ func (g *gatedSync) WaitStarted(t *testing.T, n int) { } // Release lets the running sync end. -func (g *gatedSync) Release() { +func (g *SyncerGatedSync) Release() { <-g.started g.release <- struct{}{} } -func (g *gatedSync) Count() int { +func (g *SyncerGatedSync) Count() int { g.mutex.Lock() defer g.mutex.Unlock() return g.count } -// runKilledWriter starts the writer child on the chat in root, kills it with -// SIGKILL after it acknowledged count messages, and returns the number of +func newSyncerGatedSync() *SyncerGatedSync { + return &SyncerGatedSync{ + started: make(chan struct{}, 100), + release: make(chan struct{}), + } +} + +// syncerRunKilledWriter starts the writer child on the chat in root, kills it +// with SIGKILL after it acknowledged count messages, and returns the number of // messages that it acknowledged. -func runKilledWriter(t *testing.T, root string, count int) int { +func syncerRunKilledWriter(t *testing.T, root string, count int) int { t.Helper() command := exec.Command(os.Args[0], "-test.run=^TestWriterChild$") - command.Env = append(os.Environ(), childDirVariable+"="+root) + command.Env = append(os.Environ(), syncerChildDirVariable+"="+root) stdout, err := command.StdoutPipe() require.NoError(t, err) require.NoError(t, command.Start()) @@ -218,14 +219,14 @@ func runKilledWriter(t *testing.T, root string, count int) int { return acknowledged } -// writerKind returns the kind of message id of the writer child. -func writerKind(id int) string { +// syncerWriterKind returns the kind of message id of the writer child. +func syncerWriterKind(id int) string { return []string{"user", "tool", "echo", "reply"}[id%4] } -// writerText returns the text of message id of the writer child. Some texts -// are large, so a kill can come during the write of one line. -func writerText(id int) string { +// syncerWriterText returns the text of message id of the writer child. Some +// texts are large, so a kill can come during the write of one line. +func syncerWriterText(id int) string { size := 40 + id%7*30 if id%5 == 4 { size = 64*1024 + id