diff --git a/essential/iwant-embedded/src/main/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUser.java b/essential/iwant-embedded/src/main/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUser.java index c5bf0950..f4a38775 100644 --- a/essential/iwant-embedded/src/main/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUser.java +++ b/essential/iwant-embedded/src/main/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUser.java @@ -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; /** @@ -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; @@ -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(); } @@ -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; diff --git a/essential/iwant-embedded/src/test/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUserTest.java b/essential/iwant-embedded/src/test/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUserTest.java index 0f941564..f42156bb 100644 --- a/essential/iwant-embedded/src/test/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUserTest.java +++ b/essential/iwant-embedded/src/test/java/org/fluentjava/iwant/embedded/AsEmbeddedIwantUserTest.java @@ -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; @@ -33,4 +40,68 @@ public void testHelloAsPath() { assertEquals("hello message", testArea.contentOf(cached)); } + public void testCustomUserPrefs() { + class ThreadDetector extends TargetBase { + + AtomicReference 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!"); + } + } diff --git a/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/DefaultUserPrefs.java b/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/DefaultUserPrefs.java index 492ecc8c..5f39500e 100644 --- a/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/DefaultUserPrefs.java +++ b/essential/iwant-entry3/src/main/java/org/fluentjava/iwant/entry3/DefaultUserPrefs.java @@ -2,7 +2,7 @@ import java.io.File; -class DefaultUserPrefs implements UserPrefs { +public class DefaultUserPrefs implements UserPrefs { private final File userPrefsFile;