]> git.giacomolli.uk - onechat/blobdiff - syncer_test.go
Share general review and documentation rules
[onechat] / syncer_test.go
index efa174ab3a21cd43a4836bc64ab1a7a31e124115..65a96725ee02829ac9e4c94f954cc5423562ca5e 100644 (file)
@@ -2,6 +2,7 @@ package main
 
 import (
        "bufio"
+       "context"
        "errors"
        "fmt"
        "os"
@@ -28,6 +29,7 @@ func TestFlushWaitsForASyncThatStartsAfterTheWrite(t *testing.T) {
        // This write comes during the first sync, so that sync does not cover
        // it.
        s.Written()
+
        flushed := make(chan error)
        go func() {
                flushed <- s.Flush(t.Context())
@@ -35,6 +37,7 @@ func TestFlushWaitsForASyncThatStartsAfterTheWrite(t *testing.T) {
 
        gate.Release()
        gate.WaitStarted(t, 2)
+
        select {
        case err := <-flushed:
                t.Fatalf("flush returned before the second sync: %v", err)
@@ -52,6 +55,7 @@ func TestOneSyncCoversTheWritesDuringTheSyncBeforeIt(t *testing.T) {
        s := newSyncer(gate.Sync)
        s.Written()
        gate.WaitStarted(t, 1)
+
        for range 10 {
                s.Written()
        }
@@ -73,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) {
@@ -103,6 +126,7 @@ func TestKilledWriterLosesNoLoggedMessage(t *testing.T) {
                memory, err := openMemory(t.Context(), root, time.Now, newFakeSummarizer(), discardLog())
                require.NoError(t, err)
                require.GreaterOrEqual(t, memory.log.Len(), acknowledged)
+
                for id := range memory.log.Len() {
                        message, err := memory.log.Message(id)
                        require.NoError(t, err)
@@ -144,12 +168,21 @@ 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()
+
        g.count++
        g.mutex.Unlock()
 
        g.started <- struct{}{}
+
        <-g.release
 
        return nil
@@ -167,6 +200,7 @@ func (g *SyncerGatedSync) WaitStarted(t *testing.T, n int) {
 // Release lets the running sync end.
 func (g *SyncerGatedSync) Release() {
        <-g.started
+
        g.release <- struct{}{}
 }
 
@@ -177,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.
@@ -206,6 +233,7 @@ func syncerRunKilledWriter(t *testing.T, root string, count int) int {
        }
 
        require.NoError(t, command.Process.Kill())
+
        for scanner.Scan() {
                _, err = strconv.Atoi(strings.TrimSpace(scanner.Text()))
                if err == nil {