-
Notifications
You must be signed in to change notification settings - Fork 5
/
ResilientCachesIT.scala
434 lines (333 loc) · 17.7 KB
/
ResilientCachesIT.scala
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package com.wix.hoopoe.koboshi
import java.io.File
import java.net.URL
import java.util.concurrent.{ScheduledExecutorService, TimeUnit}
import better.files.{File => BetterFile}
import ch.qos.logback.classic.Level
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.joda.JodaModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.wix.hoopoe.koboshi.RemoteDataFetchingLogDriver.aRemoteDataFetchingLogDriver
import com.wix.hoopoe.koboshi.ResilientCachesIT._
import com.wix.hoopoe.koboshi.SchedulingContext.SchedulingAugmentor
import com.wix.hoopoe.koboshi.cache.persistence.{FolderPersistentCaches, PersistentCache}
import com.wix.hoopoe.koboshi.cache.{ReadOnlyLocalCache, ResilientCacheBuilder, _}
import com.wix.hoopoe.koboshi.example.SomeDataType
import com.wix.hoopoe.koboshi.marshaller.{JacksonMarshaller, JacksonMarshallers}
import com.wix.hoopoe.koboshi.namespace.NamespaceCreator.cacheNamespace
import com.wix.hoopoe.koboshi.registry.MapBasedRemoteDataFetcherRegistry
import com.wix.hoopoe.koboshi.remote.{BlockingRemoteDataFetcher, RemoteDataSource}
import com.wix.hoopoe.koboshi.report.{DelegatingRemoteDataFetcherReporter, FixedReporterReporters, LoggingRemoteDataFetchingReporter}
import com.wix.hoopoe.koboshi.scheduler.{FakeClock, Schedulers, SchedulingDelays}
import org.hamcrest.Matchers.{containsString, is}
import org.hamcrest.{Description, Matcher, TypeSafeMatcher}
import org.jmock.lib.concurrent.DeterministicScheduler
import org.joda.time.Instant
import org.junit.Assert._
import org.slf4j.LoggerFactory
import org.specs2.DescribeOf
import org.specs2.mutable.{BeforeAfter, SpecificationWithJUnit}
import org.specs2.specification.Scope
import scala.concurrent.duration.{Duration, _}
import scala.reflect._
class ResilientCachesIT extends SpecificationWithJUnit {
sequential
"A Resilient Cache" should {
"keep remote data available" in new Context {
val transientCacheDriver = aCacheInitializedFromRemoteWith(RemoteData)
remoteServerDriver.stopResponding()
transientCacheDriver.has(RemoteData)
}
"sync with remote source periodically" in new Context {
val transientCacheDriver = aCacheInitializedFromRemoteWith(RemoteData)
transientCacheDriver.has(RemoteData)
val differentRemoteData: SomeDataType = new SomeDataType("DIFFERENT DATA")
remoteServerDriver.respondWith(differentRemoteData)
advanceTimeToNextScheduledFetch()
transientCacheDriver.has(differentRemoteData)
}
"retrieve data from disk on init" in new Context {
persistentCacheDriver.setTo(InitialLocalData)
val transientCacheDriver = aLocalInitializedCacheDriver()
transientCacheDriver.has(InitialLocalData)
}
"update the persistent cache on init if no persistent cache exists" in new Context {
aCacheInitializedFromRemoteWith(RemoteData)
persistentCacheDriver.has(RemoteData)
}
"update the persistent cache after sync with remote" in new Context {
persistentCacheDriver.setTo(InitialLocalData)
aCacheInitializedFromRemoteWith(RemoteData)
advanceTimeBy(defaultSchedulingDelays.initialDelayWhenHaveNotSyncedWithRemote)
persistentCacheDriver.has(RemoteData)
}
"report failure when scheduled remote fetching fails" in new Context with LogReporting {
aLocalInitializedCacheDriver()
remoteServerDriver.stopResponding()
advanceTimeToNextScheduledFetch()
reportsRemoteFetchingFailureFor[SomeDataType]()
}
"reflect last successful data time" in new Context {
setTimeTo(APointInTime)
val transientCacheDriver = aTimestampedCacheInitializedFromRemote()
transientCacheDriver.has(remoteDataWithTimestamp(APointInTime))
}
"reflect last successful data time when remote fetching fails" in new Context {
setTimeTo(APointInTime)
val transientCacheDriver = aTimestampedCacheInitializedFromRemote()
val aLaterPointInTime: Instant = APointInTime.plus(SomeTime)
setTimeTo(aLaterPointInTime)
remoteServerDriver.stopResponding()
advanceTimeToNextScheduledFetch()
transientCacheDriver.has(remoteDataWithTimestamp(APointInTime))
}
"reflect the data timestamp when loading from the persistent cache" in new Context {
persistentCacheDriver.setTo(new TimestampedData[SomeDataType](InitialLocalData, APointInTime))
val transientCacheDriver = aTimestampedLocalInitializedCacheDriver()
transientCacheDriver.has(dataWithTimestamp(APointInTime, InitialLocalData))
}
"persist the data timestamp to the persistent cache" in new Context {
remoteServerDriver.respondWith(RemoteData)
setTimeTo(APointInTime)
val transientCacheDriver = aTimestampedLocalInitializedCacheDriver()
persistentCacheDriver.has(dataWithTimestamp(APointInTime, RemoteData))
}
"report persistent cache activities" in new Context with LogReporting {
val transientCacheDriver = aLocalInitializedCacheDriver()
remoteDataFetchingLogDriver.reports(Level.TRACE, containsString(disk.toJava.getPath))
}
}
}
trait SchedulingContractTest extends SpecificationWithJUnit with DescribeOf {
sequential
val schedulingDelays: SchedulingDelays
implicit def augmentor: SchedulingAugmentor
import schedulingDelays._
"A Resilient Cache's scheduling" should {
"have a first sync delay" of {
s"$initialDelayWhenHaveNotSyncedWithRemote when initialized from persistent cache" in new SchedulingContext {
val transientCacheDriver = aCacheInitializedFromPersistentCopy()
remoteServerDriver.respondWith(RemoteData)
advanceTimeBy(initialDelayWhenHaveNotSyncedWithRemote)
transientCacheDriver.has(RemoteData)
}
s"$initialDelayWhenHaveNotSyncedWithRemote when initialized without data" in new SchedulingContext {
val transientCacheDriver = aCacheInitializedWithoutData()
remoteServerDriver.respondWith(RemoteData)
advanceTimeBy(initialDelayWhenHaveNotSyncedWithRemote)
transientCacheDriver.has(RemoteData)
}
s"$initialFetchDelayWhenSyncedWithRemote when initialized from remote" in new SchedulingContext {
val transientCacheDriver = aCacheInitializedFromRemoteWith(RemoteData)
remoteServerDriver.respondWith(DifferentData)
private val smallTime = initialFetchDelayWhenSyncedWithRemote * epsilon
advanceTimeBy(initialFetchDelayWhenSyncedWithRemote - smallTime)
transientCacheDriver.has(RemoteData)
advanceTimeBy(smallTime)
transientCacheDriver.has(DifferentData)
}
}
//do the -1 trick for the tests that don't already do it
"have an ongoing sync delay" of {
s"$nextFetchDelayWhenHaveNotSyncedWithRemote when initialized from persistent cache and initial sync failed" in new SchedulingContext {
val transientCacheDriver = aCacheInitializedFromPersistentCopy()
initialSyncFails(schedulingDelays)
remoteServerDriver.respondWith(DifferentData)
advanceTimeBy(nextFetchDelayWhenHaveNotSyncedWithRemote)
transientCacheDriver.has(DifferentData)
}
s"$nextFetchDelayWhenSyncedWithRemote when initialized from persistent cache and initial sync succeeds" in new SchedulingContext {
val transientCacheDriver = aCacheInitializedFromPersistentCopy()
initialSyncSucceedsWith(DifferentData, schedulingDelays)
remoteServerDriver.respondWith(YetAnotherDifferentData)
private val smallTime = nextFetchDelayWhenSyncedWithRemote * epsilon
advanceTimeBy(nextFetchDelayWhenSyncedWithRemote - smallTime)
transientCacheDriver.has(DifferentData)
advanceTimeBy(smallTime)
transientCacheDriver.has(YetAnotherDifferentData)
}
s"$nextFetchDelayWhenHaveNotSyncedWithRemote when initialized without data and initial sync failed" in new SchedulingContext {
val transientCacheDriver = aCacheInitializedWithoutData()
initialSyncFails(schedulingDelays)
remoteServerDriver.respondWith(DifferentData)
advanceTimeBy(nextFetchDelayWhenHaveNotSyncedWithRemote)
transientCacheDriver.has(DifferentData)
}
s"$nextFetchDelayWhenSyncedWithRemote when initialized without data and initial sync succeeds" in new SchedulingContext {
val transientCacheDriver = aCacheInitializedWithoutData()
initialSyncSucceedsWith(DifferentData, schedulingDelays)
remoteServerDriver.respondWith(YetAnotherDifferentData)
private val smallTime = nextFetchDelayWhenSyncedWithRemote * epsilon
advanceTimeBy(nextFetchDelayWhenSyncedWithRemote - smallTime)
transientCacheDriver.has(DifferentData)
advanceTimeBy(smallTime)
transientCacheDriver.has(YetAnotherDifferentData)
}
s"$nextFetchDelayWhenSyncedWithRemote when initialized from remote and initial sync succeeds" in new SchedulingContext {
val transientCacheDriver = aCacheInitializedFromRemote()
initialSyncSucceedsWith(DifferentData, schedulingDelays)
remoteServerDriver.respondWith(YetAnotherDifferentData)
private val smallTime = nextFetchDelayWhenSyncedWithRemote * epsilon
advanceTimeBy(nextFetchDelayWhenSyncedWithRemote - smallTime)
transientCacheDriver.has(DifferentData)
advanceTimeBy(smallTime)
transientCacheDriver.has(YetAnotherDifferentData)
override def initialSyncSucceedsWith(data: SomeDataType, delays: SchedulingDelays): Unit = {
remoteServerDriver.respondWith(data)
advanceTimeBy(delays.initialFetchDelayWhenSyncedWithRemote)
}
}
}
}
}
class DefaultSchedulingTest extends SchedulingContractTest {
val schedulingDelays: SchedulingDelays = SchedulingDelays.Default
override implicit def augmentor: SchedulingAugmentor = SchedulingAugmentor.identity
}
class CustomSchedulingTest extends SchedulingContractTest {
val schedulingDelays: SchedulingDelays = SchedulingDelays(
initialFetchDelayWhenSyncedWithRemote = 10.minutes,
nextFetchDelayWhenSyncedWithRemote = 10.minutes,
nextFetchDelayWhenHaveNotSyncedWithRemote = 3.minutes
)
override implicit def augmentor: SchedulingAugmentor = SchedulingAugmentor(_.withCustomSchedulingDelays(schedulingDelays))
}
abstract class SchedulingContext(implicit augmentor: SchedulingAugmentor) extends Context {
override def aLocalInitializedCacheDriver(): ReadOnlyCacheDriver[SomeDataType] = {
val scheduledBuilder = augmentor.augment(aTestResilientCacheBuilder)
new ReadOnlyCacheDriver[SomeDataType](scheduledBuilder.build())
}
}
object SchedulingContext {
type CacheBuilder = ResilientCacheBuilder[SomeDataType, ReadOnlyLocalCache]
trait SchedulingAugmentor {
def augment(builder: CacheBuilder): CacheBuilder
}
object SchedulingAugmentor {
def identity: SchedulingAugmentor = SchedulingAugmentor(Predef.identity)
def apply(f: CacheBuilder => CacheBuilder): SchedulingAugmentor = new SchedulingAugmentor {
override def augment(builder: CacheBuilder): CacheBuilder = f(builder)
}
}
}
trait Context extends Scope with BeforeAfter with Log {
val defaultSchedulingDelays = SchedulingDelays()
def initialSyncFails(delays: SchedulingDelays): Unit = {
remoteServerDriver.stopResponding()
advanceTimeBy(delays.initialDelayWhenHaveNotSyncedWithRemote)
}
def initialSyncSucceedsWith(data: SomeDataType, delays: SchedulingDelays): Unit = {
remoteServerDriver.respondWith(data)
advanceTimeBy(delays.initialDelayWhenHaveNotSyncedWithRemote)
}
def aCacheInitializedFromRemote(): ReadOnlyCacheDriver[SomeDataType] =
aCacheInitializedFromRemoteWith(RemoteData)
def aCacheInitializedFromRemoteWith(data: SomeDataType): ReadOnlyCacheDriver[SomeDataType] = {
remoteServerDriver.respondWith(data)
aLocalInitializedCacheDriver()
}
def aTimestampedCacheInitializedFromRemote(): ReadOnlyTimestampedCacheDriver[SomeDataType] = {
remoteServerDriver.respondWith(RemoteData)
aTimestampedLocalInitializedCacheDriver()
}
def aCacheInitializedWithoutData(): ReadOnlyCacheDriver[SomeDataType] = {
remoteServerDriver.stopResponding()
//the persistent cache is empty by default
aLocalInitializedCacheDriver()
}
def aCacheInitializedFromPersistentCopy(): ReadOnlyCacheDriver[SomeDataType] = {
persistentCacheDriver.setTo(InitialLocalData)
aLocalInitializedCacheDriver()
}
val clock = new FakeClock(InitialTime)
val remoteServerDriver = new RemoteServerDriver
val scheduler = new DeterministicScheduler
val disk = BetterFile.newTemporaryDirectory(prefix = "koboshi")
val persistentCacheDriver = aPersistentCacheDriver(disk.toJava)
val resilientCaches = new CustomizableResilientCaches(new MapBasedRemoteDataFetcherRegistry,
new FixedSchedulerSchedulers(scheduler),
new FixedReporterReporters(new LoggingRemoteDataFetchingReporter(logger, cacheNamespace[SomeDataType])),
new FolderPersistentCaches(disk.toJava),
new JacksonMarshallers(aScalaAndJodaSupportingObjectMapper),
clock)
def before: Unit = {
deletePersistentCacheLeftovers(disk)
remoteServerDriver.start()
}
def after: Unit = {
remoteServerDriver.stop()
}
def aLocalInitializedCacheDriver(): ReadOnlyCacheDriver[SomeDataType] =
new ReadOnlyCacheDriver[SomeDataType](resilientCaches.aResilientInitializedCache(testDataSourceFor(remoteServerDriver)))
def aTimestampedLocalInitializedCacheDriver(): ReadOnlyTimestampedCacheDriver[SomeDataType] =
new ReadOnlyTimestampedCacheDriver[SomeDataType](aTestResilientCacheBuilder.withTimestampedData().build())
def aTestResilientCacheBuilder: ResilientCacheBuilder[SomeDataType, ReadOnlyLocalCache] =
resilientCaches.aResilientCacheBuilder(testDataSourceFor(remoteServerDriver))
def setTimeTo(time: Instant): Unit = clock.setCurrent(time)
def advanceTimeToNextScheduledFetch(): Unit = advanceTimeBy(defaultSchedulingDelays.nextFetchDelayWhenSyncedWithRemote)
def advanceTimeBy(duration: Duration): Unit = scheduler.tick(duration.toMillis, TimeUnit.MILLISECONDS)
}
trait Log {
val logger = LoggerFactory.getLogger(classOf[BlockingRemoteDataFetcher[SomeDataType]] + "." + classOf[SomeDataType])
}
trait LogReporting extends Log {
val remoteDataFetchingLogDriver = aRemoteDataFetchingLogDriver(logger)
def reportsRemoteFetchingFailureFor[T: ClassTag](): Unit =
remoteDataFetchingLogDriver.reports(Level.ERROR, containsString(cacheNamespace(classTag[T])))
}
object ResilientCachesIT {
val RemoteData = new SomeDataType("IRRELEVANT CONTENT")
val DifferentData = new SomeDataType("DIFFERENT DATA")
val YetAnotherDifferentData = new SomeDataType("YET ANOTHER DIFFERENT DATA")
val InitialLocalData = new SomeDataType("CACHED DATA")
val epsilon = 0.1
val InitialTime = new Instant(0)
val SomeTime = 60 * 1000
val APointInTime = InitialTime.plus(SomeTime)
class ReadOnlyCacheDriver[T](private val readOnlyLocalCache: ReadOnlyLocalCache[T]) {
def has(data: T): Unit = assertThat[T](readOnlyLocalCache.read(), is(data))
}
class PersistentCacheDriver[T](private val persistentCache: PersistentCache[T]) {
def has(data: T): Unit = assertThat[T](persistentCache.readTimestamped().data, is(data))
def has(matcher: Matcher[TimestampedData[T]]): Unit = assertThat(persistentCache.readTimestamped(), is(matcher))
def setTo(timestampedData: TimestampedData[T]): Unit = persistentCache.write(timestampedData)
def setTo(data: T): Unit = setTo(new TimestampedData[T](data, new Instant))
}
class ReadOnlyTimestampedCacheDriver[T](private val readOnlyTimestampedLocalCache: ReadOnlyTimestampedLocalCache[T]) {
def has(matcher: Matcher[TimestampedData[T]]): Unit = assertThat(readOnlyTimestampedLocalCache.readTimestamped(), is(matcher))
}
def aPersistentCacheDriver(folder: File): PersistentCacheDriver[SomeDataType] = {
val persistentCaches = new FolderPersistentCaches(folder)
val persistentCache = persistentCaches.aPersistentCache[SomeDataType](cacheNamespace[SomeDataType], new JacksonMarshaller[SomeDataType](aScalaAndJodaSupportingObjectMapper), new DelegatingRemoteDataFetcherReporter())
new PersistentCacheDriver[SomeDataType](persistentCache)
}
def aScalaAndJodaSupportingObjectMapper: ObjectMapper = new ObjectMapper().registerModule(DefaultScalaModule).registerModule(new JodaModule)
def testDataSourceFor(remoteServerDriver: RemoteServerDriver): RemoteDataSource[SomeDataType] =
new RemoteDataSource[SomeDataType]() {
val remoteUrl = new URL("http://localhost:" + remoteServerDriver.port)
val objectMapper = aScalaAndJodaSupportingObjectMapper
def fetch(): SomeDataType = objectMapper.readValue(remoteUrl, classOf[SomeDataType])
}
def remoteDataWithTimestamp(update: Instant): Matcher[TimestampedData[SomeDataType]] =
dataWithTimestamp(update, RemoteData)
def dataWithTimestamp(update: Instant, data: SomeDataType): TypeSafeMatcher[TimestampedData[SomeDataType]] =
new TypeSafeMatcher[TimestampedData[SomeDataType]]() {
protected def matchesSafely(item: TimestampedData[SomeDataType]): Boolean = {
is(data).matches(item.data) && is(update).matches(item.lastUpdate)
}
def describeTo(description: Description) {
description.appendText("data ").appendValue(data).appendText(" and update ").appendValue(update)
}
}
def deletePersistentCacheLeftovers(persistentCacheFolder: better.files.File): Unit = {
persistentCacheFolder.parent
.collectChildren(isKoboshiTempDir)
.filter(notTheCurrentTestTempDir)
.foreach(f => f.delete(swallowIOExceptions = true))
def isKoboshiTempDir(f: BetterFile) = f.name.startsWith("koboshi")
def notTheCurrentTestTempDir(f: BetterFile) = f != persistentCacheFolder
}
}
private class FixedSchedulerSchedulers(scheduler: ScheduledExecutorService) extends Schedulers {
override def aScheduler(namespace: String): ScheduledExecutorService = scheduler
}