|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +//package org.apache.seata.server.storage; |
| 18 | +// |
| 19 | +//import java.util.ArrayList; |
| 20 | +//import java.util.ConcurrentModificationException; |
| 21 | +//import java.util.List; |
| 22 | +//import java.util.concurrent.CountDownLatch; |
| 23 | +//import java.util.concurrent.ExecutorService; |
| 24 | +//import java.util.concurrent.Executors; |
| 25 | +//import java.util.concurrent.atomic.AtomicBoolean; |
| 26 | +//import org.apache.seata.core.model.BranchStatus; |
| 27 | +//import org.apache.seata.core.model.BranchType; |
| 28 | +//import org.apache.seata.server.session.BranchSession; |
| 29 | +//import org.junit.jupiter.api.RepeatedTest; |
| 30 | +//import org.junit.jupiter.api.extension.ExtendWith; |
| 31 | +//import org.mockito.junit.jupiter.MockitoExtension; |
| 32 | +//import org.springframework.boot.test.context.SpringBootTest; |
| 33 | +// |
| 34 | +//import static org.junit.jupiter.api.Assertions.assertFalse; |
| 35 | +// |
| 36 | +//@ExtendWith(MockitoExtension.class) |
| 37 | +//@SpringBootTest |
| 38 | +//public class SessionConverterTest { |
| 39 | +// // Repeat 100 for adding success per |
| 40 | +// @RepeatedTest(100) |
| 41 | +// public void testConcurrentModificationException() throws InterruptedException { |
| 42 | +// List<BranchSession> branchSessions = new ArrayList<>(); |
| 43 | +// for (int i = 0; i < 1000; i++) { |
| 44 | +// branchSessions.add(createMockBranchSession(i)); |
| 45 | +// } |
| 46 | +// |
| 47 | +// CountDownLatch startLatch = new CountDownLatch(1); |
| 48 | +// CountDownLatch endLatch = new CountDownLatch(2); |
| 49 | +// AtomicBoolean exceptionThrown = new AtomicBoolean(false); |
| 50 | +// |
| 51 | +// ExecutorService executorService = Executors.newFixedThreadPool(2); |
| 52 | +// |
| 53 | +// // Thread for converting branch sessions |
| 54 | +// executorService.submit(() -> { |
| 55 | +// try { |
| 56 | +// startLatch.await(); |
| 57 | +// for (int i = 0; i < 100; i++) { |
| 58 | +// try { |
| 59 | +// SessionConverter.convertBranchSession(branchSessions); |
| 60 | +// } catch (ConcurrentModificationException e) { |
| 61 | +// exceptionThrown.set(true); |
| 62 | +// break; |
| 63 | +// } |
| 64 | +// } |
| 65 | +// } catch (InterruptedException e) { |
| 66 | +// Thread.currentThread().interrupt(); |
| 67 | +// } finally { |
| 68 | +// endLatch.countDown(); |
| 69 | +// } |
| 70 | +// }); |
| 71 | +// |
| 72 | +// // Thread for modifying the list |
| 73 | +// executorService.submit(() -> { |
| 74 | +// try { |
| 75 | +// startLatch.await(); |
| 76 | +// for (int i = 0; i < 1000; i++) { |
| 77 | +// branchSessions.add(createMockBranchSession(1000 + i)); |
| 78 | +// if (i % 10 == 0) { |
| 79 | +// branchSessions.remove(0); |
| 80 | +// } |
| 81 | +// } |
| 82 | +// } catch (InterruptedException e) { |
| 83 | +// Thread.currentThread().interrupt(); |
| 84 | +// } finally { |
| 85 | +// endLatch.countDown(); |
| 86 | +// } |
| 87 | +// }); |
| 88 | +// // Start both threads |
| 89 | +// startLatch.countDown(); |
| 90 | +// // Wait for both threads to finish |
| 91 | +// endLatch.await(); |
| 92 | +// |
| 93 | +// executorService.shutdown(); |
| 94 | +// |
| 95 | +// assertFalse(exceptionThrown.get(), "ConcurrentModificationException was not thrown"); |
| 96 | +// } |
| 97 | +// |
| 98 | +// private BranchSession createMockBranchSession(int id) { |
| 99 | +// BranchSession session = new BranchSession(); |
| 100 | +// session.setXid("xid" + id); |
| 101 | +// session.setTransactionId(id); |
| 102 | +// session.setBranchId(id); |
| 103 | +// session.setResourceGroupId("resourceGroup" + id); |
| 104 | +// session.setResourceId("resource" + id); |
| 105 | +// session.setBranchType(BranchType.AT); |
| 106 | +// session.setStatus(BranchStatus.Registered); |
| 107 | +// session.setClientId("client" + id); |
| 108 | +// session.setApplicationData("data" + id); |
| 109 | +// return session; |
| 110 | +// } |
| 111 | +//} |
0 commit comments