Home | History | Annotate | Download | only in libnetdutils
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "netdutils/OperationLimiter.h"
     18 
     19 #include <gtest/gtest-spi.h>
     20 
     21 namespace android {
     22 namespace netdutils {
     23 
     24 TEST(OperationLimiter, limits) {
     25     OperationLimiter<int> limiter(3);
     26 
     27     EXPECT_TRUE(limiter.start(42));
     28     EXPECT_TRUE(limiter.start(42));
     29     EXPECT_TRUE(limiter.start(42));
     30 
     31     // Limit reached... calling any number of times should have no effect.
     32     EXPECT_FALSE(limiter.start(42));
     33     EXPECT_FALSE(limiter.start(42));
     34     EXPECT_FALSE(limiter.start(42));
     35 
     36     // Finishing a single operations is enough for starting a new one...
     37     limiter.finish(42);
     38     EXPECT_TRUE(limiter.start(42));
     39 
     40     // ...but not two!
     41     EXPECT_FALSE(limiter.start(42));
     42 
     43     // Different ids should still have quota...
     44     EXPECT_TRUE(limiter.start(666));
     45     limiter.finish(666);
     46 
     47     // Finish all pending operations
     48     limiter.finish(42);
     49     limiter.finish(42);
     50     limiter.finish(42);
     51 }
     52 
     53 TEST(OperationLimiter, finishWithoutStart) {
     54     OperationLimiter<int> limiter(1);
     55 
     56     // Will output a LOG(FATAL_WITHOUT_ABORT), but we have no way to probe this.
     57     limiter.finish(42);
     58 
     59     // This will ensure that the finish() above didn't set a negative value.
     60     EXPECT_TRUE(limiter.start(42));
     61     EXPECT_FALSE(limiter.start(42));
     62 }
     63 
     64 TEST(OperationLimiter, destroyWithActiveOperations) {
     65     // The death message doesn't seem to be captured on Android.
     66     EXPECT_DEBUG_DEATH({
     67         OperationLimiter<int> limiter(3);
     68         limiter.start(42);
     69     }, "" /* "active operations */);
     70 }
     71 
     72 }  // namespace netdutils
     73 }  // namespace android
     74