From b8b26de90151359331ae47a09d3b65b2b733b34b Mon Sep 17 00:00:00 2001 From: Saxon Date: Wed, 13 Mar 2019 00:49:25 +1030 Subject: [PATCH] revid: added TestMultiSenderFailRetry test Added test to check that if a sender is set to retry on send fails, it will keep trying to send until it is successful, and have all data that was intended to be written sent off. --- revid/senders_test.go | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/revid/senders_test.go b/revid/senders_test.go index 215b5a20..392dc8ed 100644 --- a/revid/senders_test.go +++ b/revid/senders_test.go @@ -501,3 +501,50 @@ func TestMultiSenderFailNoRetry(t *testing.T) { } } } + +// TestMultiSenderFailRetry checks that a if a sender is set to retry on failed +// sends, that it does so repeatedly until it can successfully send. +func TestMultiSenderFailRetry(t *testing.T) { + // NB: This is only being tested with one sender - this is AusOcean's use case. + senders := []loadSender{newDummyLoadSender(false, true)} + ms, err := newMultiSender(senders, func() bool { return true }) + if err != nil { + t.Fatalf("Unexpected error: %v", err) + } + + // Perform one write with successful send. + ms.Write([]byte{0x00}) + + // Now cause sender to fail on next write. + failedSender := ms.senders[0].(*dummyLoadSender) + failedSender.failOnSend = true + + // Wrap next write in a routine. It will keep trying to send until we set + // failOnSend to false. + done := false + go func() { + ms.Write([]byte{0x01}) + done = true + }() + + // Now set failOnSend to false. + failedSender.mu.Lock() + failedSender.failOnSend = false + failedSender.mu.Unlock() + + // Sleep and then check that we've successfully returned from the write. + time.Sleep(10 * time.Millisecond) + if done != true { + t.Fatal("did not exit write when send was successful") + } + + // Write on last time. + ms.Write([]byte{0x02}) + + // Check that all the data is there. + got := failedSender.buf + want := [][]byte{{0x00}, {0x01}, {0x02}} + if !reflect.DeepEqual(got, want) { + t.Errorf("sender did not send expected data. \nGot: %v\nWant: %v\n", got, want) + } +}