Search in sources :

Example 1 with WaitingQueueSubscription

use of alfio.model.WaitingQueueSubscription in project alf.io by alfio-event.

the class WaitingQueueProcessorMultiThreadedIntegrationTest method testPreRegistration.

@Test
public void testPreRegistration() throws InterruptedException {
    IntegrationTestUtil.ensureMinimalConfiguration(configurationRepository);
    configurationManager.saveSystemConfiguration(ConfigurationKeys.ENABLE_PRE_REGISTRATION, "true");
    configurationManager.saveSystemConfiguration(ConfigurationKeys.ENABLE_WAITING_QUEUE, "true");
    initAdminUser(userRepository, authorityRepository);
    Event event = null;
    try {
        List<TicketCategoryModification> categories = Collections.singletonList(new TicketCategoryModification(null, "default", TicketCategory.TicketAccessType.INHERIT, 10, new DateTimeModification(LocalDate.now(ClockProvider.clock()).plusDays(1), LocalTime.now(ClockProvider.clock())), new DateTimeModification(LocalDate.now(ClockProvider.clock()).plusDays(2), LocalTime.now(ClockProvider.clock())), DESCRIPTION, BigDecimal.TEN, false, "", false, null, null, null, null, null, 0, null, null, AlfioMetadata.empty()));
        Pair<Event, String> pair = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);
        event = pair.getKey();
        waitingQueueManager.subscribe(event, new CustomerName("Giuseppe Garibaldi", "Giuseppe", "Garibaldi", event.mustUseFirstAndLastName()), "peppino@garibaldi.com", null, Locale.ENGLISH);
        waitingQueueManager.subscribe(event, new CustomerName("Nino Bixio", "Nino", "Bixio", event.mustUseFirstAndLastName()), "bixio@mille.org", null, Locale.ITALIAN);
        assertEquals(2, (int) waitingQueueRepository.countWaitingPeople(event.getId()));
        final int parallelism = 10;
        List<Callable<Void>> calls = new ArrayList<>(parallelism);
        ExecutorService executor = Executors.newFixedThreadPool(parallelism);
        final Event eventF = event;
        for (int i = 0; i < parallelism; i++) {
            calls.add(() -> {
                waitingQueueSubscriptionProcessor.distributeAvailableSeats(eventF);
                return null;
            });
        }
        executor.invokeAll(calls);
        executor.shutdown();
        while (!executor.awaitTermination(10, TimeUnit.MILLISECONDS)) {
        }
        assertEquals(18, ticketRepository.findFreeByEventId(event.getId()).size());
        TicketCategoryModification tcm = new TicketCategoryModification(null, "default", TicketCategory.TicketAccessType.INHERIT, 10, new DateTimeModification(LocalDate.now(ClockProvider.clock()).minusDays(1), LocalTime.now(ClockProvider.clock())), new DateTimeModification(LocalDate.now(ClockProvider.clock()).plusDays(5), LocalTime.now(ClockProvider.clock())), DESCRIPTION, BigDecimal.TEN, false, "", true, null, null, null, null, null, 0, null, null, AlfioMetadata.empty());
        eventManager.insertCategory(event.getId(), tcm, pair.getValue());
        // System.err.println("------------------------------------------------");
        executor = Executors.newFixedThreadPool(parallelism);
        calls.clear();
        for (int i = 0; i < parallelism; i++) {
            calls.add(() -> {
                waitingQueueSubscriptionProcessor.distributeAvailableSeats(eventF);
                return null;
            });
        }
        executor.invokeAll(calls);
        executor.shutdown();
        while (!executor.awaitTermination(10, TimeUnit.MILLISECONDS)) {
        }
        List<WaitingQueueSubscription> subscriptions = waitingQueueRepository.loadAll(event.getId());
        assertEquals(2, subscriptions.stream().filter(w -> StringUtils.isNotBlank(w.getReservationId())).count());
        assertTrue(subscriptions.stream().allMatch(w -> w.getStatus().equals(WaitingQueueSubscription.Status.PENDING)));
        assertTrue(subscriptions.stream().allMatch(w -> w.getSubscriptionType().equals(WaitingQueueSubscription.Type.PRE_SALES)));
    } finally {
        if (event != null) {
            eventManager.deleteEvent(event.getId(), UserManager.ADMIN_USERNAME);
        }
        configurationManager.deleteKey(ConfigurationKeys.ENABLE_PRE_REGISTRATION.name());
        configurationManager.deleteKey(ConfigurationKeys.ENABLE_WAITING_QUEUE.name());
        removeAdminUser(userRepository, authorityRepository);
    }
}
Also used : java.util(java.util) WaitingQueueRepository(alfio.repository.WaitingQueueRepository) AlfioMetadata(alfio.model.metadata.AlfioMetadata) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) Callable(java.util.concurrent.Callable) ConfigurationManager(alfio.manager.system.ConfigurationManager) StringUtils(org.apache.commons.lang3.StringUtils) DateTimeModification(alfio.model.modification.DateTimeModification) BigDecimal(java.math.BigDecimal) Pair(org.apache.commons.lang3.tuple.Pair) LocalTime(java.time.LocalTime) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ExecutorService(java.util.concurrent.ExecutorService) AuthorityRepository(alfio.repository.user.AuthorityRepository) OrganizationRepository(alfio.repository.user.OrganizationRepository) TicketCategoryModification(alfio.model.modification.TicketCategoryModification) TicketRepository(alfio.repository.TicketRepository) CustomerName(alfio.model.CustomerName) EventRepository(alfio.repository.EventRepository) IntegrationTestUtil(alfio.test.util.IntegrationTestUtil) Executors(java.util.concurrent.Executors) ConfigurationRepository(alfio.repository.system.ConfigurationRepository) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) TicketCategory(alfio.model.TicketCategory) WaitingQueueSubscription(alfio.model.WaitingQueueSubscription) Initializer(alfio.config.Initializer) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) UserRepository(alfio.repository.user.UserRepository) UserManager(alfio.manager.user.UserManager) ContextConfiguration(org.springframework.test.context.ContextConfiguration) LocalDate(java.time.LocalDate) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) DataSourceConfiguration(alfio.config.DataSourceConfiguration) Event(alfio.model.Event) ClockProvider(alfio.util.ClockProvider) TestConfiguration(alfio.TestConfiguration) ConfigurationKeys(alfio.model.system.ConfigurationKeys) Callable(java.util.concurrent.Callable) WaitingQueueSubscription(alfio.model.WaitingQueueSubscription) DateTimeModification(alfio.model.modification.DateTimeModification) TicketCategoryModification(alfio.model.modification.TicketCategoryModification) ExecutorService(java.util.concurrent.ExecutorService) Event(alfio.model.Event) CustomerName(alfio.model.CustomerName) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with WaitingQueueSubscription

use of alfio.model.WaitingQueueSubscription in project alf.io by alfio-event.

the class WaitingQueueSubscriptionProcessor method distributeAvailableSeats.

public void distributeAvailableSeats(Event event) {
    var messageSource = messageSourceManager.getMessageSourceFor(event);
    waitingQueueManager.distributeSeats(event).forEach(triple -> {
        WaitingQueueSubscription subscription = triple.getLeft();
        Locale locale = subscription.getLocale();
        ZonedDateTime expiration = triple.getRight();
        Organization organization = eventManager.loadOrganizerUsingSystemPrincipal(event);
        String reservationId = createReservation(event, triple.getMiddle(), expiration, locale);
        String subject = messageSource.getMessage("email-waiting-queue-acquired.subject", new Object[] { event.getDisplayName() }, locale);
        String reservationUrl = ticketReservationManager.reservationUrl(reservationId, event);
        Map<String, Object> model = TemplateResource.buildModelForWaitingQueueReservationEmail(organization, event, subscription, reservationUrl, expiration);
        notificationManager.sendSimpleEmail(event, reservationId, subscription.getEmailAddress(), subject, () -> templateManager.renderTemplate(event, TemplateResource.WAITING_QUEUE_RESERVATION_EMAIL, model, locale));
        waitingQueueRepository.flagAsPending(reservationId, subscription.getId());
    });
}
Also used : Organization(alfio.model.user.Organization) ZonedDateTime(java.time.ZonedDateTime) WaitingQueueSubscription(alfio.model.WaitingQueueSubscription)

Example 3 with WaitingQueueSubscription

use of alfio.model.WaitingQueueSubscription in project alf.io by alfio-event.

the class WaitingQueueManagerTest method processPreReservations.

@Test
void processPreReservations() {
    Ticket ticket = mock(Ticket.class);
    WaitingQueueSubscription subscription = mock(WaitingQueueSubscription.class);
    when(subscription.isPreSales()).thenReturn(true);
    when(eventRepository.countExistingTickets(eq(eventId))).thenReturn(10);
    when(event.getZoneId()).thenReturn(ZoneId.systemDefault());
    when(waitingQueueRepository.countWaitingPeople(eq(eventId))).thenReturn(1);
    when(ticketRepository.countWaiting(eq(eventId))).thenReturn(0);
    when(configurationManager.getFor(eq(ENABLE_PRE_REGISTRATION), any())).thenReturn(new ConfigurationManager.MaybeConfiguration(ENABLE_PRE_REGISTRATION, new ConfigurationKeyValuePathLevel(null, "true", null)));
    when(configurationManager.getFor(eq(WAITING_QUEUE_RESERVATION_TIMEOUT), any())).thenReturn(new ConfigurationManager.MaybeConfiguration(WAITING_QUEUE_RESERVATION_TIMEOUT));
    when(ticketRepository.selectWaitingTicketsForUpdate(eventId, Ticket.TicketStatus.PRE_RESERVED.name(), 1)).thenReturn(Collections.singletonList(ticket));
    when(waitingQueueRepository.loadAllWaitingForUpdate(eventId)).thenReturn(Collections.singletonList(subscription));
    when(waitingQueueRepository.loadWaiting(eventId, 1)).thenReturn(Collections.singletonList(subscription));
    Stream<Triple<WaitingQueueSubscription, TicketReservationWithOptionalCodeModification, ZonedDateTime>> stream = manager.distributeSeats(event);
    assertEquals(1L, stream.count());
    verify(waitingQueueRepository).loadAllWaitingForUpdate(eq(eventId));
    verify(waitingQueueRepository).loadWaiting(eq(eventId), eq(1));
    verify(ticketRepository).countWaiting(eq(eventId));
    verify(ticketRepository, never()).revertToFree(eq(eventId));
    verify(ticketRepository).countPreReservedTickets(eq(eventId));
    verify(ticketRepository).preReserveTicket(anyList());
    verify(ticketRepository).selectWaitingTicketsForUpdate(eq(eventId), anyString(), anyInt());
}
Also used : Triple(org.apache.commons.lang3.tuple.Triple) Ticket(alfio.model.Ticket) ConfigurationManager(alfio.manager.system.ConfigurationManager) WaitingQueueSubscription(alfio.model.WaitingQueueSubscription) ConfigurationKeyValuePathLevel(alfio.model.system.ConfigurationKeyValuePathLevel) Test(org.junit.jupiter.api.Test)

Aggregations

WaitingQueueSubscription (alfio.model.WaitingQueueSubscription)3 ConfigurationManager (alfio.manager.system.ConfigurationManager)2 Test (org.junit.jupiter.api.Test)2 TestConfiguration (alfio.TestConfiguration)1 DataSourceConfiguration (alfio.config.DataSourceConfiguration)1 Initializer (alfio.config.Initializer)1 UserManager (alfio.manager.user.UserManager)1 CustomerName (alfio.model.CustomerName)1 Event (alfio.model.Event)1 Ticket (alfio.model.Ticket)1 TicketCategory (alfio.model.TicketCategory)1 AlfioMetadata (alfio.model.metadata.AlfioMetadata)1 DateTimeModification (alfio.model.modification.DateTimeModification)1 TicketCategoryModification (alfio.model.modification.TicketCategoryModification)1 ConfigurationKeyValuePathLevel (alfio.model.system.ConfigurationKeyValuePathLevel)1 ConfigurationKeys (alfio.model.system.ConfigurationKeys)1 Organization (alfio.model.user.Organization)1 EventRepository (alfio.repository.EventRepository)1 TicketRepository (alfio.repository.TicketRepository)1 WaitingQueueRepository (alfio.repository.WaitingQueueRepository)1