github.com/prebid/prebid-server/v2@v2.18.0/exchange/tmax_adjustments_test.go (about)

     1  package exchange
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/prebid/prebid-server/v2/config"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestGetBidderTmax(t *testing.T) {
    12  	var (
    13  		requestTmaxMS               int64 = 700
    14  		bidderNetworkLatencyBuffer  uint  = 50
    15  		responsePreparationDuration uint  = 60
    16  	)
    17  	requestTmaxNS := requestTmaxMS * int64(time.Millisecond)
    18  	startTime := time.Date(2023, 5, 30, 1, 0, 0, 0, time.UTC)
    19  	deadline := time.Date(2023, 5, 30, 1, 0, 0, int(requestTmaxNS), time.UTC)
    20  	ctx := &mockBidderTmaxCtx{startTime: startTime, deadline: deadline, ok: true}
    21  	tests := []struct {
    22  		description     string
    23  		ctx             bidderTmaxContext
    24  		requestTmax     int64
    25  		expectedTmax    int64
    26  		tmaxAdjustments TmaxAdjustmentsPreprocessed
    27  	}{
    28  		{
    29  			description:     "returns-requestTmax-when-IsEnforced-is-false",
    30  			ctx:             ctx,
    31  			requestTmax:     requestTmaxMS,
    32  			tmaxAdjustments: TmaxAdjustmentsPreprocessed{IsEnforced: false},
    33  			expectedTmax:    requestTmaxMS,
    34  		},
    35  		{
    36  			description:     "returns-requestTmax-when-BidderResponseDurationMin-is-not-set",
    37  			ctx:             ctx,
    38  			requestTmax:     requestTmaxMS,
    39  			tmaxAdjustments: TmaxAdjustmentsPreprocessed{IsEnforced: true, BidderResponseDurationMin: 0},
    40  			expectedTmax:    requestTmaxMS,
    41  		},
    42  		{
    43  			description:     "returns-requestTmax-when-BidderNetworkLatencyBuffer-and-PBSResponsePreparationDuration-is-not-set",
    44  			ctx:             ctx,
    45  			requestTmax:     requestTmaxMS,
    46  			tmaxAdjustments: TmaxAdjustmentsPreprocessed{IsEnforced: true, BidderResponseDurationMin: 100, BidderNetworkLatencyBuffer: 0, PBSResponsePreparationDuration: 0},
    47  			expectedTmax:    requestTmaxMS,
    48  		},
    49  		{
    50  			description:     "returns-requestTmax-when-context-deadline-is-not-set",
    51  			ctx:             &mockBidderTmaxCtx{ok: false},
    52  			requestTmax:     requestTmaxMS,
    53  			tmaxAdjustments: TmaxAdjustmentsPreprocessed{IsEnforced: true, BidderResponseDurationMin: 100, BidderNetworkLatencyBuffer: 50, PBSResponsePreparationDuration: 60},
    54  			expectedTmax:    requestTmaxMS,
    55  		},
    56  		{
    57  			description:     "returns-remaing-duration-by-subtracting-BidderNetworkLatencyBuffer-and-PBSResponsePreparationDuration",
    58  			ctx:             ctx,
    59  			requestTmax:     requestTmaxMS,
    60  			tmaxAdjustments: TmaxAdjustmentsPreprocessed{IsEnforced: true, BidderResponseDurationMin: 100, BidderNetworkLatencyBuffer: bidderNetworkLatencyBuffer, PBSResponsePreparationDuration: responsePreparationDuration},
    61  			expectedTmax:    ctx.RemainingDurationMS(deadline) - int64(bidderNetworkLatencyBuffer) - int64(responsePreparationDuration),
    62  		},
    63  	}
    64  	for _, test := range tests {
    65  		t.Run(test.description, func(t *testing.T) {
    66  			assert.Equal(t, test.expectedTmax, getBidderTmax(test.ctx, test.requestTmax, test.tmaxAdjustments))
    67  		})
    68  	}
    69  }
    70  
    71  func TestProcessTMaxAdjustments(t *testing.T) {
    72  	tests := []struct {
    73  		description     string
    74  		expected        *TmaxAdjustmentsPreprocessed
    75  		tmaxAdjustments config.TmaxAdjustments
    76  	}{
    77  		{
    78  			description:     "returns-nil-when-tmax-is-not-enabled",
    79  			tmaxAdjustments: config.TmaxAdjustments{Enabled: false},
    80  			expected:        nil,
    81  		},
    82  		{
    83  			description:     "BidderResponseDurationMin-is-not-set",
    84  			tmaxAdjustments: config.TmaxAdjustments{Enabled: true, BidderResponseDurationMin: 0, BidderNetworkLatencyBuffer: 10, PBSResponsePreparationDuration: 20},
    85  			expected:        &TmaxAdjustmentsPreprocessed{IsEnforced: false, BidderResponseDurationMin: 0, BidderNetworkLatencyBuffer: 10, PBSResponsePreparationDuration: 20},
    86  		},
    87  		{
    88  			description:     "BidderNetworkLatencyBuffer-and-PBSResponsePreparationDuration-are-not-set",
    89  			tmaxAdjustments: config.TmaxAdjustments{Enabled: true, BidderResponseDurationMin: 100, BidderNetworkLatencyBuffer: 0, PBSResponsePreparationDuration: 0},
    90  			expected:        &TmaxAdjustmentsPreprocessed{IsEnforced: false, BidderResponseDurationMin: 100, BidderNetworkLatencyBuffer: 0, PBSResponsePreparationDuration: 0},
    91  		},
    92  		{
    93  			description:     "BidderNetworkLatencyBuffer-is-not-set",
    94  			tmaxAdjustments: config.TmaxAdjustments{Enabled: true, BidderResponseDurationMin: 100, BidderNetworkLatencyBuffer: 0, PBSResponsePreparationDuration: 10},
    95  			expected:        &TmaxAdjustmentsPreprocessed{IsEnforced: true, BidderResponseDurationMin: 100, BidderNetworkLatencyBuffer: 0, PBSResponsePreparationDuration: 10},
    96  		},
    97  		{
    98  			description:     "PBSResponsePreparationDuration-is-not-set",
    99  			tmaxAdjustments: config.TmaxAdjustments{Enabled: true, BidderResponseDurationMin: 100, BidderNetworkLatencyBuffer: 10, PBSResponsePreparationDuration: 0},
   100  			expected:        &TmaxAdjustmentsPreprocessed{IsEnforced: true, BidderResponseDurationMin: 100, BidderNetworkLatencyBuffer: 10, PBSResponsePreparationDuration: 0},
   101  		},
   102  	}
   103  	for _, test := range tests {
   104  		t.Run(test.description, func(t *testing.T) {
   105  			assert.Equal(t, test.expected, ProcessTMaxAdjustments(test.tmaxAdjustments))
   106  		})
   107  	}
   108  }