Fix cache test
This commit is contained in:
parent
4e590d3e14
commit
d3689638e0
@ -87,9 +87,8 @@ class CachePlugin(meta: Meta) : BasicPlugin(meta) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Synchronized
|
||||||
override fun run() {
|
override fun run() {
|
||||||
//TODO add executor
|
|
||||||
synchronized(cache) {
|
|
||||||
when {
|
when {
|
||||||
data.goal.isDone -> data.future.thenAccept { result.complete(it) }
|
data.goal.isDone -> data.future.thenAccept { result.complete(it) }
|
||||||
cache.containsKey(id) -> {
|
cache.containsKey(id) -> {
|
||||||
@ -109,7 +108,6 @@ class CachePlugin(meta: Meta) : BasicPlugin(meta) {
|
|||||||
else -> evalData()
|
else -> evalData()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private fun evalData() {
|
private fun evalData() {
|
||||||
data.goal.run()
|
data.goal.run()
|
||||||
@ -162,7 +160,7 @@ class CachePlugin(meta: Meta) : BasicPlugin(meta) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun <V : Any> cacheNode(cacheName: String, node: DataNode<V>, idFactory: (NamedData<*>) -> Meta): DataNode<V> {
|
fun <V : Any> cacheNode(cacheName: String, node: DataNode<V>, idFactory: (NamedData<*>) -> Meta): DataNode<V> {
|
||||||
val builder = DataTree.edit(node.type).also {cached->
|
val builder = DataTree.edit(node.type).also { cached ->
|
||||||
cached.name = node.name
|
cached.name = node.name
|
||||||
cached.meta = node.meta
|
cached.meta = node.meta
|
||||||
//recursively caching everything
|
//recursively caching everything
|
||||||
|
@ -175,9 +175,7 @@ interface DataNode<T : Any> : Iterable<NamedData<out T>>, Named, Metoid, Provide
|
|||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
fun nodeGoal(): GoalGroup {
|
fun nodeGoal(): GoalGroup = GoalGroup(dataStream().map { it.goal }.toList())
|
||||||
return GoalGroup(this.dataStream().map { it.goal }.toList())
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle result when the node is evaluated. Does not trigger node evaluation. Ignores exceptional completion
|
* Handle result when the node is evaluated. Does not trigger node evaluation. Ignores exceptional completion
|
||||||
|
@ -71,9 +71,8 @@ abstract class AbstractWorkspace(
|
|||||||
/**
|
/**
|
||||||
* Put given data node into cache one by one
|
* Put given data node into cache one by one
|
||||||
*/
|
*/
|
||||||
private fun <R : Any> cacheTaskResult(model: TaskModel, node: DataNode<out R>): DataNode<out R> {
|
private fun <R : Any> cacheTaskResult(model: TaskModel, node: DataNode<out R>): DataNode<out R> =
|
||||||
return cache.cacheNode(model.name, node) { model.getID(it) }
|
cache.cacheNode(model.name, node) { model.getID(it) }
|
||||||
}
|
|
||||||
|
|
||||||
override fun clean() {
|
override fun clean() {
|
||||||
logger.info("Cleaning up cache...")
|
logger.info("Cleaning up cache...")
|
||||||
|
@ -24,6 +24,7 @@ import hep.dataforge.meta.MetaBuilder
|
|||||||
import hep.dataforge.workspace.tasks.PipeTask
|
import hep.dataforge.workspace.tasks.PipeTask
|
||||||
import hep.dataforge.workspace.tasks.TaskModel
|
import hep.dataforge.workspace.tasks.TaskModel
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
|
import org.junit.Before
|
||||||
import org.junit.BeforeClass
|
import org.junit.BeforeClass
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
@ -49,17 +50,23 @@ class WorkspaceTest {
|
|||||||
val res2 = wsp.runTask("test2", Meta.empty())
|
val res2 = wsp.runTask("test2", Meta.empty())
|
||||||
res1.computeAll()
|
res1.computeAll()
|
||||||
res2.computeAll()
|
res2.computeAll()
|
||||||
assertEquals(6, counter.get().toLong())
|
assertEquals(6, counter.get())
|
||||||
val res3 = wsp.runTask("test2", MetaBuilder().putValue("a", 1))
|
val res3 = wsp.runTask("test2", MetaBuilder().putValue("a", 1))
|
||||||
.getCheckedData("data_2", Number::class.java).get().toLong()
|
.getCheckedData("data_2", Number::class.java).get().toLong()
|
||||||
assertEquals(6, res3)
|
assertEquals(6, res3)
|
||||||
assertEquals(8, counter.get().toLong())
|
assertEquals(8, counter.get())
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val counter = AtomicInteger()
|
private val counter = AtomicInteger()
|
||||||
private lateinit var wsp: Workspace
|
private lateinit var wsp: Workspace
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun beforeEach(){
|
||||||
|
counter.set(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun setup() {
|
fun setup() {
|
||||||
@ -67,19 +74,13 @@ class WorkspaceTest {
|
|||||||
load(CachePlugin::class.java, MetaBuilder().setValue("fileCache.enabled", false))
|
load(CachePlugin::class.java, MetaBuilder().setValue("fileCache.enabled", false))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
val task1 = object : PipeTask<Number, Number>("test1", Number::class.java, Number::class.java) {
|
val task1 = object : PipeTask<Number, Number>("test1", Number::class.java, Number::class.java) {
|
||||||
override fun buildModel(model: TaskModel.Builder, meta: Meta) {
|
override fun buildModel(model: TaskModel.Builder, meta: Meta) {
|
||||||
model.data("*")
|
model.data("*")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun result(context: Context, name: String, input: Number, meta: Meta): Number {
|
override fun result(context: Context, name: String, input: Number, meta: Meta): Number {
|
||||||
try {
|
|
||||||
Thread.sleep(200)
|
Thread.sleep(200)
|
||||||
} catch (e: InterruptedException) {
|
|
||||||
throw RuntimeException(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
counter.incrementAndGet()
|
counter.incrementAndGet()
|
||||||
return input.toInt() + meta.getInt("a", 2)
|
return input.toInt() + meta.getInt("a", 2)
|
||||||
}
|
}
|
||||||
@ -91,12 +92,7 @@ class WorkspaceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun result(context: Context, name: String, input: Number, meta: Meta): Number {
|
override fun result(context: Context, name: String, input: Number, meta: Meta): Number {
|
||||||
try {
|
|
||||||
Thread.sleep(200)
|
Thread.sleep(200)
|
||||||
} catch (e: InterruptedException) {
|
|
||||||
throw RuntimeException(e)
|
|
||||||
}
|
|
||||||
|
|
||||||
counter.incrementAndGet()
|
counter.incrementAndGet()
|
||||||
return input.toInt() * meta.getInt("b", 2)
|
return input.toInt() * meta.getInt("b", 2)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user