/
AndroidHandleManagerTest.kt
209 lines (183 loc) · 7.39 KB
/
AndroidHandleManagerTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package arcs.android.storage.handle
import android.app.Application
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import androidx.test.core.app.ActivityScenario
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.work.testing.WorkManagerTestInitHelper
import arcs.core.crdt.CrdtSet
import arcs.core.crdt.CrdtSingleton
import arcs.core.crdt.VersionMap
import arcs.core.data.FieldType
import arcs.core.data.RawEntity
import arcs.core.data.Schema
import arcs.core.data.SchemaFields
import arcs.core.data.SchemaName
import arcs.core.data.util.toReferencable
import arcs.core.storage.StorageKey
import arcs.core.storage.driver.RamDisk
import arcs.core.storage.driver.RamDiskStorageKey
import arcs.core.storage.handle.HandleManager
import arcs.core.storage.handle.SetCallbacks
import arcs.core.storage.handle.SingletonCallbacks
import arcs.core.storage.referencemode.ReferenceModeStorageKey
import arcs.sdk.android.storage.service.DefaultConnectionFactory
import arcs.sdk.android.storage.service.testutil.TestBindingDelegate
import com.google.common.truth.Truth.assertThat
import com.nhaarman.mockitokotlin2.mock
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.runBlockingTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
@Suppress("EXPERIMENTAL_API_USAGE")
@RunWith(AndroidJUnit4::class)
class AndroidHandleManagerTest : LifecycleOwner {
private lateinit var lifecycle: LifecycleRegistry
override fun getLifecycle() = lifecycle
private lateinit var app: Application
private lateinit var handleManager: HandleManager
val entity1 = RawEntity(
"entity1",
singletons = mapOf(
"name" to "Jason".toReferencable(),
"age" to 21.toReferencable(),
"is_cool" to false.toReferencable()
),
collections = emptyMap()
)
val entity2 = RawEntity(
"entity2",
singletons = mapOf(
"name" to "Jason".toReferencable(),
"age" to 22.toReferencable(),
"is_cool" to true.toReferencable()
),
collections = emptyMap()
)
private val schema = Schema(
listOf(SchemaName("Person")),
SchemaFields(
singletons = mapOf(
"name" to FieldType.Text,
"age" to FieldType.Number,
"is_cool" to FieldType.Boolean
),
collections = emptyMap()
),
"1234acf"
)
private val singletonKey = ReferenceModeStorageKey(
backingKey = RamDiskStorageKey("single-back"),
storageKey = RamDiskStorageKey("single-ent")
)
private val setKey = ReferenceModeStorageKey(
backingKey = RamDiskStorageKey("set-back"),
storageKey = RamDiskStorageKey("set-ent")
)
@Before
fun setUp() {
RamDisk.clear()
lifecycle = LifecycleRegistry(this).apply {
setCurrentState(Lifecycle.State.CREATED)
setCurrentState(Lifecycle.State.STARTED)
setCurrentState(Lifecycle.State.RESUMED)
}
app = ApplicationProvider.getApplicationContext()
// Initialize WorkManager for instrumentation tests.
WorkManagerTestInitHelper.initializeTestWorkManager(app)
handleManager = AndroidHandleManager(
lifecycle = lifecycle,
context = app,
connectionFactory = DefaultConnectionFactory(app, TestBindingDelegate(app))
)
}
@Test
fun singleton_writeAndReadback() = runBlocking {
val singletonHandle = handleManager.singletonHandle(singletonKey, schema)
singletonHandle.store(entity1)
// Now read back from a different handle
val readbackHandle = handleManager.singletonHandle(singletonKey, schema)
val readBack = readbackHandle.fetch()
assertThat(readBack).isEqualTo(entity1)
}
@Test
fun set_writeAndReadback() = runBlocking<Unit> {
val setHandle = handleManager.setHandle(setKey, schema)
setHandle.store(entity1)
setHandle.store(entity2)
// Now read back from a different handle
val secondHandle = handleManager.setHandle(setKey, schema)
val readBack = secondHandle.fetchAll()
assertThat(readBack).containsExactly(entity1, entity2)
}
private fun testMapForKey(key: StorageKey) = VersionMap(key.toKeyString() to 1)
@Test
fun set_onHandleUpdate() = runBlocking<Unit> {
val testCallback1 = mock<SetCallbacks<RawEntity>>()
val testCallback2 = mock<SetCallbacks<RawEntity>>()
val firstHandle = handleManager.setHandle(setKey, schema, testCallback1)
val secondHandle = handleManager.setHandle(setKey, schema, testCallback2)
val expectedAdd = CrdtSet.Operation.Add(
setKey.toKeyString(),
testMapForKey(setKey),
entity1
)
secondHandle.store(entity1)
verify(testCallback1, times(1)).onUpdate(firstHandle, expectedAdd)
verify(testCallback2, times(1)).onUpdate(secondHandle, expectedAdd)
firstHandle.remove(entity1)
val expectedRemove = CrdtSet.Operation.Remove(
setKey.toKeyString(),
testMapForKey(setKey),
entity1
)
verify(testCallback1, times(1)).onUpdate(firstHandle, expectedRemove)
verify(testCallback2, times(1)).onUpdate(secondHandle, expectedRemove)
}
@Test
fun singleton_OnHandleUpdate() = runBlocking<Unit> {
val testCallback1 = mock<SingletonCallbacks<RawEntity>>()
val testCallback2 = mock<SingletonCallbacks<RawEntity>>()
val firstHandle = handleManager.singletonHandle(singletonKey, schema, testCallback1)
val secondHandle = handleManager.singletonHandle(singletonKey, schema, testCallback2)
secondHandle.store(entity1)
val expectedAdd = CrdtSingleton.Operation.Update(
singletonKey.toKeyString(),
testMapForKey(singletonKey),
entity1
)
verify(testCallback1, times(1)).onUpdate(firstHandle, expectedAdd)
verify(testCallback2, times(1)).onUpdate(secondHandle, expectedAdd)
firstHandle.clear()
val expectedRemove = CrdtSingleton.Operation.Clear<RawEntity>(
singletonKey.toKeyString(),
testMapForKey(singletonKey)
)
verify(testCallback1, times(1)).onUpdate(firstHandle, expectedRemove)
verify(testCallback2, times(1)).onUpdate(secondHandle, expectedRemove)
}
@Test
fun set_syncOnRegister() = runBlocking<Unit> {
val testCallback = mock<SetCallbacks<RawEntity>>()
val firstHandle = handleManager.setHandle(setKey, schema, testCallback)
verify(testCallback, times(1)).onSync(firstHandle)
firstHandle.fetchAll()
verify(testCallback, times(1)).onSync(firstHandle)
}
@Test
fun testSingletonSyncOnRegister() = runBlocking<Unit> {
val testCallback = mock<SingletonCallbacks<RawEntity>>()
val firstHandle = handleManager.singletonHandle(setKey, schema, testCallback)
verify(testCallback, times(1)).onSync(firstHandle)
firstHandle.fetch()
verify(testCallback, times(1)).onSync(firstHandle)
}
}