Reputation: 1415
I have a Spring boot application. As part of it, I should keep track of a simple object ActiveVersion that has two fields, type and version. I persist this object in both redis and mongo. ActiveVersionCacheRepository is a reactive redis repository (using reactiveRedisTemplate) and ActiveVersionRepository is a reactive mongo repository. This is part of ActiveVersionService. Methods to retrieve and persist the activeVersion:
fun getActiveVersion(type: String): Mono<ActiveVersion> {
return activeVersionCacheRepository.findByKey(type)
.switchIfEmpty(
Mono.defer{activeVersionRepository.findByType(type)}
).switchIfEmpty(
Mono.defer{persist(ActiveVersion(type,1))}
)
}
fun persist(activeVersion: ActiveVersion): Mono<ActiveVersion> {
activeVersionCacheRepository.save(activeVersion.type, activeVersion)
return activeVersionRepository.save(activeVersion)
}
This is my test method:
@Test
fun getNewActiveVersion(){
var activeVersion = activeVersionService.getActiveVersion("newDummy").block()
assertEquals(activeVersion?.version,1)
}
My problem is when I debug the test method and go step by step in ide and evaluate persist lines with block method such as: activeVersionCacheRepository.save(activeVersion.type, activeVersion).block()
the active version indeed gets persisted in redis. But when I just go through the code without evaluating with block (or simply by running the test not debugging) nothing gets saved in redis. I am new to reactive programming so maybe I am missing something in switchIfEmpty or somewhere else.
Upvotes: 0
Views: 620
Reputation: 1415
It was me getting the paradigm wrong. The problem was with my persist method. I changed it to this and it worked:
fun persist(activeVersion: ActiveVersion): Mono<ActiveVersion> {
return activeVersionCacheRepository.save(activeVersion.type, activeVersion).then(
activeVersionRepository.save(activeVersion)
)
}
Upvotes: 1