Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(ts) Runtime de-globalling (step 3) #6758

Draft
wants to merge 32 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
3afe810
modernize dev-shell
Nov 6, 2020
a908f48
tweaks
Nov 6, 2020
a093036
law of Demeter
Nov 6, 2020
193c1a7
remove storageService
Nov 12, 2020
4dad548
cleanups
Nov 12, 2020
09fd6b6
surgically remove static (global) runtime object
Nov 17, 2020
32e20ae
clean ups
Nov 18, 2020
1987f50
big runtime usage refactor
Nov 20, 2020
16a0961
cleanups
Nov 20, 2020
ab675d4
cleanups
Nov 20, 2020
328ac92
serious degloballing
Nov 26, 2020
4978ea0
repairs after unpleasant rebase
Dec 8, 2020
1900fe2
remove debugger statement
Dec 8, 2020
00505f1
sleuthing test failures
Dec 8, 2020
6a9f14d
Don't call idle immediately after closing StorageEndpoint
jblebrun Dec 8, 2020
9482eef
Refactors several classes to use injected StorageKeyManager instances…
csilvestrini Dec 8, 2020
7f2ea17
rename EntitySpecTest.kt to javatests/arcs/sdk/GeneratedEntityTest.kt
galganif Dec 8, 2020
7f5e9f6
Fix typo
csilvestrini Dec 8, 2020
2aff690
Tests for BaseHandle.kt
Dec 8, 2020
bdfdce5
Add missing strict deps for type aliases.
arcs-c3po Dec 8, 2020
b4bb177
rename ReferenceSpecTest.kt to javatests/arcs/sdk/GeneratedReferenceT…
galganif Dec 8, 2020
7ebf1f0
add support for inline schema field inside ordered list in fromLitera…
mariakleiner Dec 8, 2020
b81fbd2
Rename EntityHandleManager to HandleManagerImpl to reflect the actual…
yuangu-google Dec 8, 2020
d5242e8
Add more unit tests for DirectStore.
yuangu-google Dec 8, 2020
f1092db
Caching RawEntity hashCode to improve overhead.
alxmrs Dec 9, 2020
408388c
Add error message for CollectionHandle init (#6690)
SHeimlich Dec 9, 2020
0557c0b
Refactor mock helper methods from the Handle tests into testutil/
Dec 9, 2020
be944aa
Remove unused methods from EntityHandleManager and StorageProxy.
Dec 9, 2020
0e2bf53
Create showcase & showcase test for queries
Dec 9, 2020
c9f4166
Disable import ordering in ktlint (#6749)
csilvestrini Dec 10, 2020
75c791e
remove dead code
Dec 10, 2020
4b6bc47
Merge branch 'master' of github.com:PolymerLabs/arcs into dec-dev-she…
Dec 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create showcase & showcase test for queries
PiperOrigin-RevId: 346459397
  • Loading branch information
Josh Pratt authored and Scott J. Miles committed Dec 10, 2020
commit 0e2bf53e6e90a774f1ab6bff450c6e3d744b71e1
3 changes: 0 additions & 3 deletions javatests/arcs/core/allocator/AllocatorTestBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ open class AllocatorTestBase {
private val schedulerProvider = SimpleSchedulerProvider(Dispatchers.Default)
private lateinit var scope: CoroutineScope

/**
* Recipe hand translated from 'person.arcs'
*/
protected lateinit var allocator: Allocator
private lateinit var hostRegistry: HostRegistry
private lateinit var writePersonParticle: Plan.Particle
Expand Down
51 changes: 51 additions & 0 deletions javatests/arcs/showcase/queries/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
load(
"//third_party/java/arcs/build_defs:build_defs.bzl",
"arcs_kt_android_library",
"arcs_kt_android_test_suite",
"arcs_kt_gen",
)

licenses(["notice"])

arcs_kt_gen(
name = "codegen",
srcs = ["queries.arcs"],
)

arcs_kt_android_library(
name = "particles",
testonly = 1,
srcs = [
"ProductClassifier.kt",
"ProductDatabase.kt",
],
deps = [
":codegen",
"//java/arcs/android/storage/database",
"//java/arcs/core/entity",
"//java/arcs/jvm/host",
"//java/arcs/sdk/android/storage",
"//java/arcs/sdk/android/storage/service",
"//javatests/arcs/android/integration",
],
)

arcs_kt_android_test_suite(
name = "tests",
size = "small",
srcs = glob(["*Test.kt"]),
manifest = "//java/arcs/android/common:AndroidManifest.xml",
package = "arcs.showcase.queries",
deps = [
":codegen",
":particles",
"//java/arcs/core/host",
"//java/arcs/core/testutil",
"//java/arcs/core/testutil/handles",
"//javatests/arcs/android/integration",
"//third_party/android/androidx_test/ext/junit",
"//third_party/java/junit:junit-android",
"//third_party/java/truth:truth-android",
"//third_party/kotlin/kotlinx_coroutines",
],
)
60 changes: 60 additions & 0 deletions javatests/arcs/showcase/queries/ProductClassifier.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package arcs.showcase.queries

import arcs.jvm.host.TargetHost

/**
* This particle is an example of using handles with associated queries.
* It performs three different queries which select products based on values provided at runtime:
* - an arbitrary double [CHEAP_PRICE]
* - an arbitrary double [EXPENSIVE_PRICE], and
* - an arbitrary string [SEARCH_NAME].
*
* It filters dummy data from [ProductDatabase], but could be used with any other source of data and
* performs a simple labelling task showing how data can be combined from different handles without
* loading the whole database into the Particle.
*/
@TargetHost(arcs.android.integration.IntegrationHost::class)
class ProductClassifier : AbstractProductClassifier() {

override fun onReady() {
// This map from product names to 'tags' accumulates the output that this particle provides.
val productDescriptions = mutableMapOf<String, MutableSet<String>>()

// Here, 'cheap' products are defined as products from the [cheapProducts] handle that have a
// price less than or equal to [CHEAP_PRICE].
// i.e. `price <= ?` see the definition in [queries.arcs]
handles.cheapProducts.query(CHEAP_PRICE).forEach {
productDescriptions.getOrPut(it.name) { mutableSetOf() }.add("cheap")
}
// Here, 'expensive' products are defined as products from the [expensiveProducts] handle that have a
// price greater than or equal to [EXPENSIVE_PRICE].
// i.e. `price >= ?` see the definition in [queries.arcs]
handles.expensiveProducts.query(EXPENSIVE_PRICE).forEach {
productDescriptions.getOrPut(it.name) { mutableSetOf() }.add("expensive")
}
// Here, 'named' products are defined as products from the [namedProducts] handle that have the
// name equal to [SEARCH_NAME].
// i.e. `name == ?` see the definition in [queries.arcs]
handles.namedProducts.query(SEARCH_NAME).forEach {
productDescriptions.getOrPut(it.name) { mutableSetOf() }.add("selected")
}

// This renders the 'tags' and product names as strings and stores them using the
// [productDescriptions] handle.
handles.productDescriptions.storeAll(
productDescriptions.map { (name, tags) ->
ProductDescription(description = "$name: ${tags.joinToString()}")
}
)
}

companion object {
/**
* These constants are arbitrary values to show that query arguments are provided at runtime.
* They do not actually need to be constants and could be changed at runtime.
*/
private const val CHEAP_PRICE = 3.0
private const val EXPENSIVE_PRICE = 25.0
private const val SEARCH_NAME = "Pencil"
}
}
27 changes: 27 additions & 0 deletions javatests/arcs/showcase/queries/ProductDatabase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package arcs.showcase.queries

import arcs.jvm.host.TargetHost

typealias Product = AbstractProductDatabase.Product

/**
* This particle generates dummy data that is used in testing queries.
* @see ProductClassifier
*/
@TargetHost(arcs.android.integration.IntegrationHost::class)
class ProductDatabase : AbstractProductDatabase() {
override fun onFirstStart() {
handles.products.storeAll(
listOf(
Product(name = "Pencil", price = 2.5),
Product(name = "Ice cream", price = 3.0),
Product(name = "Chocolate", price = 3.0),
Product(name = "Blueberries", price = 4.0),
Product(name = "Sandwich", price = 4.50),
Product(name = "Scarf", price = 20.0),
Product(name = "Hat", price = 25.0),
Product(name = "Stop sign", price = 100.0)
)
)
}
}
46 changes: 46 additions & 0 deletions javatests/arcs/showcase/queries/QueriesShowcaseTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package arcs.showcase.queries

import androidx.test.ext.junit.runners.AndroidJUnit4
import arcs.android.integration.IntegrationEnvironment
import arcs.core.host.toRegistration
import arcs.core.testutil.handles.dispatchFetchAll
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class QueriesShowcaseTest {

@get:Rule
val env = IntegrationEnvironment(
::ProductClassifier.toRegistration(),
::ProductDatabase.toRegistration()
)

/**
* Tests that a set of known queries, on known runtime data produce expected results.
* - Products are defined in [ProductDatabase],
* - filtering criteria are defined in [ProductClassifier].
*/
@Test
fun testQueries() = runBlocking {
val arc = env.startArc(ClassifyProductsPlan)
val productClassifier: ProductClassifier = env.getParticle<ProductClassifier>(arc)
val productDescriptions = productClassifier.handles.productDescriptions.dispatchFetchAll()
val descriptions = productDescriptions.map { it.description }

assertThat(descriptions).containsExactly(
"Pencil: cheap, selected",
"Ice cream: cheap",
"Chocolate: cheap",
"Hat: expensive",
"Stop sign: expensive"
)

env.stopArc(arc)
}
}
27 changes: 27 additions & 0 deletions javatests/arcs/showcase/queries/queries.arcs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
meta
namespace: arcs.showcase.queries

schema Product
name: Text
price: Number

particle ProductDatabase in '.ProductDatabase'
products: writes [Product {name, price}]

particle ProductClassifier in '.ProductClassifier'
cheapProducts: reads [Product {name, price} [price <= ?]]
expensiveProducts: reads [Product {name, price} [price >= ?]]
namedProducts: reads [Product {name, price} [name == ?]]
productDescriptions: reads writes [ProductDescription {description: Text}] // reads is only for testing.

@arcId('testArc')
recipe ClassifyProducts
products: create 'products'
productDescriptions: create 'productDescriptions'
ProductDatabase
products: products
ProductClassifier
cheapProducts: products
expensiveProducts: products
namedProducts: products
productDescriptions: productDescriptions