text
stringlengths
0
2.2M
bslmt::ThreadUtil::microSleep(static_cast<int>(uS));
}
}
//..
// Notice that we wait by putting the thread into a sleep state instead of
// using busy-waiting to better optimize for multi-threaded applications.
//..
} break;
case 12: {
// --------------------------------------------------------------------
// CLASS METHOD 'cancelReserved'
//
// Concerns:
//: 1 The method decrements the number of 'unitsReserved'.
//:
//: 2 The method does not submit units.
//:
//: 3 The method correctly handles the case, when 'numOfUnits' is
//: greater than the number of 'unitsReserved'.
//
// Testing:
// void cancelReserved(bsls::Types::Uint64 numOfUnits)
// --------------------------------------------------------------------
if (verbose) cout << endl
<< "CLASS METHOD 'cancelReserved'" << 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_unitsToCancel;
Uint64 d_expectedUnitsReserved;
} DATA[] = {
// LINE RESERVE CANCEL EXP_RES
// ---- ---------- ------------- --------------------
{L_, 1000, 300, 700},
// C-3
{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_CANCEL = DATA[ti].d_unitsToCancel;
const Uint64 EXPECTED_UNITS_RESERVED =
DATA[ti].d_expectedUnitsReserved;
Obj x(S_RATE, S_WND, P_RATE, P_WND, Ti(0));
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));
// C-1
x.reserve(UNITS_TO_RESERVE);
sustLb.reserve(UNITS_TO_RESERVE);
peakLb.reserve(UNITS_TO_RESERVE);
LOOP_ASSERT(LINE, UNITS_TO_RESERVE == x.unitsReserved());
x.cancelReserved(UNITS_TO_CANCEL);
sustLb.cancelReserved(UNITS_TO_CANCEL);
peakLb.cancelReserved(UNITS_TO_CANCEL);
LOOP_ASSERT(LINE,
EXPECTED_UNITS_RESERVED == x.unitsReserved());
// Check that no units were submitted to the rate limiter.
// C-2
if (false == x.wouldExceedBandwidth(Ti(0))) {
bsls::Types::Uint64 freeUnits = bsl::min(
sustLb.capacity() - x.unitsReserved(),
peakLb.capacity() - x.unitsReserved());
x.submit(freeUnits);
LOOP_ASSERT(LINE, true == x.wouldExceedBandwidth(Ti(0)));
x.reset(Ti(0));
x.reserve(UNITS_TO_RESERVE);