Search in sources :

Example 11 with RaidEntity

use of pokeraidbot.infrastructure.jpa.raid.RaidEntity in project pokeraidbot by magnusmickelsson.

the class RaidEntityRepositoryTest method createAndGet.

@Test
public void createAndGet() throws Exception {
    final String id = "id1";
    final RaidEntity raidEntity = entityRepository.save(new RaidEntity(id, "Mupp", LocalDateTime.now().plusMinutes(20), "Thegym", "Theuser", "Theregion"));
    assertNotNull(raidEntity);
    assertThat(entityRepository.findOne(id), is(raidEntity));
}
Also used : RaidEntity(pokeraidbot.infrastructure.jpa.raid.RaidEntity) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 12 with RaidEntity

use of pokeraidbot.infrastructure.jpa.raid.RaidEntity in project pokeraidbot by magnusmickelsson.

the class RaidEntityRepositoryTest method createSignUp.

@Transactional(propagation = Propagation.REQUIRES_NEW)
public RaidEntitySignUp createSignUp(String id, Integer thread, LocalTime now, Random random, int i) {
    RaidEntitySignUp signUp = new RaidEntitySignUp("Mupp" + thread + "_" + i, random.nextInt(4) + 1, printTime(now));
    RaidEntity entity = entityRepository.findOne(id);
    entity.addSignUp(signUp);
    entityRepository.save(entity);
    LOGGER.warn("Created: " + signUp);
    return signUp;
}
Also used : RaidEntitySignUp(pokeraidbot.infrastructure.jpa.raid.RaidEntitySignUp) RaidEntity(pokeraidbot.infrastructure.jpa.raid.RaidEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 13 with RaidEntity

use of pokeraidbot.infrastructure.jpa.raid.RaidEntity in project pokeraidbot by magnusmickelsson.

the class RaidEntityRepositoryTest method deleteSomeSignUps.

@Transactional(propagation = Propagation.REQUIRES_NEW)
public int deleteSomeSignUps(String id) {
    int numberDeleted = 0;
    final RaidEntity theEntity = entityRepository.findOne(id);
    for (RaidEntitySignUp signUp : theEntity.getSignUpsAsSet()) {
        if (signUp.getNumberOfPeople().equals(3)) {
            theEntity.removeSignUp(signUp);
            LOGGER.warn("Removed: " + signUp);
            numberDeleted++;
        }
    }
    entityRepository.save(theEntity);
    return numberDeleted;
}
Also used : RaidEntitySignUp(pokeraidbot.infrastructure.jpa.raid.RaidEntitySignUp) RaidEntity(pokeraidbot.infrastructure.jpa.raid.RaidEntity) Transactional(org.springframework.transaction.annotation.Transactional)

Example 14 with RaidEntity

use of pokeraidbot.infrastructure.jpa.raid.RaidEntity in project pokeraidbot by magnusmickelsson.

the class RaidEntityRepositoryTest method createWithGroup.

@Test
public void createWithGroup() throws Exception {
    final String id = "id1";
    RaidEntity raidEntity = new RaidEntity(id, "Mupp", LocalDateTime.now().plusMinutes(20), "Thegym", "Theuser", "Theregion");
    final RaidGroup group = new RaidGroup("testserver", "channel", "abc1", "abc2", "testId", LocalDateTime.now().plusMinutes(10));
    assertThat(raidEntity.addGroup(group), is(true));
    raidEntity = entityRepository.save(raidEntity);
    final RaidEntity loaded = entityRepository.findOne(id);
    assertThat(loaded, is(raidEntity));
    final Set<RaidGroup> groups = loaded.getGroupsAsSet();
    assertThat(groups.size(), is(1));
    assertThat(groups.iterator().next(), is(group));
    final List<RaidGroup> groupsForServer = entityRepository.findGroupsForServer("testserver");
    assertThat(groupsForServer.size(), is(1));
    assertThat(groupsForServer.iterator().next(), is(group));
}
Also used : RaidGroup(pokeraidbot.infrastructure.jpa.raid.RaidGroup) RaidEntity(pokeraidbot.infrastructure.jpa.raid.RaidEntity) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 15 with RaidEntity

use of pokeraidbot.infrastructure.jpa.raid.RaidEntity in project pokeraidbot by magnusmickelsson.

the class RaidEntityRepositoryTest method createWithSignupsTryToCleanUpWithoutConcurrentModificationException.

@Test
public void createWithSignupsTryToCleanUpWithoutConcurrentModificationException() throws Exception {
    final String id = "id1";
    final RaidEntity raidEntity = entityRepository.save(new RaidEntity(id, "Mupp", LocalDateTime.now().plusMinutes(20), "Thegym", "Theuser", "Theregion"));
    final LocalTime now = LocalTime.now();
    // Create signups with 100 ms interval, during ~3 seconds
    Callable<Integer> creatingSignUpsTask = () -> {
        int numberOfSignUpsCreated = 0;
        final Random random = new Random();
        for (int i = 0; i < 30; i++) {
            Thread.sleep(100);
            createSignUp(id, 1, now, random, i);
            numberOfSignUpsCreated++;
        }
        return numberOfSignUpsCreated;
    };
    Callable<Integer> creatingSignUpsTask2 = () -> {
        int numberOfSignUpsCreated = 0;
        final Random random = new Random();
        for (int i = 0; i < 30; i++) {
            Thread.sleep(100);
            createSignUp(id, 2, now, random, i);
            numberOfSignUpsCreated++;
        }
        return numberOfSignUpsCreated;
    };
    // After 2 seconds, try and remove all signups for a certain time
    Callable<Integer> cleanUpSignUpsTask = () -> {
        Thread.sleep(2000);
        int numberDeleted = deleteSomeSignUps(id);
        return numberDeleted;
    };
    final List<Future<Integer>> futures = executorService.invokeAll(Arrays.asList(cleanUpSignUpsTask, creatingSignUpsTask, creatingSignUpsTask2));
    final RaidEntity theEntity = entityRepository.findOne(id);
    assertThat(theEntity == null, is(false));
    final Iterator<Future<Integer>> futureIterator = futures.iterator();
    final Future<Integer> cleanedUpFuture = futureIterator.next();
    final Future<Integer> signupFuture1 = futureIterator.next();
    final Future<Integer> signupFuture2 = futureIterator.next();
    final Integer signedUpInThread1 = signupFuture1.get();
    final Integer signedUpInThread2 = signupFuture2.get();
    final Integer cleanedUp = cleanedUpFuture.get();
    int signups = signedUpInThread1 + signedUpInThread2 - cleanedUp;
    assertThat(signedUpInThread1, is(30));
    assertThat(signedUpInThread2, is(30));
    assertThat(cleanedUp > 0, is(true));
    assertThat(theEntity.getSignUpsAsSet().size(), is(signups));
}
Also used : LocalTime(java.time.LocalTime) RaidEntity(pokeraidbot.infrastructure.jpa.raid.RaidEntity) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

RaidEntity (pokeraidbot.infrastructure.jpa.raid.RaidEntity)25 RaidEntitySignUp (pokeraidbot.infrastructure.jpa.raid.RaidEntitySignUp)9 RaidGroup (pokeraidbot.infrastructure.jpa.raid.RaidGroup)5 Test (org.junit.Test)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 Transactional (org.springframework.transaction.annotation.Transactional)4 UserMessedUpException (pokeraidbot.domain.errors.UserMessedUpException)4 LocalTime (java.time.LocalTime)3 LocalDateTime (java.time.LocalDateTime)1 ArrayList (java.util.ArrayList)1 Guild (net.dv8tion.jda.core.entities.Guild)1 User (net.dv8tion.jda.core.entities.User)1 RaidExistsException (pokeraidbot.domain.errors.RaidExistsException)1 RaidNotFoundException (pokeraidbot.domain.errors.RaidNotFoundException)1 Gym (pokeraidbot.domain.gym.Gym)1 SignUp (pokeraidbot.domain.raid.signup.SignUp)1 Config (pokeraidbot.infrastructure.jpa.config.Config)1