fix(tasteprofile): create the taste profile row race-free #211
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/taste-profile-race"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Cause
TasteProfileServicecreated a user's taste profile as find-then-insert (findByUserId(...) ?: TasteProfile(userId)followed bysave). When two transactions work on the same profile before it exists, both see no row and both insert. The second insert hits the unique index ontaste_profiles(user_id), andGlobalExceptionHandlermaps theDataIntegrityViolationExceptionto 409.In
Phase3IntegrationTest.test17_getTasteProfilethe two writers are:TasteProfileInvalidatedEventlistener (onTasteProfileInvalidated->recalculate), triggered by the three rating POSTs.GET /api/taste-profile, which callsrecalculateitself when no row exists yet.These overlap even under the
testprofile.TestAsyncConfigis meant to make@Asyncsynchronous, but as far as I can tell from the code,@Asyncdoes not end up on itsSyncTaskExecutor. Spring resolves the default async executor withgetBean(TaskExecutor.class), and the@PrimarytaskSchedulerbean (aThreadPoolTaskScheduler, which is also aTaskExecutor) wins that lookup. The listener therefore runs on thetest-scheduling-thread while the test already sends its GET. The existing test comments (@Async