github.com/voedger/voedger@v0.0.0-20240520144910-273e84102129/pkg/processors/query/operator-counter-impl_test.go (about)

     1  /*
     2   * Copyright (c) 2021-present unTill Pro, Ltd.
     3   */
     4  
     5  package queryprocessor
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/voedger/voedger/pkg/pipeline"
    13  )
    14  
    15  func TestCounterOperator_DoAsync(t *testing.T) {
    16  	tests := []struct {
    17  		startFrom      int64
    18  		count          int64
    19  		name           string
    20  		first          bool
    21  		second         bool
    22  		third          bool
    23  		fourth         bool
    24  		fifth          bool
    25  		sixth          bool
    26  		releaseCounter int64
    27  	}{
    28  		{
    29  			name:           "Should return all workpieces",
    30  			startFrom:      0,
    31  			count:          0,
    32  			first:          true,
    33  			second:         true,
    34  			third:          true,
    35  			fourth:         true,
    36  			fifth:          true,
    37  			sixth:          true,
    38  			releaseCounter: 0,
    39  		},
    40  		{
    41  			name:           "Should skip 2 workpieces then return 2 workpieces",
    42  			startFrom:      2,
    43  			count:          2,
    44  			first:          false,
    45  			second:         false,
    46  			third:          true,
    47  			fourth:         true,
    48  			fifth:          false,
    49  			sixth:          false,
    50  			releaseCounter: 4,
    51  		},
    52  		{
    53  			name:           "Should skip 3 workpieces then return remaining workpieces",
    54  			startFrom:      3,
    55  			count:          0,
    56  			first:          false,
    57  			second:         false,
    58  			third:          false,
    59  			fourth:         true,
    60  			fifth:          true,
    61  			sixth:          true,
    62  			releaseCounter: 3,
    63  		},
    64  	}
    65  	outWorkIsPresent := func(outWork pipeline.IWorkpiece, _ error) bool {
    66  		return outWork != nil
    67  	}
    68  	for _, test := range tests {
    69  		t.Run(test.name, func(t *testing.T) {
    70  			releaseCounter := int64(0)
    71  			work := testWorkpiece{release: func() {
    72  				releaseCounter++
    73  			}}
    74  			operator := newCounterOperator(test.startFrom, test.count, &testMetrics{})
    75  
    76  			require.Equal(t, test.first, outWorkIsPresent(operator.DoAsync(context.Background(), work)))
    77  			require.Equal(t, test.second, outWorkIsPresent(operator.DoAsync(context.Background(), work)))
    78  			require.Equal(t, test.third, outWorkIsPresent(operator.DoAsync(context.Background(), work)))
    79  			require.Equal(t, test.fourth, outWorkIsPresent(operator.DoAsync(context.Background(), work)))
    80  			require.Equal(t, test.fifth, outWorkIsPresent(operator.DoAsync(context.Background(), work)))
    81  			require.Equal(t, test.sixth, outWorkIsPresent(operator.DoAsync(context.Background(), work)))
    82  			require.Equal(t, test.releaseCounter, releaseCounter)
    83  		})
    84  	}
    85  }