Expect.java 文件源码

java
阅读 27 收藏 0 点赞 0 评论 0

项目:dcos-commons 作者:
/**
 * Verifies that the resources for the provided task names have been unreserved.
 */
public static Expect unreservedTasks(Collection<String> taskNames) {
    return new Expect() {
        // Use this form instead of using ArgumentCaptor.forClass() to avoid problems with typecasting generics:
        @Captor private ArgumentCaptor<Collection<Protos.OfferID>> offerIdsCaptor;
        @Captor private ArgumentCaptor<Collection<Protos.Offer.Operation>> operationsCaptor;

        @Override
        public void expect(ClusterState state, SchedulerDriver mockDriver) {
            MockitoAnnotations.initMocks(this);
            verify(mockDriver, atLeastOnce())
                    .acceptOffers(offerIdsCaptor.capture(), operationsCaptor.capture(), any());
            Assert.assertEquals(state.getLastOffer().getId(), offerIdsCaptor.getValue().iterator().next());
            Collection<String> expectedResourceIds = taskNames.stream()
                    .map(taskName ->
                            ResourceUtils.getResourceIds(state.getLastLaunchedTask(taskName).getResourcesList()))
                    .flatMap(List::stream)
                    .collect(Collectors.toList());
            Assert.assertFalse(String.format("Expected some resource ids for tasks: %s, got none", taskNames),
                    expectedResourceIds.isEmpty());
            Collection<String> unreservedResourceIds = new ArrayList<>();
            for (Protos.Offer.Operation operation : operationsCaptor.getValue()) {
                if (operation.getType().equals(Protos.Offer.Operation.Type.DESTROY)) {
                    // Destroy volume(s)
                    unreservedResourceIds.addAll(
                            ResourceUtils.getResourceIds(operation.getDestroy().getVolumesList()));
                } else if (operation.getType().equals(Protos.Offer.Operation.Type.UNRESERVE)) {
                    // Unreserve resource(s)
                    unreservedResourceIds.addAll(
                            ResourceUtils.getResourceIds(operation.getUnreserve().getResourcesList()));
                }
            }
            Assert.assertTrue(
                    String.format("Expected unreserved resource ids: %s, got ids: %s",
                            expectedResourceIds, unreservedResourceIds),
                    unreservedResourceIds.containsAll(expectedResourceIds)
                    && expectedResourceIds.containsAll(unreservedResourceIds));
        }

        @Override
        public String getDescription() {
            return String.format("Resources for tasks have been unreserved: %s", taskNames);
        }
    };
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号