objectbox / objectbox/objectbox-java

Make it easier to reliably test data observers

Open
#616 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
4.6k
Forks
311
PR merge metrics
No merged PRs in 30d

Description

In my experience, testing data observers is somewhat brittle. Even with explicit transactions I sometimes get "delayed" notifications that have been triggered by the previous operation.

Let's take this test (ObjectBox 2.2.0 + RxJava 2):

boxStore.runInTx {
    boxStore.boxFor(SomeEntity::class.java).put(SomeEntity())
}

val testObserver = TestObserver.create<Optional<SomeEntity>>()

boxStore.runInTx {
    repository.find(123).subscribeWith(testObserver)
}

testObserver.awaitCount(1)
testObserver.assertValueCount(1)

Sometimes, I get a value count of 2 from the original put operation. For completeness, the code of the find() method:

fun find(id: Long): Observable<Optional<SomeEntity>> {
    return Observable.create { emitter ->
        val query = this.boxStore.boxFor(SomeEntity::class)
            .query()
            .equal(SomeEntity_.id, id)
            .build()

        query.subscribe().observer { result ->
            emitter.onNext(result.firstOrNull().toOptional())
        }
    }
}

Assuming I did not misunderstand the docs or misuse the ObjectBox API, it would be great if ObjectBox could offer some method e.g. to block until all notifications triggered by the last transaction have been issued.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the runInTx, TestObserver, and query.subscribe().observer sequence shown in the issue, then review the ObjectBox data-observer and transaction behavior it exercises. Done means a reliable way to wait for notifications from the preceding transaction, demonstrated by a test that consistently receives the expected single value.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin
Domain
databases, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.