text
stringlengths
0
2.2M
//:
//: 6 Specifying wrong parameters to 'submitReserved' causes certain
//: behavior in special build configuration.
//
// Testing:
// void reserve(bsls::Types::Uint64 numOfUnits);
// bsls::Types::Uint64 unitsReserved();
// void submitReserved(bsls::Types::Uint64 numOfUnits);
//---------------------------------------------------------------------
if (verbose)
cout << endl
<< "TESTING: 'reserve', 'unitsReserved', 'submitReserved'"
<< endl
<< "====================================================="
<< endl;
const Uint64 S_RATE = 1000;
const Ti S_WND = Ti(1);
const Uint64 P_RATE = 10000;
const Ti P_WND = Ti(0.01);
struct {
int d_line;
Uint64 d_unitsToReserve;
Uint64 d_unitsToSubmit;
Uint64 d_expectedUnitsReserved;
} DATA[] = {
// LINE RESERVE SUBMIT EXP_RES
// ---- ---------- ------------- --------------------
{L_, 1000, 300, 700},
// C-4
{L_, 1000, 1000, 0},
{L_, 1000, 700, 300},
{L_, ULLONG_MAX, ULLONG_MAX/2, ULLONG_MAX-ULLONG_MAX/2}
};
const int NUM_DATA = sizeof(DATA)/sizeof(*DATA);
for(int ti=0; ti<NUM_DATA; ti++) {
const Uint64 LINE = DATA[ti].d_line;
const Uint64 UNITS_TO_RESERVE = DATA[ti].d_unitsToReserve;
const Uint64 UNITS_TO_SUBMIT = DATA[ti].d_unitsToSubmit;
const Uint64 EXPECTED_UNITS_RESERVED =
DATA[ti].d_expectedUnitsReserved;
balb::LeakyBucket peakLb(
P_RATE,
balb::LeakyBucket::calculateCapacity(P_RATE,
P_WND),
Ti(0));
balb::LeakyBucket sustLb(
S_RATE,
balb::LeakyBucket::calculateCapacity(S_RATE,
S_WND),
Ti(0));
Obj x(S_RATE, S_WND, P_RATE, P_WND, Ti(0));
// C-1, C-2
x.reserve(UNITS_TO_RESERVE);
LOOP_ASSERT(LINE, UNITS_TO_RESERVE == x.unitsReserved());
sustLb.reserve(UNITS_TO_RESERVE);
peakLb.reserve(UNITS_TO_RESERVE);
Ti t1 = x.calculateTimeToSubmit(Ti(0));
x.submitReserved(UNITS_TO_SUBMIT);
sustLb.submitReserved(UNITS_TO_SUBMIT);
peakLb.submitReserved(UNITS_TO_SUBMIT);
Ti t2 = x.calculateTimeToSubmit(Ti(0));
// C-3
LOOP_ASSERT(LINE,
EXPECTED_UNITS_RESERVED == x.unitsReserved());
// Check, whether units were actually submitted to rate limiter.
if (false == x.wouldExceedBandwidth(Ti(0))) {
bsls::Types::Uint64 freeUnits = bsl::min(
sustLb.capacity() - sustLb.unitsInBucket(),
peakLb.capacity() - peakLb.unitsInBucket());
x.submit(freeUnits);
LOOP_ASSERT(LINE, true == x.wouldExceedBandwidth(Ti(0)));
}
else {
if (0 != UNITS_TO_SUBMIT) {
LOOP_ASSERT(LINE, t2 >= t1);
}
}
}