Last active
November 12, 2024 00:46
-
-
Save wreulicke/40c4e45670ce291733534dc7430b4353 to your computer and use it in GitHub Desktop.
scoped values 試したときのメモ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tasks.withType<JavaCompile> { | |
options.compilerArgs.add("--enable-preview") | |
} | |
// Apply a specific Java toolchain to ease working on different environments. | |
java { | |
toolchain { | |
languageVersion.set(JavaLanguageVersion.of(22)) | |
} | |
} | |
tasks.named<Test>("test") { | |
// Use JUnit Platform for unit tests. | |
useJUnitPlatform() | |
jvmArgs = listOf("--enable-preview") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.wreulicke; | |
import org.junit.jupiter.api.Test; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.slf4j.spi.MDCAdapter; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Deque; | |
import java.util.Map; | |
import java.util.Properties; | |
import java.util.concurrent.Executors; | |
import java.util.concurrent.StructuredTaskScope; | |
import static org.junit.jupiter.api.Assertions.assertNotNull; | |
public class ScopedValuesTest { | |
private static final Logger log = LoggerFactory.getLogger(ScopedValuesTest.class); | |
@Test | |
void test() { | |
ScopedValue<String> value = ScopedValue.newInstance(); | |
ScopedValue.where(value, "test").run(() -> { | |
try (StructuredTaskScope.ShutdownOnFailure scope = new StructuredTaskScope.ShutdownOnFailure()) { | |
scope.fork(() -> { | |
log.info("hello"); | |
log.info(value.get()); | |
return null; | |
}); | |
scope.fork(() -> { | |
log.info("hello2"); | |
log.info(value.get()); | |
return null; | |
}); | |
scope.join(); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
Executors.newVirtualThreadPerTaskExecutor().execute(() -> { | |
log.info(value.get()); | |
}); | |
}); | |
} | |
@Test | |
void testB() throws IOException { | |
URL resource = ScopedValuesTest.class.getResource("/project.properties"); | |
assertNotNull(resource); | |
Properties properties = new Properties(); | |
properties.load(resource.openStream()); | |
assertNotNull(properties.get("test")); | |
} | |
class TestMDCAdapter implements MDCAdapter { | |
ScopedValue<Map<String, String>> context = ScopedValue.newInstance(); | |
@Override | |
public void put(String s, String s1) { | |
context.get().put(s, s1); | |
} | |
@Override | |
public String get(String s) { | |
return context.get().get(s); | |
} | |
@Override | |
public void remove(String s) { | |
context.get().remove(s); | |
} | |
@Override | |
public void clear() { | |
context.get().clear(); | |
} | |
@Override | |
public Map<String, String> getCopyOfContextMap() { | |
return Map.ofEntries(context.get().entrySet().toArray(new Map.Entry[0])); | |
} | |
@Override | |
public void setContextMap(Map<String, String> map) { | |
context.get().putAll(map); | |
} | |
@Override | |
public void pushByKey(String s, String s1) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public String popByKey(String s) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public Deque<String> getCopyOfDequeByKey(String s) { | |
throw new UnsupportedOperationException(); | |
} | |
@Override | |
public void clearDequeByKey(String s) { | |
throw new UnsupportedOperationException(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment