OperationLimiterTest.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "netdutils/OperationLimiter.h"
  17. #include <gtest/gtest-spi.h>
  18. namespace android {
  19. namespace netdutils {
  20. TEST(OperationLimiter, limits) {
  21. OperationLimiter<int> limiter(3);
  22. EXPECT_TRUE(limiter.start(42));
  23. EXPECT_TRUE(limiter.start(42));
  24. EXPECT_TRUE(limiter.start(42));
  25. // Limit reached... calling any number of times should have no effect.
  26. EXPECT_FALSE(limiter.start(42));
  27. EXPECT_FALSE(limiter.start(42));
  28. EXPECT_FALSE(limiter.start(42));
  29. // Finishing a single operations is enough for starting a new one...
  30. limiter.finish(42);
  31. EXPECT_TRUE(limiter.start(42));
  32. // ...but not two!
  33. EXPECT_FALSE(limiter.start(42));
  34. // Different ids should still have quota...
  35. EXPECT_TRUE(limiter.start(666));
  36. limiter.finish(666);
  37. // Finish all pending operations
  38. limiter.finish(42);
  39. limiter.finish(42);
  40. limiter.finish(42);
  41. }
  42. TEST(OperationLimiter, finishWithoutStart) {
  43. OperationLimiter<int> limiter(1);
  44. // Will output a LOG(FATAL_WITHOUT_ABORT), but we have no way to probe this.
  45. limiter.finish(42);
  46. // This will ensure that the finish() above didn't set a negative value.
  47. EXPECT_TRUE(limiter.start(42));
  48. EXPECT_FALSE(limiter.start(42));
  49. }
  50. TEST(OperationLimiter, destroyWithActiveOperations) {
  51. // The death message doesn't seem to be captured on Android.
  52. EXPECT_DEBUG_DEATH({
  53. OperationLimiter<int> limiter(3);
  54. limiter.start(42);
  55. }, "" /* "active operations */);
  56. }
  57. } // namespace netdutils
  58. } // namespace android