diff --git a/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManagerSpek.kt b/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManagerSpek.kt index 1413be8..a965394 100644 --- a/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManagerSpek.kt +++ b/app-test/src/test/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManagerSpek.kt @@ -10,31 +10,41 @@ import java.util.concurrent.atomic.AtomicReference import kotlin.test.assertEquals import com.jayway.awaitility.Awaitility import java.util.concurrent.TimeUnit +import kotlin.test.assertTrue +import kotlin.test.assertFalse /** * Created by bspeice on 2/16/15. */ +data class Mocks() { + val mockBook = Mockito.mock(javaClass()) + val mockIndex = Mockito.mock(javaClass()) + val indexManager = MBIndexManager(mockIndex) +} + class MBIndexManagerSpek() : Spek() {{ given("a mock IndexManager, Book, and real MBIndexManager") { - val returnDelay: Long = 1000 - val mockIndex = Mockito.mock(javaClass()) - val mockBook = Mockito.mock(javaClass()) - val indexManager = MBIndexManager(mockIndex) + val mocks = Mocks() + val mockBook = mocks.mockBook + val indexManager = mocks.indexManager val firstStatus = IndexStatus.UNDONE val secondStatus = IndexStatus.DONE - // We sleep the first response to give us time to actually subscribe - Mockito.`when`(mockBook.getIndexStatus()) - .thenAnswer { Thread.sleep(returnDelay); firstStatus } - .thenReturn(secondStatus) - - on("attempting to create the index") { + on("setting up the book and attempting to create the index") { val firstNext = AtomicReference() val secondNext = AtomicReference() val completedReference = AtomicBoolean(false) + Mockito.`when`(mockBook.getIndexStatus()) + // MBIndexManager checks status + .thenReturn(firstStatus) + // First actual status + .thenReturn(firstStatus) + // Second actual status + .thenReturn(secondStatus) + val subject = indexManager.buildIndex(mockBook) subject.subscribe({ @@ -46,10 +56,8 @@ class MBIndexManagerSpek() : Spek() {{ {}, { completedReference.set(true) }) - it("should fire an onComplete so we can continue further validation") { - Awaitility.waitAtMost(returnDelay * 2, TimeUnit.MILLISECONDS) - .untilTrue(completedReference) - } + // Wait until completed + Awaitility.waitAtMost(1, TimeUnit.SECONDS).untilTrue(completedReference); it("should fire the correct first status") { assertEquals(firstStatus, firstNext.get()) @@ -58,13 +66,18 @@ class MBIndexManagerSpek() : Spek() {{ it("should fire the correct second status") { assertEquals(secondStatus, secondNext.get()) } + + it("should fire the onCompleted event") { + assertTrue(completedReference.get()) + } } } given("a mock IndexManager, Book, and real MBIndexManager") { - val indexManager = Mockito.mock(javaClass()) - val book = Mockito.mock(javaClass()) - val mbIndex = MBIndexManager(indexManager) + val mocks = Mocks() + val indexManager = mocks.mockIndex + val book = mocks.mockBook + val mbIndex = mocks.indexManager on("trying to remove a book's index") { mbIndex.removeIndex(book) @@ -74,5 +87,68 @@ class MBIndexManagerSpek() : Spek() {{ } } } + + given("a Book that is indexed and real MBIndexManager") { + val mocks = Mocks() + val book = mocks.mockBook + val indexManager = mocks.indexManager + + Mockito.`when`(book.getIndexStatus()) + .thenReturn(IndexStatus.DONE) + + on("trying to determine whether we should index") { + it("should not try to index") { + assertFalse(indexManager shouldIndex book) + } + } + + on("trying to determine if an index is ready") { + it("should let us know that everything is ready") { + assertTrue(indexManager indexReady book) + } + } + } + + given("a Book with an indexing error") { + val mocks = Mocks() + val book = mocks.mockBook + val indexManager = mocks.indexManager + + Mockito.`when`(book.getIndexStatus()) + .thenReturn(IndexStatus.INVALID) + + on("trying to determine whether we should index") { + it("should try to index again and over-write the original") { + assertTrue(indexManager shouldIndex book) + } + } + + on("trying to determine if an index is ready") { + it("should inform us that the index is most certainly not ready") { + assertFalse(indexManager indexReady book) + } + } + } + + given("a Book in progress of being indexed") { + val mocks = Mocks() + val book = mocks.mockBook + val indexManager = mocks.indexManager + + Mockito.`when`(book.getIndexStatus()) + .thenReturn(IndexStatus.CREATING) + + on("trying to determine whether we should index") { + it("should not create a second indexing thread") { + assertFalse(indexManager shouldIndex book) + } + } + + on("trying to determine if the index is ready") { + it("should let us know that the index is still in progress") { + assertFalse(indexManager shouldIndex book) + } + } + } } } diff --git a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt index 4008025..8c7ae66 100644 --- a/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt +++ b/app/src/main/kotlin/org/bspeice/minimalbible/activity/search/MBIndexManager.kt @@ -13,7 +13,9 @@ import rx.subjects.ReplaySubject */ class MBIndexManager(val indexManager: IndexManager) { - fun shouldIndex(b: Book) = b.getIndexStatus() == IndexStatus.UNDONE + fun shouldIndex(b: Book) = b.getIndexStatus() == IndexStatus.UNDONE || + b.getIndexStatus() == IndexStatus.INVALID + fun indexReady(b: Book) = b.getIndexStatus() == IndexStatus.DONE /** @@ -24,7 +26,7 @@ class MBIndexManager(val indexManager: IndexManager) { */ fun buildIndex(b: Book): ReplaySubject { if (!shouldIndex(b)) { - Log.e("MBIndexManager", "Current status is ${b.getIndexStatus()}, not creating index") + // Log.e("MBIndexManager", "Current status is ${b.getIndexStatus()}, not creating index") throw IllegalStateException("Don't try and index a book that should not get it.") } @@ -33,10 +35,10 @@ class MBIndexManager(val indexManager: IndexManager) { .observeOn(Schedulers.computation()) .subscribe({ indexStatus.onNext(b.getIndexStatus()) - Log.e("MBIndexManager", "Building index for ${b.getInitials()}, ${b.getIndexStatus()}") + // Log.e("MBIndexManager", "Building index for ${b.getInitials()}, ${b.getIndexStatus()}") indexManager scheduleIndexCreation b - Log.e("MBIndexManager", "Done building index for ${b.getInitials()}, ${b.getIndexStatus()}") + // Log.e("MBIndexManager", "Done building index for ${b.getInitials()}, ${b.getIndexStatus()}") indexStatus.onNext(b.getIndexStatus()) indexStatus.onCompleted()