mirror of
https://github.com/MinimalBible/MinimalBible
synced 2024-11-22 07:58:20 -05:00
More thorough (and fixed) MBIndexManager tests
This commit is contained in:
parent
6d534cfa9b
commit
184345bc06
@ -10,31 +10,41 @@ import java.util.concurrent.atomic.AtomicReference
|
|||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
import com.jayway.awaitility.Awaitility
|
import com.jayway.awaitility.Awaitility
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
import kotlin.test.assertFalse
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by bspeice on 2/16/15.
|
* Created by bspeice on 2/16/15.
|
||||||
*/
|
*/
|
||||||
|
data class Mocks() {
|
||||||
|
val mockBook = Mockito.mock(javaClass<Book>())
|
||||||
|
val mockIndex = Mockito.mock(javaClass<IndexManager>())
|
||||||
|
val indexManager = MBIndexManager(mockIndex)
|
||||||
|
}
|
||||||
|
|
||||||
class MBIndexManagerSpek() : Spek() {{
|
class MBIndexManagerSpek() : Spek() {{
|
||||||
|
|
||||||
given("a mock IndexManager, Book, and real MBIndexManager") {
|
given("a mock IndexManager, Book, and real MBIndexManager") {
|
||||||
val returnDelay: Long = 1000
|
val mocks = Mocks()
|
||||||
val mockIndex = Mockito.mock(javaClass<IndexManager>())
|
val mockBook = mocks.mockBook
|
||||||
val mockBook = Mockito.mock(javaClass<Book>())
|
val indexManager = mocks.indexManager
|
||||||
val indexManager = MBIndexManager(mockIndex)
|
|
||||||
|
|
||||||
val firstStatus = IndexStatus.UNDONE
|
val firstStatus = IndexStatus.UNDONE
|
||||||
val secondStatus = IndexStatus.DONE
|
val secondStatus = IndexStatus.DONE
|
||||||
|
|
||||||
// We sleep the first response to give us time to actually subscribe
|
on("setting up the book and attempting to create the index") {
|
||||||
Mockito.`when`(mockBook.getIndexStatus())
|
|
||||||
.thenAnswer { Thread.sleep(returnDelay); firstStatus }
|
|
||||||
.thenReturn(secondStatus)
|
|
||||||
|
|
||||||
on("attempting to create the index") {
|
|
||||||
val firstNext = AtomicReference<IndexStatus>()
|
val firstNext = AtomicReference<IndexStatus>()
|
||||||
val secondNext = AtomicReference<IndexStatus>()
|
val secondNext = AtomicReference<IndexStatus>()
|
||||||
val completedReference = AtomicBoolean(false)
|
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)
|
val subject = indexManager.buildIndex(mockBook)
|
||||||
|
|
||||||
subject.subscribe({
|
subject.subscribe({
|
||||||
@ -46,10 +56,8 @@ class MBIndexManagerSpek() : Spek() {{
|
|||||||
{},
|
{},
|
||||||
{ completedReference.set(true) })
|
{ completedReference.set(true) })
|
||||||
|
|
||||||
it("should fire an onComplete so we can continue further validation") {
|
// Wait until completed
|
||||||
Awaitility.waitAtMost(returnDelay * 2, TimeUnit.MILLISECONDS)
|
Awaitility.waitAtMost(1, TimeUnit.SECONDS).untilTrue(completedReference);
|
||||||
.untilTrue(completedReference)
|
|
||||||
}
|
|
||||||
|
|
||||||
it("should fire the correct first status") {
|
it("should fire the correct first status") {
|
||||||
assertEquals(firstStatus, firstNext.get())
|
assertEquals(firstStatus, firstNext.get())
|
||||||
@ -58,13 +66,18 @@ class MBIndexManagerSpek() : Spek() {{
|
|||||||
it("should fire the correct second status") {
|
it("should fire the correct second status") {
|
||||||
assertEquals(secondStatus, secondNext.get())
|
assertEquals(secondStatus, secondNext.get())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
it("should fire the onCompleted event") {
|
||||||
|
assertTrue(completedReference.get())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
given("a mock IndexManager, Book, and real MBIndexManager") {
|
given("a mock IndexManager, Book, and real MBIndexManager") {
|
||||||
val indexManager = Mockito.mock(javaClass<IndexManager>())
|
val mocks = Mocks()
|
||||||
val book = Mockito.mock(javaClass<Book>())
|
val indexManager = mocks.mockIndex
|
||||||
val mbIndex = MBIndexManager(indexManager)
|
val book = mocks.mockBook
|
||||||
|
val mbIndex = mocks.indexManager
|
||||||
|
|
||||||
on("trying to remove a book's index") {
|
on("trying to remove a book's index") {
|
||||||
mbIndex.removeIndex(book)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,9 @@ import rx.subjects.ReplaySubject
|
|||||||
*/
|
*/
|
||||||
class MBIndexManager(val indexManager: IndexManager) {
|
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
|
fun indexReady(b: Book) = b.getIndexStatus() == IndexStatus.DONE
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -24,7 +26,7 @@ class MBIndexManager(val indexManager: IndexManager) {
|
|||||||
*/
|
*/
|
||||||
fun buildIndex(b: Book): ReplaySubject<IndexStatus> {
|
fun buildIndex(b: Book): ReplaySubject<IndexStatus> {
|
||||||
if (!shouldIndex(b)) {
|
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.")
|
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())
|
.observeOn(Schedulers.computation())
|
||||||
.subscribe({
|
.subscribe({
|
||||||
indexStatus.onNext(b.getIndexStatus())
|
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
|
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.onNext(b.getIndexStatus())
|
||||||
indexStatus.onCompleted()
|
indexStatus.onCompleted()
|
||||||
|
Loading…
Reference in New Issue
Block a user