X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/0da50dc187c8b4aa23ac1ddd0418c0da779389d9..HEAD:/whatsapp_image_test.go diff --git a/whatsapp_image_test.go b/whatsapp_image_test.go index f00bc3a..75a95dc 100644 --- a/whatsapp_image_test.go +++ b/whatsapp_image_test.go @@ -15,6 +15,7 @@ import ( "path/filepath" "strings" "testing" + "testing/synctest" "time" "github.com/stretchr/testify/require" @@ -79,28 +80,58 @@ func TestWhatsAppImagesAreDurableBeforeAcknowledgement(t *testing.T) { } func TestWhatsAppImageDownloadCanOutlastNameLookup(t *testing.T) { - whatsapp, fake := whatsappStartTestLinked(t) - whatsapp.client = fake - whatsapp.inbox.lookupTime = 10 * time.Millisecond - whatsapp.inbox.imageTime = time.Second - fake.downloadWait = 50 * time.Millisecond - data := imageTestPNG(t, 3, 2) - fake.downloadData = data + synctest.Test(t, func(t *testing.T) { + whatsapp, fake := whatsappStartTestLinked(t) + whatsapp.client = fake + fake.downloadWait = whatsappContactLookupTimeout + time.Second + data := imageTestPNG(t, 3, 2) + fake.downloadData = data + + require.True(t, whatsapp.handle(whatsappImageTestEvent(whatsappTestAna, "Slow photo."))) + + restored := whatsappTestState(t, whatsapp.state.path) + require.NoError(t, restored.CloseAll()) + require.Len(t, restored.Closed(), 1) + + text := restored.Closed()[0].Messages[0].Text + names := imageFiles(text) + require.Len(t, names, 1) + require.Equal(t, imageText(names, "Slow photo."), text) + + saved, err := whatsapp.inbox.images.Read(names[0]) + require.NoError(t, err) + require.Equal(t, data, saved) + }) +} - require.True(t, whatsapp.handle(whatsappImageTestEvent(whatsappTestAna, "Slow photo."))) +func TestWhatsAppRunCancellationEndsAnImageDownload(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + test := whatsappRetryTest(t) + test.fake.downloadWait = time.Hour + test.start(t) + synctest.Wait() - restored := whatsappTestState(t, whatsapp.state.path) - require.NoError(t, restored.CloseAll()) - require.Len(t, restored.Closed(), 1) + acknowledged := make(chan bool, 1) + go func() { + acknowledged <- test.fake.Emit(whatsappImageTestEvent(whatsappTestAna, "Keep this caption.")) + }() - text := restored.Closed()[0].Messages[0].Text - names := imageFiles(text) - require.Len(t, names, 1) - require.Equal(t, imageText(names, "Slow photo."), text) + synctest.Wait() - saved, err := whatsapp.inbox.images.Read(names[0]) - require.NoError(t, err) - require.Equal(t, data, saved) + select { + case <-acknowledged: + t.Fatal("download ended before daemon cancellation") + default: + } + + test.cancel() + require.True(t, <-acknowledged) + require.NoError(t, <-test.done) + require.Empty(t, test.fake.Downloads()) + require.Contains(t, test.logs.String(), "context canceled") + require.NoError(t, test.whatsapp.state.CloseAll()) + require.Equal(t, "(image not downloaded)\nKeep this caption.", test.whatsapp.state.Closed()[0].Messages[0].Text) + }) } func TestWhatsAppImageFailureKeepsTheBatchMoving(t *testing.T) {