import (
"bufio"
+ "context"
"errors"
"fmt"
"os"
}
func TestAFailedSyncFailsEachLaterFlush(t *testing.T) {
+ failure := errors.New("disk error")
s := newSyncer(func() error {
- return errors.New("disk error")
+ return failure
})
s.Written()
- require.ErrorContains(t, s.Flush(t.Context()), "disk error")
+ require.ErrorIs(t, s.Flush(t.Context()), failure)
s.Written()
- require.ErrorContains(t, s.Flush(t.Context()), "disk error")
- require.ErrorContains(t, s.Close(), "disk error")
+ require.ErrorIs(t, s.Flush(t.Context()), failure)
+ require.ErrorIs(t, s.Close(), failure)
+}
+
+func TestFlushCancellationKeepsTheDirectContextError(t *testing.T) {
+ gate := newSyncerGatedSync()
+ s := newSyncer(gate.Sync)
+ s.Written()
+ gate.WaitStarted(t, 1)
+
+ ctx, cancel := context.WithCancel(t.Context())
+ defer cancel()
+
+ cancel()
+
+ err := s.Flush(ctx)
+ require.Equal(t, context.Canceled, err)
+
+ gate.Release()
+ require.NoError(t, s.Close())
}
func TestMemoryFlushSyncsTheChat(t *testing.T) {
release chan struct{}
}
+func newSyncerGatedSync() *SyncerGatedSync {
+ return &SyncerGatedSync{
+ started: make(chan struct{}, 100),
+ release: make(chan struct{}),
+ }
+}
+
func (g *SyncerGatedSync) Sync() error {
g.mutex.Lock()
return g.count
}
-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.