### Question:
ConcurrencyConstraint extends ProtoConstraint.ConcurrencyConstraint implements CheckableConstraint { @Override public ConstraintResult check(ProgramSchedule schedule, ConstraintContext context) { Map<ProgramRunId, RunRecordDetail> activeRuns = context.getActiveRuns(schedule.getProgramId()); if (activeRuns.size() >= maxConcurrency) { LOG.debug("Skipping run of program {} from schedule {} because there are {} active runs.", schedule.getProgramId(), schedule.getName(), activeRuns.size()); return notSatisfied(context); } return ConstraintResult.SATISFIED; } ConcurrencyConstraint(int maxConcurrency); @Override ConstraintResult check(ProgramSchedule schedule, ConstraintContext context);  }### Answer:
@Test public void testMaxConcurrentRuns() { Store store = AppFabricTestHelper.getInjector().getInstance(Store.class); try { long now = System.currentTimeMillis(); ProgramSchedule schedule = new ProgramSchedule("SCHED1", "one partition schedule", WORKFLOW_ID, ImmutableMap.of("prop3", "abc"), new PartitionTrigger(DATASET_ID, 1), ImmutableList.of()); SimpleJob job = new SimpleJob(schedule, 0, now, Collections.emptyList(), Job.State.PENDING_TRIGGER, 0L); ConcurrencyConstraint concurrencyConstraint = new ConcurrencyConstraint(2); ConstraintContext constraintContext = new ConstraintContext(job, now, store); assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext)); ProgramRunId pid1 = WORKFLOW_ID.run(RunIds.generate().getId()); ProgramRunId pid2 = WORKFLOW_ID.run(RunIds.generate().getId()); ProgramRunId pid3 = WORKFLOW_ID.run(RunIds.generate().getId()); Map<String, String> systemArgs = ImmutableMap.of(ProgramOptionConstants.SCHEDULE_NAME, schedule.getName()); setStartAndRunning(store, pid1, EMPTY_MAP, systemArgs); assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext)); systemArgs = ImmutableMap.of(ProgramOptionConstants.SCHEDULE_NAME, "not" + schedule.getName()); setStartAndRunning(store, pid2, EMPTY_MAP, systemArgs); assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext)); setStartAndRunning(store, pid3); assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext)); store.setStop(pid1, System.currentTimeMillis(), ProgramRunStatus.COMPLETED, AppFabricTestHelper.createSourceId(++sourceId)); assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext)); store.setSuspend(pid3, AppFabricTestHelper.createSourceId(++sourceId), -1); assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext)); store.setResume(pid3, AppFabricTestHelper.createSourceId(++sourceId), -1); assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext)); store.setStop(pid3, System.currentTimeMillis(), ProgramRunStatus.KILLED, AppFabricTestHelper.createSourceId(++sourceId)); assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext)); ProgramRunId pid4 = WORKFLOW_ID.run(RunIds.generate().getId()); setProvisioning(store, pid4, Collections.emptyMap(), Collections.emptyMap()); assertSatisfied(false, concurrencyConstraint.check(schedule, constraintContext)); store.setStop(pid4, System.currentTimeMillis(), ProgramRunStatus.FAILED, AppFabricTestHelper.createSourceId(++sourceId)); assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext)); store.setStop(pid2, System.currentTimeMillis(), ProgramRunStatus.FAILED, AppFabricTestHelper.createSourceId(++sourceId)); assertSatisfied(true, concurrencyConstraint.check(schedule, constraintContext)); } finally { AppFabricTestHelper.shutdown(); } }