Skip to content

Commit

Permalink
userPrefs support in embedded-iwant
Browse files Browse the repository at this point in the history
  • Loading branch information
wipu committed Nov 9, 2024
1 parent 57c9de1 commit e9ccca1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import org.fluentjava.iwant.api.wsdef.WorkspaceModuleContext;
import org.fluentjava.iwant.entry.Iwant;
import org.fluentjava.iwant.entry3.CachesImpl;
import org.fluentjava.iwant.entry3.DefaultUserPrefs;
import org.fluentjava.iwant.entry3.UserPrefs;
import org.fluentjava.iwant.entry3.WishEvaluator;

/**
Expand All @@ -29,6 +31,7 @@ public static class IHavePlease {

private File wsRoot;
private File wsCache;
private UserPrefs userPrefs = new DefaultUserPrefs(null);

public IHavePlease workspaceAt(File wsRoot) {
this.wsRoot = wsRoot;
Expand All @@ -40,6 +43,11 @@ public IHavePlease cacheAt(File wsCache) {
return this;
}

public IHavePlease userPrefs(UserPrefs userPrefs) {
this.userPrefs = userPrefs;
return this;
}

public TargetOrSideEffect iwant() {
return new TargetOrSideEffect();
}
Expand Down Expand Up @@ -67,7 +75,7 @@ public File asPath() {
WsInfo wsInfo = null;
Caches caches = new CachesImpl(wsCache, iwantApiClasses,
iwant.network());
int workerCount = 1;
int workerCount = userPrefs.workerCount();
JavaSrcModule wsdefdefJavaModule = null;
JavaSrcModule wsdefJavaModule = null;
WorkspaceModuleContext wsdefCtx = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package org.fluentjava.iwant.embedded;

import java.io.File;
import java.util.concurrent.atomic.AtomicReference;

import org.fluentjava.iwant.api.core.Concatenated;
import org.fluentjava.iwant.api.core.HelloTarget;
import org.fluentjava.iwant.api.model.TargetEvaluationContext;
import org.fluentjava.iwant.api.target.TargetBase;
import org.fluentjava.iwant.coreservices.FileUtil;
import org.fluentjava.iwant.entry.Iwant;
import org.fluentjava.iwant.entry3.UserPrefs;
import org.fluentjava.iwant.testarea.TestArea;

import junit.framework.TestCase;
Expand Down Expand Up @@ -33,4 +40,68 @@ public void testHelloAsPath() {
assertEquals("hello message", testArea.contentOf(cached));
}

public void testCustomUserPrefs() {
class ThreadDetector extends TargetBase {

AtomicReference<String> threadName = new AtomicReference<>();

public ThreadDetector(String name) {
super(name);
}

@Override
protected IngredientsAndParametersDefined ingredientsAndParameters(
IngredientsAndParametersPlease iUse) {
return iUse.nothingElse();
}

@Override
public void path(TargetEvaluationContext ctx) throws Exception {
File dest = ctx.cached(this);
FileUtil.newTextFile(dest, name());
threadName.set(Thread.currentThread().getName());
}

}

ThreadDetector td1 = new ThreadDetector("td1");
ThreadDetector td2 = new ThreadDetector("td2");

Concatenated root = Concatenated.named("root").unixPathTo(td1)
.unixPathTo(td2).end();

// 1 thread only
UserPrefs prefsFor1 = new UserPrefs() {
@Override
public int workerCount() {
return 1;
}
};

AsEmbeddedIwantUser.with().userPrefs(prefsFor1).workspaceAt(wsRoot)
.cacheAt(cacheDir).iwant().target(root).asPath();
assertEquals(td1.threadName.get(), td2.threadName.get());

// 2 threads configured => td1 and td2 are built using different
// threads. Here we don't even care if happens always, 1 out of 20 is
// good enough:

UserPrefs prefsFor2 = new UserPrefs() {
@Override
public int workerCount() {
return 2;
}
};
for (int i = 0; i < 20; i++) {
Iwant.del(cacheDir);

AsEmbeddedIwantUser.with().userPrefs(prefsFor2).workspaceAt(wsRoot)
.cacheAt(cacheDir).iwant().target(root).asPath();
if (!td1.threadName.get().equals(td2.threadName.get())) {
return;
}
}
fail("User prefs of using 2 threads were never obeyed!");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.File;

class DefaultUserPrefs implements UserPrefs {
public class DefaultUserPrefs implements UserPrefs {

private final File userPrefsFile;

Expand Down

0 comments on commit e9ccca1

Please sign in to comment.