Source:
<code>public void rateLimitsBlockBuilds(int rate) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo"); prj.setCriteria(null); BranchSource source = new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)); source.setStrategy(new DefaultBranchPropertyStrategy(new BranchProperty[]{ new RateLimitBranchProperty(rate, "hour") })); prj.getSourcesList().add(source); prj.scheduleBuild2(0).getFuture().get(); r.waitUntilNoActivity(); FreeStyleProject master = prj.getItem("master"); master.setQuietPeriod(0); assertThat(master.getProperties(), hasEntry( instanceOf(RateLimitBranchProperty.JobPropertyImpl.DescriptorImpl.class), allOf( instanceOf(RateLimitBranchProperty.JobPropertyImpl.class), hasProperty("count", is(rate)), hasProperty("durationName", is("hour")) ) ) ); assertThat(master.isInQueue(), is(false)); assertThat(master.getQueueItem(), nullValue()); QueueTaskFuture<FreeStyleBuild> future = master.scheduleBuild2(0); while (!master.isInQueue()) { <START> Thread.yield(); <END> } long startTime = System.currentTimeMillis(); assertThat(master.isInQueue(), is(true)); Queue.getInstance().maintain(); assertThat(master.getQueueItem().isBlocked(), is(true)); assertThat(master.getQueueItem().getCauseOfBlockage().getShortDescription().toLowerCase(), containsString("throttle")); Future<FreeStyleBuild> startCondition = future.getStartCondition(); long endTime = startTime + 60*60/rate*1000L*5; while (!startCondition.isDone() && System.currentTimeMillis() < endTime) { Queue.getInstance().maintain(); Thread.sleep(100); } assertThat(startCondition.isDone(), is(true)); assertThat("At least the rate implied delay but no more than 500ms longer", System.currentTimeMillis() - startTime, allOf( greaterThanOrEqualTo(60 * 60 / rate * 1000L), lessThanOrEqualTo(60 * 60 / rate * 1000L * 500L) ) ); future.get(); } }</code><technical_language>waitUntilNoActivity</technical_language>

Target:
public void rateLimitsBlockBuilds(int rate) throws Exception { try (MockSCMController c = MockSCMController.create()) { c.createRepository("foo"); BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo"); prj.setCriteria(null); BranchSource source = new BranchSource(new MockSCMSource(null, c, "foo", true, false, false)); source.setStrategy(new DefaultBranchPropertyStrategy(new BranchProperty[]{ new RateLimitBranchProperty(rate, "hour") })); prj.getSourcesList().add(source); prj.scheduleBuild2(0).getFuture().get(); r.waitUntilNoActivity(); FreeStyleProject master = prj.getItem("master"); master.setQuietPeriod(0); assertThat(master.getProperties(), hasEntry( instanceOf(RateLimitBranchProperty.JobPropertyImpl.DescriptorImpl.class), allOf( instanceOf(RateLimitBranchProperty.JobPropertyImpl.class), hasProperty("count", is(rate)), hasProperty("durationName", is("hour")) ) ) ); assertThat(master.isInQueue(), is(false)); assertThat(master.getQueueItem(), nullValue()); QueueTaskFuture<FreeStyleBuild> future = master.scheduleBuild2(0); while (!master.isInQueue()) { Thread.yield(); } long startTime = System.currentTimeMillis(); assertThat(master.isInQueue(), is(true)); Queue.getInstance().maintain(); assertThat(master.getQueueItem().isBlocked(), is(true)); assertThat(master.getQueueItem().getCauseOfBlockage().getShortDescription().toLowerCase(), containsString("throttle")); Future<FreeStyleBuild> startCondition = future.getStartCondition(); long endTime = startTime + 60*60/rate*1000L*5; while (!startCondition.isDone() && System.currentTimeMillis() < endTime) { Queue.getInstance().maintain(); Thread.sleep(100); } assertThat(startCondition.isDone(), is(true)); assertThat("At least the rate implied delay but no more than 500ms longer", System.currentTimeMillis() - startTime, allOf( greaterThanOrEqualTo(60 * 60 / rate * 1000L - 200L), lessThanOrEqualTo(60 * 60 / rate * 1000L * 500L) ) ); future.get(); } }