?????????????????????????????????????
????"throw exception once reached the limit [naive??flaky]" in new ctx {
????for (i <- 1 to maxCount) {
????Future {
????throttler(waitForever())
????}
????}
????throttler {} must throwA[ThrottledException]
????????????? maxCount ?????????? Future{}???????? waitForever ???????煤????????????????????????????????????????????????? —— maxCount + 1????????????????????? ThrottledException ????????????????????????????????????????????????????? future ???????校?future ???????????????????????????
????????????????????????????????????????????????????????????违???????????????????械????????????????? waitForever ?????斜????????????????????????????些???????????? future ?????????????????????????????????????????????? sleep ????????些????????
????"throw exception once reached the limit [naive?? bad]" in new ctx {
????for (i <- 1 to maxCount) {
????Future {
????throttler(waitForever())
????}
????}
????Thread.sleep(1000)
????throttler {} must throwA[ThrottledException]
????}
??????????????????????????????????????????????????????????????????
?????????????????????????????煤??”????????”????谩?
??????????????????锟�??????????????????????????????????????
?????????????械??????????????? Google ????????
?????????????????????????(future)????????????????????????????????????? java.util.concurrent ????? CountDownLatch ??
????"throw exception once reached the limit [working]" in new ctx {
????val barrier = new CountDownLatch(maxCount)
????for (i <- 1 to maxCount) {
????Future {
????throttler {
????barrier.countDown()
????waitForever()
????}
????}
????}
????barrier.await(5?? TimeUnit.SECONDS) must beTrue
????throttler {} must throwA[ThrottledException]
????}
??????????? CountDownLatch ??????????????????????????????????????????????? 0?????????????????校????????些?????????? future???????? future ??????? countDown ?????????????? 1???????????? 0?????械? future ????????械? waitForever ?????????
?????????????????????? throttler ???????????写???????maxCount??????????????????????? throttler ????????????????????????????????????????????????????????? throttler??????????????????????????? 0 ???? CountDownLatch ???????????
????????些???????????榉�??????????贸?????????????????????????????????????榉�????????????????????????????????????????????????????????????????????????
????????
???????????????????????????????????????????????????????????????魏??????????????????????????????????????????? Thread.sleep ???????????????????????????????????
??????????????锟�????????????斜????????????????????????? CountDownLatch ???? Thead.sleep????? CountDownlatch ????????????????????????????????????????????????????????????????????????????????????????????????????? waiting ?????????? waitForever ?????????????????? Thread.sleep(Long.MAX_VALUE) ?????????????????????????????貌??????????