<commit_before>/*
 * Copyright (c) 2010. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.core.repository.eventsourcing;

import org.axonframework.core.AbstractEventSourcedAggregateRoot;
import org.axonframework.core.DomainEvent;
import org.axonframework.core.DomainEventStream;
import org.axonframework.core.StubDomainEvent;
import org.axonframework.core.eventhandler.EventBus;
import org.axonframework.core.repository.ConcurrencyException;
import org.axonframework.core.repository.LockingStrategy;
import org.axonframework.core.repository.eventsourcing.fs.FileSystemEventStore;
import org.junit.*;
import org.junit.rules.*;
import org.springframework.core.io.FileSystemResource;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**
 * @author <NAME>
 */
public class EventSourcingRepositoryIntegrationTest implements Thread.UncaughtExceptionHandler {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    private EventSourcingRepository<SimpleAggregateRoot> repository;
    private UUID aggregateIdentifier;
    private EventBus mockEventBus;
    private FileSystemEventStore eventStore;
    private List<Throwable> uncaughtExceptions = new Vector<Throwable>();
    private List<Thread> startedThreads = new ArrayList<Thread>();

    @Test(timeout = 60000)
    public void testPessimisticLocking() throws Throwable {
        initializeRepository(LockingStrategy.PESSIMISTIC);
        long lastSequenceNumber = executeConcurrentModifications(10);

        // with pessimistic locking, all modifications are guaranteed successful
        // note: sequence number 20 means there are 21 events. This includes the one from the setup
        assertEquals(20, lastSequenceNumber);
        assertEquals(10, getSuccessfulModifications());
    }

    @Test(timeout = 60000)
    public void testOptimisticLocking() throws Throwable {
        // unfortunately, we cannot use @Before on the setUp, because of the TemporaryFolder
        initializeRepository(LockingStrategy.OPTIMISTIC);
        long lastSequenceNumber = executeConcurrentModifications(10);
        assertTrue("Expected at least one successful modification. Got " + getSuccessfulModifications(),
                   getSuccessfulModifications() >= 1);
        int expectedEventCount = getSuccessfulModifications() * 2;
        assertEquals(expectedEventCount, lastSequenceNumber);
        System.out.println("Successful modifications: " + getSuccessfulModifications());

        verify(mockEventBus, atLeast(2)).publish(isA(DomainEvent.class));
    }

    private int getSuccessfulModifications() {
        return 10 - uncaughtExceptions.size();
    }

    private void initializeRepository(LockingStrategy strategy) {
        repository = new SimpleEventSourcingRepository(strategy);
        eventStore = new FileSystemEventStore(new XStreamEventSerializer());
        eventStore.setBaseDir(new FileSystemResource(folder.getRoot().getPath() + "/"));
        repository.setEventStore(eventStore);
        mockEventBus = mock(EventBus.class);<commit_msg>Changed the EventSourcingRepositoryIntegrationTest to easily configure the amount of concurrent modifiers to use. Was needed to enlarge the chance of potential concurrency problems.<commit_after>/*
 * Copyright (c) 2010. Axon Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.axonframework.core.repository.eventsourcing;

import org.axonframework.core.AbstractEventSourcedAggregateRoot;
import org.axonframework.core.DomainEvent;
import org.axonframework.core.DomainEventStream;
import org.axonframework.core.StubDomainEvent;
import org.axonframework.core.eventhandler.EventBus;
import org.axonframework.core.repository.ConcurrencyException;
import org.axonframework.core.repository.LockingStrategy;
import org.axonframework.core.repository.eventsourcing.fs.FileSystemEventStore;
import org.junit.*;
import org.junit.rules.*;
import org.springframework.core.io.FileSystemResource;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.Vector;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

/**
 * @author <NAME>
 */
public class EventSourcingRepositoryIntegrationTest implements Thread.UncaughtExceptionHandler {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    private EventSourcingRepository<SimpleAggregateRoot> repository;
    private UUID aggregateIdentifier;
    private EventBus mockEventBus;
    private FileSystemEventStore eventStore;
    private List<Throwable> uncaughtExceptions = new Vector<Throwable>();
    private List<Thread> startedThreads = new ArrayList<Thread>();
    private static final int CONCURRENT_MODIFIERS = 10;

    @Test(timeout = 60000)
    public void testPessimisticLocking() throws Throwable {
        initializeRepository(LockingStrategy.PESSIMISTIC);
        long lastSequenceNumber = executeConcurrentModifications(CONCURRENT_MODIFIERS);

        // with pessimistic locking, all modifications are guaranteed successful
        // note: sequence number 20 means there are 21 events. This includes the one from the setup
        assertEquals(2 * CONCURRENT_MODIFIERS, lastSequenceNumber);
        assertEquals(CONCURRENT_MODIFIERS, getSuccessfulModifications());
    }

    @Test(timeout = 60000)
    public void testOptimisticLocking() throws Throwable {
        // unfortunately, we cannot use @Before on the setUp, because of the TemporaryFolder
        initializeRepository(LockingStrategy.OPTIMISTIC);
        long lastSequenceNumber = executeConcurrentModifications(CONCURRENT_MODIFIERS);
        assertTrue("Expected at least one successful modification. Got " + getSuccessfulModifications(),
                   getSuccessfulModifications() >= 1);
        int expectedEventCount = getSuccessfulModifications() * 2;
        assertEquals(expectedEventCount, lastSequenceNumber);
        System.out.println("Successful modifications: " + getSuccessfulModifications());

        verify(mockEventBus, atLeast(2)).publish(isA(DomainEvent.class));
    }

    private int getSuccessfulModifications() {
        return CONCURRENT_MODIFIERS - uncaughtExceptions.size();
    }

    private void initializeRepository(LockingStrategy strategy) {
        repository = new SimpleEventSourcingRepository(strategy);
        eventStore = new FileSystemEventStore(new XStreamEventSerializer());
        eventStore.setBaseDir(new FileSystemResource(folder.getRoot().getPath() + "/"));
        repository.setEventStore(eventStore);
        mockEventBus = mock(EventBus.class);
{'fraction_non_alphanumeric': 0.081758629377677, 'fraction_numerical': 0.007936507936507936, 'mean_word_length': 4.52086230876217, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5112775', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1977, 'n_words': 566}