15 "github.com/stretchr/testify/require"
18 // syncerChildDirVariable names the chat directory of the writer child process
19 // of TestKilledWriterLosesNoLoggedMessage.
20 const syncerChildDirVariable = "ONECHAT_TEST_CHILD_DIR"
22 func TestFlushWaitsForASyncThatStartsAfterTheWrite(t *testing.T) {
23 gate := newSyncerGatedSync()
24 s := newSyncer(gate.Sync)
26 gate.WaitStarted(t, 1)
28 // This write comes during the first sync, so that sync does not cover
32 flushed := make(chan error)
34 flushed <- s.Flush(t.Context())
38 gate.WaitStarted(t, 2)
41 case err := <-flushed:
42 t.Fatalf("flush returned before the second sync: %v", err)
43 case <-time.After(20 * time.Millisecond):
47 require.NoError(t, <-flushed)
48 require.NoError(t, s.Close())
49 require.Equal(t, 2, gate.Count())
52 func TestOneSyncCoversTheWritesDuringTheSyncBeforeIt(t *testing.T) {
53 gate := newSyncerGatedSync()
54 s := newSyncer(gate.Sync)
56 gate.WaitStarted(t, 1)
63 gate.WaitStarted(t, 2)
65 require.NoError(t, s.Flush(t.Context()))
66 require.Equal(t, 2, gate.Count())
67 require.NoError(t, s.Close())
70 func TestFlushWithoutWritesNeedsNoSync(t *testing.T) {
71 gate := newSyncerGatedSync()
72 s := newSyncer(gate.Sync)
73 require.NoError(t, s.Flush(t.Context()))
74 require.NoError(t, s.Close())
75 require.Equal(t, 0, gate.Count())
78 func TestAFailedSyncFailsEachLaterFlush(t *testing.T) {
79 failure := errors.New("disk error")
80 s := newSyncer(func() error {
85 require.ErrorIs(t, s.Flush(t.Context()), failure)
88 require.ErrorIs(t, s.Flush(t.Context()), failure)
89 require.ErrorIs(t, s.Close(), failure)
92 func TestMemoryFlushSyncsTheChat(t *testing.T) {
93 memory, err := openMemory(t.Context(), t.TempDir(), newTestClock().Now, newFakeSummarizer(), discardLog())
94 require.NoError(t, err)
96 _, err = memory.Append("tool", "shell: ls")
97 require.NoError(t, err)
98 require.NoError(t, memory.Flush(t.Context()))
99 require.NoError(t, memory.Close())
102 func TestKilledWriterLosesNoLoggedMessage(t *testing.T) {
104 for round := range 3 {
105 acknowledged := syncerRunKilledWriter(t, root, 200+100*round)
107 memory, err := openMemory(t.Context(), root, time.Now, newFakeSummarizer(), discardLog())
108 require.NoError(t, err)
109 require.GreaterOrEqual(t, memory.log.Len(), acknowledged)
111 for id := range memory.log.Len() {
112 message, err := memory.log.Message(id)
113 require.NoError(t, err)
114 require.Equal(t, syncerWriterKind(id), message.Kind)
115 require.Equal(t, syncerWriterText(id), message.Text)
118 require.NoError(t, memory.Close())
122 // TestWriterChild is the child process of TestKilledWriterLosesNoLoggedMessage.
123 // It appends messages to the chat in the directory of syncerChildDirVariable
124 // and prints the ID of each message after its Append returns, until it is
126 func TestWriterChild(t *testing.T) {
127 root := os.Getenv(syncerChildDirVariable)
129 t.Skip("runs only as a child process")
132 memory, err := openMemory(t.Context(), root, time.Now, newFakeSummarizer(), discardLog())
133 require.NoError(t, err)
135 for id := memory.log.Len(); ; id++ {
136 _, err = memory.Append(syncerWriterKind(id), syncerWriterText(id))
137 require.NoError(t, err)
139 _, err = fmt.Println(id)
140 require.NoError(t, err)
144 // SyncerGatedSync is a sync function that blocks until the test releases it.
145 type SyncerGatedSync struct {
148 started chan struct{}
149 release chan struct{}
152 func newSyncerGatedSync() *SyncerGatedSync {
153 return &SyncerGatedSync{
154 started: make(chan struct{}, 100),
155 release: make(chan struct{}),
159 func (g *SyncerGatedSync) Sync() error {
165 g.started <- struct{}{}
172 // WaitStarted waits until sync number n has started.
173 func (g *SyncerGatedSync) WaitStarted(t *testing.T, n int) {
176 require.Eventually(t, func() bool {
177 return g.Count() >= n
178 }, time.Second, time.Millisecond)
181 // Release lets the running sync end.
182 func (g *SyncerGatedSync) Release() {
185 g.release <- struct{}{}
188 func (g *SyncerGatedSync) Count() int {
190 defer g.mutex.Unlock()
195 // syncerRunKilledWriter starts the writer child on the chat in root, kills it
196 // with SIGKILL after it acknowledged count messages, and returns the number of
197 // messages that it acknowledged.
198 func syncerRunKilledWriter(t *testing.T, root string, count int) int {
201 command := exec.Command(os.Args[0], "-test.run=^TestWriterChild$")
202 command.Env = append(os.Environ(), syncerChildDirVariable+"="+root)
203 stdout, err := command.StdoutPipe()
204 require.NoError(t, err)
205 require.NoError(t, command.Start())
208 scanner := bufio.NewScanner(stdout)
209 for acknowledged < count && scanner.Scan() {
210 _, err = strconv.Atoi(strings.TrimSpace(scanner.Text()))
216 require.NoError(t, command.Process.Kill())
219 _, err = strconv.Atoi(strings.TrimSpace(scanner.Text()))
225 require.Error(t, command.Wait())
226 require.Equal(t, count, min(acknowledged, count), "the child stopped early")
231 // syncerWriterKind returns the kind of message id of the writer child.
232 func syncerWriterKind(id int) string {
233 return []string{"user", "tool", "echo", "reply"}[id%4]
236 // syncerWriterText returns the text of message id of the writer child. Some
237 // texts are large, so a kill can come during the write of one line.
238 func syncerWriterText(id int) string {
244 return strings.Repeat(strconv.Itoa(id%10), size)