use of com.google.common.base.Predicate in project cdap by caskdata.
the class WorkflowTest method testOneActionWorkflow.
@Test(timeout = 120 * 1000L)
public void testOneActionWorkflow() throws Exception {
final ApplicationWithPrograms app = AppFabricTestHelper.deployApplicationWithManager(OneActionWorkflowApp.class, TEMP_FOLDER_SUPPLIER);
final Injector injector = AppFabricTestHelper.getInjector();
final ProgramDescriptor programDescriptor = Iterators.filter(app.getPrograms().iterator(), new Predicate<ProgramDescriptor>() {
@Override
public boolean apply(ProgramDescriptor input) {
return input.getProgramId().getType() == ProgramType.WORKFLOW;
}
}).next();
final SettableFuture<String> completion = SettableFuture.create();
final ProgramController controller = AppFabricTestHelper.submit(app, programDescriptor.getSpecification().getClassName(), new BasicArguments(), TEMP_FOLDER_SUPPLIER);
controller.addListener(new AbstractListener() {
@Override
public void init(ProgramController.State currentState, @Nullable Throwable cause) {
LOG.info("Initializing");
injector.getInstance(Store.class).setStart(controller.getProgramRunId().getParent(), controller.getProgramRunId().getRun(), System.currentTimeMillis());
}
@Override
public void completed() {
LOG.info("Completed");
completion.set("Completed");
}
@Override
public void error(Throwable cause) {
LOG.info("Error", cause);
completion.setException(cause);
}
}, Threads.SAME_THREAD_EXECUTOR);
String run = completion.get();
Assert.assertEquals("Completed", run);
}
use of com.google.common.base.Predicate in project cdap by caskdata.
the class LeaderElectionInfoServiceTest method testParticipants.
@Test
public void testParticipants() throws Exception {
final int size = 5;
String prefix = "/election";
List<ZKClientService> zkClients = new ArrayList<>();
ZKClientService infoZKClient = DefaultZKClientService.Builder.of(zkServer.getConnectionStr()).build();
infoZKClient.startAndWait();
zkClients.add(infoZKClient);
// Start the LeaderElectionInfoService
LeaderElectionInfoService infoService = new LeaderElectionInfoService(infoZKClient, prefix);
infoService.startAndWait();
// This will timeout as there is no leader election node created yet
try {
infoService.getParticipants(1, TimeUnit.SECONDS);
Assert.fail("Expected timeout");
} catch (TimeoutException e) {
// Expected
}
// Starts multiple leader elections
List<LeaderElection> leaderElections = new ArrayList<>();
for (int i = 0; i < size; i++) {
ZKClientService zkClient = DefaultZKClientService.Builder.of(zkServer.getConnectionStr()).build();
zkClient.startAndWait();
zkClients.add(zkClient);
final int participantId = i;
LeaderElection leaderElection = new LeaderElection(zkClient, prefix, new ElectionHandler() {
@Override
public void leader() {
LOG.info("Leader: {}", participantId);
}
@Override
public void follower() {
LOG.info("Follow: {}", participantId);
}
});
leaderElection.start();
leaderElections.add(leaderElection);
}
// Get the dynamic participants map
final SortedMap<Integer, LeaderElectionInfoService.Participant> participants = infoService.getParticipants(5, TimeUnit.SECONDS);
// Expects to set all participants with hostname information
Tasks.waitFor(true, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
if (participants.size() != size) {
return false;
}
return Iterables.all(participants.values(), new Predicate<LeaderElectionInfoService.Participant>() {
@Override
public boolean apply(LeaderElectionInfoService.Participant input) {
return input.getHostname() != null;
}
});
}
}, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
// Fetch the static snapshot. It should be the same as the dynamic participants.
SortedMap<Integer, LeaderElectionInfoService.Participant> snapshot = infoService.fetchCurrentParticipants();
Assert.assertEquals(size, snapshot.size());
Assert.assertEquals(participants, snapshot);
int expectedSize = size;
for (LeaderElection leaderElection : leaderElections) {
leaderElection.stopAndWait();
Tasks.waitFor(--expectedSize, new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return participants.size();
}
}, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
}
// Fetch the static snapshot again. It should be empty and the same as the dynamic one.
snapshot = infoService.fetchCurrentParticipants();
Assert.assertTrue(snapshot.isEmpty());
Assert.assertEquals(participants, snapshot);
infoService.stopAndWait();
for (ZKClientService zkClient : zkClients) {
zkClient.stopAndWait();
}
}
use of com.google.common.base.Predicate in project android by JetBrains.
the class AndroidModuleInfoTest method testManifestMerger.
public void testManifestMerger() throws Exception {
loadProject(MODULE_INFO_MANIFEST_MERGER);
assertNotNull(myAndroidFacet);
assertEquals(1, AndroidUtils.getAllAndroidDependencies(myAndroidFacet.getModule(), true).size());
MergedManifest manifestInfo = MergedManifest.get(myAndroidFacet);
List<Element> mergedActivities = manifestInfo.getActivities();
assertEquals(3, mergedActivities.size());
Set<String> activities = Sets.newHashSet(ActivityLocatorUtils.getQualifiedName(mergedActivities.get(0)), ActivityLocatorUtils.getQualifiedName(mergedActivities.get(1)), ActivityLocatorUtils.getQualifiedName(mergedActivities.get(2)));
assertTrue(activities.contains("test.helloworldapp.Debug"));
assertTrue(activities.contains("test.helloworldapp.MyActivity"));
assertTrue(activities.contains("test.helloworldapp.lib.LibActivity"));
assertNotNull(manifestInfo.getVersionCode());
assertEquals(2, manifestInfo.getVersionCode().intValue());
// Make a change to the psi
VirtualFile manifestFile = AndroidRootUtil.getPrimaryManifestFile(myAndroidFacet);
assertNotNull(manifestFile);
PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(manifestFile);
assertNotNull(psiFile);
new WriteCommandAction.Simple(getProject(), psiFile) {
@Override
protected void run() throws Throwable {
assertNotNull(myAndroidFacet.getManifest());
XmlTag manifestTag = myAndroidFacet.getManifest().getXmlTag();
XmlTag applicationTag = Iterables.find(Arrays.asList(manifestTag.getSubTags()), new Predicate<XmlTag>() {
@Override
public boolean apply(@Nullable XmlTag input) {
assertNotNull(input);
return "application".equals(input.getName());
}
});
XmlTag activityTag = applicationTag.createChildTag("activity", "", null, false);
activityTag.setAttribute("android:name", ".AddedActivity");
applicationTag.addSubTag(activityTag, false);
}
}.execute();
UIUtil.dispatchAllInvocationEvents();
// reload data and check it is correct
manifestInfo.clear();
mergedActivities = manifestInfo.getActivities();
assertEquals(4, mergedActivities.size());
activities = Sets.newHashSet(ActivityLocatorUtils.getQualifiedName(mergedActivities.get(0)), ActivityLocatorUtils.getQualifiedName(mergedActivities.get(1)), ActivityLocatorUtils.getQualifiedName(mergedActivities.get(2)), ActivityLocatorUtils.getQualifiedName(mergedActivities.get(3)));
assertTrue(activities.contains("test.helloworldapp.Debug"));
assertTrue(activities.contains("test.helloworldapp.MyActivity"));
assertTrue(activities.contains("test.helloworldapp.lib.LibActivity"));
assertTrue(activities.contains("test.helloworldapp.AddedActivity"));
// make a change to the psi
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
final Document document = documentManager.getDocument(psiFile);
WriteCommandAction.runWriteCommandAction(null, new Runnable() {
@Override
public void run() {
assertNotNull(document);
int index = document.getText().indexOf("</application>");
document.insertString(index, "<activity android:name=\".AddedActivity2\" />\n");
documentManager.commitDocument(document);
}
});
UIUtil.dispatchAllInvocationEvents();
// reload data and check it is correct
manifestInfo.clear();
mergedActivities = manifestInfo.getActivities();
assertEquals(5, mergedActivities.size());
activities = Sets.newHashSet(ActivityLocatorUtils.getQualifiedName(mergedActivities.get(0)), ActivityLocatorUtils.getQualifiedName(mergedActivities.get(1)), ActivityLocatorUtils.getQualifiedName(mergedActivities.get(2)), ActivityLocatorUtils.getQualifiedName(mergedActivities.get(3)), ActivityLocatorUtils.getQualifiedName(mergedActivities.get(4)));
assertTrue(activities.contains("test.helloworldapp.Debug"));
assertTrue(activities.contains("test.helloworldapp.MyActivity"));
assertTrue(activities.contains("test.helloworldapp.lib.LibActivity"));
assertTrue(activities.contains("test.helloworldapp.AddedActivity"));
assertTrue(activities.contains("test.helloworldapp.AddedActivity2"));
}
use of com.google.common.base.Predicate in project GeoGig by boundlessgeo.
the class ShowRef method runInternal.
@Override
public void runInternal(GeogigCLI cli) throws IOException {
ConsoleReader console = cli.getConsole();
GeoGIG geogig = cli.getGeogig();
ForEachRef op = geogig.command(ForEachRef.class);
Predicate<Ref> filter = new Predicate<Ref>() {
@Override
public boolean apply(Ref ref) {
String name = ref.getName();
if (!name.startsWith(Ref.REFS_PREFIX)) {
return false;
}
boolean match = patterns.isEmpty() ? true : false;
for (String pattern : patterns) {
if (Strings.isNullOrEmpty(pattern)) {
match = true;
} else if (name.endsWith("/" + pattern)) {
match = true;
break;
}
}
return match;
}
};
op.setFilter(filter);
ImmutableSet<Ref> refs = op.call();
for (Ref ref : refs) {
console.println(ref.getObjectId() + " " + ref.getName());
}
}
use of com.google.common.base.Predicate in project GeoGig by boundlessgeo.
the class LocalRemoteRepo method copyNewObjects.
private void copyNewObjects(RevTree oldTree, RevTree newTree, final ObjectDatabase fromDb, final ObjectDatabase toDb, final ProgressListener progress) {
checkNotNull(oldTree);
checkNotNull(newTree);
checkNotNull(fromDb);
checkNotNull(toDb);
checkNotNull(progress);
// the diff walk uses fromDb as both left and right data source since we're comparing what
// we have in the "origin" database against trees on the same repository
PostOrderDiffWalk diffWalk = new PostOrderDiffWalk(oldTree, newTree, fromDb, fromDb);
// holds object ids that need to be copied to the target db. Pruned when it reaches a
// threshold.
final Set<ObjectId> ids = new HashSet<ObjectId>();
// This filter further refines the post order diff walk by making it ignore trees/buckets
// that are already present in the target db
Predicate<Bounded> filter = new Predicate<Bounded>() {
@Override
public boolean apply(@Nullable Bounded b) {
if (b == null) {
return false;
}
if (progress.isCanceled()) {
// abort traversal
return false;
}
ObjectId id;
if (b instanceof Node) {
Node node = (Node) b;
if (RevObject.TYPE.TREE.equals(node.getType())) {
// check of existence of trees only. For features the diff filtering is good
// enough and checking for existence on each feature would be killer
// performance wise
id = node.getObjectId();
} else {
return true;
}
} else {
id = ((Bucket) b).id();
}
boolean exists = ids.contains(id) || toDb.exists(id);
return !exists;
}
};
// receives notifications of feature/bucket/tree diffs. Only interested in the "new"/right
// side of the comparisons
Consumer consumer = new Consumer() {
final int bulkSize = 10_000;
@Override
public void feature(@Nullable Node left, Node right) {
add(left);
add(right);
}
@Override
public void tree(@Nullable Node left, Node right) {
add(left);
add(right);
}
private void add(@Nullable Node node) {
if (node == null) {
return;
}
ids.add(node.getObjectId());
Optional<ObjectId> metadataId = node.getMetadataId();
if (metadataId.isPresent()) {
ids.add(metadataId.get());
}
checkLimitAndCopy();
}
@Override
public void bucket(int bucketIndex, int bucketDepth, @Nullable Bucket left, Bucket right) {
if (left != null) {
ids.add(left.id());
}
if (right != null) {
ids.add(right.id());
}
checkLimitAndCopy();
}
private void checkLimitAndCopy() {
if (ids.size() >= bulkSize) {
copy(ids, fromDb, toDb, progress);
ids.clear();
}
}
};
diffWalk.walk(filter, consumer);
// copy remaining objects
copy(ids, fromDb, toDb, progress);
}
Aggregations