forked from Onlineberatung/onlineBeratung-messageService
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: concurrency issue when save and delete a draft message in parallel
- Loading branch information
Showing
6 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/de/caritas/cob/messageservice/api/service/DraftMessageServiceIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package de.caritas.cob.messageservice.api.service; | ||
|
||
import static com.anarsoft.vmlens.concurrent.junit.TestUtil.runMultithreaded; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import de.caritas.cob.messageservice.MessageServiceApplication; | ||
import de.caritas.cob.messageservice.api.exception.CustomCryptoException; | ||
import de.caritas.cob.messageservice.api.helper.AuthenticatedUser; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = MessageServiceApplication.class) | ||
@AutoConfigureTestDatabase(replace = Replace.ANY) | ||
@TestPropertySource(properties = "spring.profiles.active=testing") | ||
public class DraftMessageServiceIT { | ||
|
||
@Autowired | ||
private DraftMessageService draftMessageService; | ||
|
||
@MockBean | ||
private AuthenticatedUser authenticatedUser; | ||
|
||
@MockBean | ||
private EncryptionService encryptionService; | ||
|
||
@Before | ||
public void setup() throws CustomCryptoException { | ||
when(this.encryptionService.encrypt(any(), any())).thenReturn("encrypted"); | ||
when(this.authenticatedUser.getUserId()).thenReturn("userId"); | ||
} | ||
|
||
@Test | ||
public void saveAndDeleteDraftMessage_Should_produceNoError_When_executionIsInParallel() | ||
throws InterruptedException { | ||
AtomicInteger errorCount = new AtomicInteger(0); | ||
int threadCount = 10; | ||
String rcGroupId = "rcGroupId"; | ||
|
||
runMultithreaded(() -> { | ||
try { | ||
draftMessageService.saveDraftMessage("message", rcGroupId); | ||
draftMessageService.deleteDraftMessageIfExist(rcGroupId); | ||
} catch (Exception e) { | ||
errorCount.incrementAndGet(); | ||
} | ||
}, threadCount); | ||
|
||
assertThat(errorCount.get(), is(0)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE TABLE DRAFTMESSAGE | ||
( | ||
ID bigint NOT NULL, | ||
USER_ID varchar(36) NOT NULL, | ||
RC_GROUP_ID varchar(255) NOT NULL, | ||
DRAFT_MESSAGE varchar(255) NOT NULL, | ||
CREATE_DATE timestamp, | ||
UPDATE_DATE timestamp, | ||
PRIMARY KEY (ID) | ||
); | ||
CREATE SEQUENCE SEQUENCE_DRAFTMESSAGE | ||
START WITH 100000 | ||
INCREMENT BY 1; |