revid: added TestMultiSenderFailNoRetry

This commit is contained in:
Saxon 2019-03-12 23:16:32 +10:30
parent 500edc05aa
commit 404190ca52
1 changed files with 60 additions and 1 deletions

View File

@ -31,6 +31,7 @@ package revid
import ( import (
"errors" "errors"
"fmt" "fmt"
"reflect"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -300,6 +301,7 @@ func (s *dummyLoadSender) send() error {
s.buf = append(s.buf, s.data) s.buf = append(s.buf, s.data)
return nil return nil
} }
s.failHandled = false
return errSendFailed return errSendFailed
} }
@ -441,5 +443,62 @@ func TestMultiSenderNotActiveRetry(t *testing.T) {
} }
} }
// TestMultiSenderFailNoRetry checks that behaviour is as expected when a sender
// fails at a send and does not retry.
func TestMultiSenderFailNoRetry(t *testing.T) {
senders := []loadSender{
newDummyLoadSender(false, false),
newDummyLoadSender(false, false),
newDummyLoadSender(false, false),
}
ms, err := newMultiSender(senders, func() bool { return true })
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// We will perform two writes. We expect the second write not to be complete,
// i.e. the senders should not send anything on this write.
ms.Write([]byte{0x00})
// Make second sender fail a send.
const failedSenderIdx = 1
failedSender := ms.senders[failedSenderIdx].(*dummyLoadSender)
failedSender.failOnSend = true
ms.Write([]byte{0x01})
// Check that handleSendFail was called.
if !failedSender.failHandled {
t.Fatal("the failed send was not handled")
}
// Now for next send we don't want to fail.
failedSender.failOnSend = false
ms.Write([]byte{0x02})
// Check number of slices sent for each sender and also check data.
for i, sender := range ms.senders {
// First check number of slices sent for each sender.
wantLen := 3
if i == failedSenderIdx {
wantLen = 2
}
curSender := sender.(*dummyLoadSender)
gotLen := len(curSender.buf)
if gotLen != wantLen {
t.Errorf("len of sender that failed is not expected: \nGot: %v\nWant: %v\n", gotLen, wantLen)
}
// Now check the quality of the data.
wantData := [][]byte{{0x00}, {0x01}, {0x02}}
if i == failedSenderIdx {
wantData = [][]byte{{0x00}, {0x02}}
}
gotData := curSender.buf
if !reflect.DeepEqual(gotData, wantData) {
t.Errorf("unexpect data sent through sender idx: %v. \nGot: %v\nWant: %v\n", i, gotData, wantData)
}
}
}
// TODO: test that send retry works // TODO: test that send retry works
// TODO: test that send fail works with no retry