X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/522d7fd117285e4e4ce3ea5ec9b5a1722521b681..4a42f7f208cbfead335eba9ebe615b35b1742130:/syncer_test.go diff --git a/syncer_test.go b/syncer_test.go index 16f3345..65a9672 100644 --- a/syncer_test.go +++ b/syncer_test.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "context" "errors" "fmt" "os" @@ -76,16 +77,35 @@ func TestFlushWithoutWritesNeedsNoSync(t *testing.T) { } 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) { @@ -148,6 +168,13 @@ type SyncerGatedSync struct { 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() @@ -184,13 +211,6 @@ func (g *SyncerGatedSync) Count() int { 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.