|
1 | 1 | package com.gu.etagcaching |
2 | 2 |
|
3 | | -import com.gu.etagcaching.FreshnessPolicy.TolerateOldValueWhileRefreshing |
| 3 | +import com.gu.etagcaching.FreshnessPolicy.AlwaysWaitForRefreshedValue |
4 | 4 | import com.gu.etagcaching.Loading.Update |
5 | | -import com.gu.etagcaching.fetching.Fetching |
6 | | -import org.scalatest.OptionValues |
| 5 | +import com.gu.etagcaching.LoadingTest.TestApparatus |
| 6 | +import com.gu.etagcaching.fetching.{ETaggedData, Fetching} |
| 7 | +import com.gu.etagcaching.testkit.{CountingParser, TestFetching} |
| 8 | +import org.scalatest.OptionValues.convertOptionToValuable |
| 9 | +import org.scalatest.concurrent.ScalaFutures.convertScalaFuture |
7 | 10 | import org.scalatest.concurrent.{Eventually, ScalaFutures} |
8 | 11 | import org.scalatest.flatspec.AnyFlatSpec |
9 | 12 | import org.scalatest.matchers.should.Matchers |
| 13 | +import org.scalatest.matchers.should.Matchers._ |
| 14 | +import org.scalatest.{Inside, OptionValues} |
10 | 15 |
|
| 16 | +import java.time.DayOfWeek |
| 17 | +import java.time.DayOfWeek.{MONDAY, SATURDAY, THURSDAY} |
| 18 | +import java.util.Locale |
| 19 | +import java.util.Locale.{FRANCE, GERMANY, UK} |
11 | 20 | import scala.collection.mutable |
12 | 21 | import scala.concurrent.ExecutionContext.Implicits.global |
13 | | -import scala.concurrent.duration._ |
14 | 22 |
|
15 | | -class LoadingTest extends AnyFlatSpec with Matchers with ScalaFutures with OptionValues with Eventually { |
16 | | - "onUpdate" should "give callbacks that allow logging updates" in { |
17 | | - val updates: mutable.Buffer[Update[String, Int]] = mutable.Buffer.empty |
| 23 | +object LoadingTest { |
| 24 | + /** |
| 25 | + * Uses a mock (optionally mutable) Map 'dataStore'. Provides an instance of [[Loading]] that fetches from that |
| 26 | + * datastore, and parses using the provided parser (counting how many times that parsing occurs). |
| 27 | + */ |
| 28 | + class TestApparatus[K, Response, V](dataStore: scala.collection.Map[K, Response])(parser: Response => V) { |
| 29 | + private val countingParser = new CountingParser[Response, V](parser) |
| 30 | + val loading: Loading[K, V] = TestFetching.withStubDataStore(dataStore).thenParsing(countingParser) |
18 | 31 |
|
19 | | - val fetching: Fetching[String, Int] = TestFetching.withIncrementingValues |
| 32 | + def parseCount(): Long = countingParser.count() |
| 33 | + |
| 34 | + def parsesCountedDuringConditionalLoadOf(k: K, oldV: ETaggedData[V]): Long = { |
| 35 | + val before = parseCount() |
| 36 | + loading.fetchThenParseIfNecessary(k, oldV).futureValue.toOption.value shouldBe parser(dataStore(k)) |
| 37 | + parseCount() - before |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +class LoadingTest extends AnyFlatSpec with Matchers with ScalaFutures with OptionValues with Eventually with Inside { |
| 43 | + |
| 44 | + "Creating a Loading instance from a Fetching instance" should "be done with 'thenParsing'" in { |
| 45 | + val fetching: Fetching[Locale, String] = |
| 46 | + TestFetching.withStubDataStore(Map(FRANCE -> "THURSDAY", GERMANY -> "MONDAY")) |
| 47 | + |
| 48 | + val loading: Loading[Locale, DayOfWeek] = fetching.thenParsing(DayOfWeek.valueOf) |
| 49 | + |
| 50 | + loading.fetchAndParse(FRANCE).futureValue.toOption.value shouldBe THURSDAY |
| 51 | + loading.fetchAndParse(GERMANY).futureValue.toOption.value shouldBe MONDAY |
| 52 | + } |
| 53 | + |
| 54 | + "fetchThenParseIfNecessary" should "*only* do parsing if fetching found a change in ETag value" in { |
| 55 | + val dataStore = mutable.Map(UK -> "SATURDAY") |
| 56 | + val testApparatus = new TestApparatus(dataStore)(parser = DayOfWeek.valueOf) |
| 57 | + |
| 58 | + inside(testApparatus.loading.fetchAndParse(UK).futureValue) { case initialLoad: ETaggedData[DayOfWeek] => |
| 59 | + testApparatus.parseCount() shouldBe 1 |
| 60 | + initialLoad.result shouldBe SATURDAY |
| 61 | + |
| 62 | + // No additional parse performed, as UK value's ETag unchanged |
| 63 | + testApparatus.parsesCountedDuringConditionalLoadOf(UK, initialLoad) shouldBe 0 |
| 64 | + |
| 65 | + dataStore(UK) = "MONDAY" |
| 66 | + |
| 67 | + // UK's ETag changed, we must parse the new value! |
| 68 | + testApparatus.parsesCountedDuringConditionalLoadOf(UK, initialLoad) shouldBe 1 |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + "onUpdate" should "provide callbacks that allow logging updates" in { |
| 73 | + val dataStore = mutable.Map(UK -> "SATURDAY") |
| 74 | + val testApparatus = new TestApparatus(dataStore)(parser = DayOfWeek.valueOf) |
| 75 | + |
| 76 | + val updates: mutable.Buffer[Update[Locale, DayOfWeek]] = mutable.Buffer.empty |
20 | 77 |
|
21 | 78 | val cache = new ETagCache( |
22 | | - fetching.thenParsing(identity).onUpdate { update => |
23 | | - updates.append(update) |
24 | | - }, |
25 | | - TolerateOldValueWhileRefreshing, |
26 | | - _.maximumSize(1).refreshAfterWrite(100.millis) |
| 79 | + testApparatus.loading.onUpdate(update => updates.append(update)), |
| 80 | + AlwaysWaitForRefreshedValue, |
| 81 | + _.maximumSize(1) |
27 | 82 | ) |
28 | 83 |
|
29 | 84 | val expectedUpdates = Seq( |
30 | | - Update("key", None, Some(0)), |
31 | | - Update("key", Some(0), Some(1)) |
| 85 | + Update(UK, None, Some(SATURDAY)), |
| 86 | + Update(UK, Some(SATURDAY), Some(MONDAY)) |
32 | 87 | ) |
33 | 88 |
|
34 | | - cache.get("key").futureValue shouldBe Some(0) |
35 | | - updates shouldBe expectedUpdates.take(1) |
36 | | - |
37 | | - Thread.sleep(105) |
| 89 | + cache.get(UK).futureValue shouldBe Some(SATURDAY) |
| 90 | + eventually(updates should contain theSameElementsInOrderAs expectedUpdates.take(1)) |
38 | 91 |
|
39 | | - eventually { cache.get("key").futureValue shouldBe Some(1) } |
40 | | - updates.toSeq shouldBe expectedUpdates |
| 92 | + dataStore(UK) = "MONDAY" |
41 | 93 |
|
42 | | - Thread.sleep(105) |
43 | | - updates.toSeq shouldBe expectedUpdates // No updates if we're not requesting the key from the cache |
| 94 | + cache.get(UK).futureValue shouldBe Some(MONDAY) |
| 95 | + eventually(updates should contain theSameElementsInOrderAs expectedUpdates) |
44 | 96 | } |
45 | 97 | } |
0 commit comments