github.com/sunriselayer/sunrise-da@v0.13.1-sr3/das/state_test.go (about)

     1  package das
     2  
     3  import (
     4  	"errors"
     5  	"sort"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func Test_coordinatorStats(t *testing.T) {
    12  	tests := []struct {
    13  		name  string
    14  		state *coordinatorState
    15  		want  SamplingStats
    16  	}{
    17  		{
    18  			"basic",
    19  			&coordinatorState{
    20  				inProgress: map[int]func() workerState{
    21  					1: func() workerState {
    22  						return workerState{
    23  							result: result{
    24  								job: job{
    25  									jobType: recentJob,
    26  									from:    21,
    27  									to:      30,
    28  								},
    29  								failed: map[uint64]int{22: 1},
    30  								err:    errors.New("22: failed"),
    31  							},
    32  							curr: 25,
    33  						}
    34  					},
    35  					2: func() workerState {
    36  						return workerState{
    37  							result: result{
    38  								job: job{
    39  									jobType: catchupJob,
    40  									from:    11,
    41  									to:      20,
    42  								},
    43  								failed: map[uint64]int{12: 1, 13: 1},
    44  								err:    errors.Join(errors.New("12: failed"), errors.New("13: failed")),
    45  							},
    46  							curr: 15,
    47  						}
    48  					},
    49  				},
    50  				failed: map[uint64]retryAttempt{
    51  					22: {count: 1},
    52  					23: {count: 1},
    53  					24: {count: 2},
    54  				},
    55  				nextJobID:   0,
    56  				next:        31,
    57  				networkHead: 100,
    58  			},
    59  			SamplingStats{
    60  				SampledChainHead: 11,
    61  				CatchupHead:      30,
    62  				NetworkHead:      100,
    63  				Failed:           map[uint64]int{22: 2, 23: 1, 24: 2, 12: 1, 13: 1},
    64  				Workers: []WorkerStats{
    65  					{
    66  						JobType: recentJob,
    67  						Curr:    25,
    68  						From:    21,
    69  						To:      30,
    70  						ErrMsg:  "22: failed",
    71  					},
    72  					{
    73  						JobType: catchupJob,
    74  						Curr:    15,
    75  						From:    11,
    76  						To:      20,
    77  						ErrMsg:  "12: failed\n13: failed",
    78  					},
    79  				},
    80  				Concurrency: 2,
    81  				CatchUpDone: false,
    82  				IsRunning:   true,
    83  			},
    84  		},
    85  	}
    86  	for _, tt := range tests {
    87  		t.Run(tt.name, func(t *testing.T) {
    88  			stats := tt.state.unsafeStats()
    89  			sort.Slice(stats.Workers, func(i, j int) bool {
    90  				return stats.Workers[i].From > stats.Workers[j].Curr
    91  			})
    92  			assert.Equal(t, tt.want, stats, "stats are not equal")
    93  		})
    94  	}
    95  }