github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/init/handle_init_test.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package initPkg
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    11  )
    12  
    13  func Test_retry(t *testing.T) {
    14  	callCounter := 0
    15  	failingPins := []types.ChunkRecord{
    16  		{
    17  			Range: "first",
    18  		},
    19  		{
    20  			Range: "second",
    21  		},
    22  		{
    23  			Range: "third",
    24  		},
    25  	}
    26  	fakeDownload := func(p []types.ChunkRecord) ([]types.ChunkRecord, bool) {
    27  		defer func() { callCounter++ }()
    28  		switch callCounter {
    29  		case 0:
    30  			return failingPins, false
    31  		case 1:
    32  			if len(p) != len(failingPins) {
    33  				t.Error("Wrong count for 2nd call")
    34  			}
    35  			return failingPins[2:], false
    36  		case 2:
    37  			if len(p) != len(failingPins[2:]) {
    38  				t.Error("Wrong count for 3nd call", len(p))
    39  			}
    40  			return []types.ChunkRecord{}, false
    41  		}
    42  
    43  		t.Fatal("Too many calls")
    44  		return nil, false
    45  	}
    46  
    47  	totalFailed := retry(failingPins, 3, fakeDownload)
    48  
    49  	if totalFailed != 0 {
    50  		t.Error("Still failing", totalFailed)
    51  	}
    52  }