Skip to content

Commit

Permalink
fix: apply spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminuj committed Sep 17, 2024
1 parent 57bb723 commit c84d0f5
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package project.seatsence.src.utilization.service.reservation;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.time.LocalDateTime;
import jdk.jfr.StackTrace;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import project.seatsence.global.code.ResponseCode;
Expand All @@ -29,19 +26,23 @@ public long chairReservation(
Reservation reservation,
ChairUtilizationRequest chairUtilizationRequest,
String userEmail,
User user) throws JsonProcessingException {
User user)
throws JsonProcessingException {
long chairId = -1;

try {
// lockRepository.getLock(storeChair.getId().toString());
// lockRepository.getLock(storeChair.getId().toString());

Integer lock = lockRepository.getLock(storeChair.getId().toString());
if (lock == 1) {
System.out.println("Lock acquired by: " + Thread.currentThread().getName());
} else if (lock == 0){
System.out.println("Failed to acquire lock during timeout: " + Thread.currentThread().getName());
} else if (lock == 0) {
System.out.println(
"Failed to acquire lock during timeout: "
+ Thread.currentThread().getName());
} else if (lock == null) {
System.out.println("Failed to acquire lock error!: " + Thread.currentThread().getName());
System.out.println(
"Failed to acquire lock error!: " + Thread.currentThread().getName());
}

chairId =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import project.seatsence.global.exceptions.BaseException;
import project.seatsence.src.store.domain.CustomUtilizationField;
import project.seatsence.src.store.domain.ReservationUnit;
import project.seatsence.src.store.domain.Store;
import project.seatsence.src.store.domain.StoreChair;
import project.seatsence.src.store.service.StoreChairService;
import project.seatsence.src.store.service.StoreCustomService;
Expand All @@ -42,7 +41,6 @@ public class ReservationService {
private final StoreCustomService storeCustomService;
private final CustomUtilizationContentRepository customUtilizationContentRepository;


// test
private final StoreChairService storeChairService;
private final UserService userService;
Expand Down Expand Up @@ -98,46 +96,53 @@ public long checkExistsReservationDateTimeAndSave(
Reservation reservation,
ChairUtilizationRequest chairUtilizationRequest,
String userEmail,
User user) throws JsonProcessingException {
User user)
throws JsonProcessingException {
List<ReservationStatus> reservationStatuses = new ArrayList<>();
reservationStatuses.add(ReservationStatus.APPROVED);
reservationStatuses.add(ReservationStatus.PENDING);
System.out.println("겹치는 예약 데이터 조회 전 : " + Thread.currentThread().getName());
System.out.println("겹치는 예약 데이터 조회 전 : " + Thread.currentThread().getName());
List<Reservation> reservationsBasedStartDateTime =
findAllByReservedStoreChairAndReservationStatusInAndStartScheduleIsBeforeAndEndScheduleIsAfterAndState(
storeChair, reservationStatuses, chairUtilizationRequest.getStartSchedule(), chairUtilizationRequest.getStartSchedule());
System.out.println("겹치는 예약 데이터 조회 후 : " + Thread.currentThread().getName());
storeChair,
reservationStatuses,
chairUtilizationRequest.getStartSchedule(),
chairUtilizationRequest.getStartSchedule());
System.out.println("겹치는 예약 데이터 조회 후 : " + Thread.currentThread().getName());
if (reservationsBasedStartDateTime.size() > 0) {
System.out.println("예약 시간 겹침 : " + Thread.currentThread().getName());
System.out.println("예약 시간 겹침 : " + Thread.currentThread().getName());
return -1;
}

List<Reservation> reservationsBasedEndDateTime =
findAllByReservedStoreChairAndReservationStatusInAndStartScheduleIsBeforeAndEndScheduleIsAfterAndState(
storeChair, reservationStatuses, chairUtilizationRequest.getEndSchedule(), chairUtilizationRequest.getEndSchedule());
storeChair,
reservationStatuses,
chairUtilizationRequest.getEndSchedule(),
chairUtilizationRequest.getEndSchedule());

if (reservationsBasedEndDateTime.size() > 0) {
System.out.println("예약 시간 겹침 : " + Thread.currentThread().getName());
System.out.println("예약 시간 겹침 : " + Thread.currentThread().getName());
return -1;
}

System.out.println("예약 가능 : " + Thread.currentThread().getName());

// StoreChair storeChair1 =
// storeChairService.findByIdAndState(1L);
// User user1 = userService.findByEmailAndState(userEmail);
// Store store1 = storeService.findByIdAndState(1L);
//
//
// Reservation reservation1 =
// Reservation.builder()
// .store(store1)
// .reservedStoreChair(storeChair1)
// .reservedStoreSpace(null)
// .user(user1)
// .startSchedule(startDateTime)
// .endSchedule(endDateTime)
// .build();
// StoreChair storeChair1 =
// storeChairService.findByIdAndState(1L);
// User user1 = userService.findByEmailAndState(userEmail);
// Store store1 = storeService.findByIdAndState(1L);
//
//
// Reservation reservation1 =
// Reservation.builder()
// .store(store1)
// .reservedStoreChair(storeChair1)
// .reservedStoreSpace(null)
// .user(user1)
// .startSchedule(startDateTime)
// .endSchedule(endDateTime)
// .build();

long reservationId = save(reservation).getId();
inputChairCustomUtilizationContent(user, reservation, chairUtilizationRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import project.seatsence.global.code.ResponseCode;
import project.seatsence.global.exceptions.BaseException;
import project.seatsence.global.response.SliceResponse;
import project.seatsence.src.store.domain.CustomUtilizationField;
import project.seatsence.src.store.domain.Store;
Expand Down Expand Up @@ -391,18 +389,14 @@ public long chairReservation(String userEmail, ChairUtilizationRequest chairUtil
// 동시성처리: 의자 예약 일정 겹치는지 검사 && 일정 겹치지 않으면 예약 DB에 저장
long savedId =
namedLockUserReservationFacade.chairReservation(
storeChair,
reservation,
chairUtilizationRequest,
userEmail,
user);

// if (savedId == -1) {
// System.out.println("service: 이미 예약이 존재!!!");
// throw new BaseException(ResponseCode.RESERVATION_ALREADY_EXIST);
// }

// inputChairCustomUtilizationContent(user, reservation, chairUtilizationRequest);
storeChair, reservation, chairUtilizationRequest, userEmail, user);

// if (savedId == -1) {
// System.out.println("service: 이미 예약이 존재!!!");
// throw new BaseException(ResponseCode.RESERVATION_ALREADY_EXIST);
// }

// inputChairCustomUtilizationContent(user, reservation, chairUtilizationRequest);

return savedId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -16,12 +15,7 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import org.springframework.transaction.annotation.Transactional;
import project.seatsence.global.code.ResponseCode;
import project.seatsence.global.entity.BaseTimeAndStateEntity.State;
import project.seatsence.global.exceptions.BaseException;
import project.seatsence.src.store.dao.StoreChairRepository;
import project.seatsence.src.store.domain.StoreChair;
import project.seatsence.src.utilization.dto.request.ChairUtilizationRequest;
import project.seatsence.src.utilization.dto.request.CustomUtilizationContentRequest;

Expand All @@ -43,8 +37,12 @@ class UserReservationServiceTest {
@Sql(
scripts = "/sql/store-table-test-data.sql",
executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/custom-utilization-field-test-data.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/sql/delete-all-test-data.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
@Sql(
scripts = "/sql/custom-utilization-field-test-data.sql",
executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(
scripts = "/sql/delete-all-test-data.sql",
executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
void testChairReservationConcurrency() throws InterruptedException {
// Given
int numberOfThreads = 20;
Expand Down Expand Up @@ -96,7 +94,8 @@ void testChairReservationConcurrency() throws InterruptedException {
service.execute(
() -> {
long id = -1;
System.out.println("gildong@naver.com 실행 스레드: " + Thread.currentThread().getName());
System.out.println(
"gildong@naver.com 실행 스레드: " + Thread.currentThread().getName());

try {
id =
Expand All @@ -106,7 +105,8 @@ void testChairReservationConcurrency() throws InterruptedException {
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
} catch (BaseException baseException) {
// log.error(ResponseCode.RESERVATION_ALREADY_EXIST.getMessage());
//
// log.error(ResponseCode.RESERVATION_ALREADY_EXIST.getMessage());
log.error("error!: " + Arrays.toString(baseException.getStackTrace()));
}

Expand All @@ -121,7 +121,8 @@ void testChairReservationConcurrency() throws InterruptedException {
service.execute(
() -> {
long id = -1;
System.out.println("minji@naver.com 실행 스레드: " + Thread.currentThread().getName());
System.out.println(
"minji@naver.com 실행 스레드: " + Thread.currentThread().getName());

try {
id =
Expand All @@ -131,7 +132,8 @@ void testChairReservationConcurrency() throws InterruptedException {
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
} catch (BaseException baseException) {
// log.error(ResponseCode.RESERVATION_ALREADY_EXIST.getMessage());
//
// log.error(ResponseCode.RESERVATION_ALREADY_EXIST.getMessage());
log.error("error!: " + Arrays.toString(baseException.getStackTrace()));
}

Expand All @@ -144,27 +146,31 @@ void testChairReservationConcurrency() throws InterruptedException {
});
latch.await();

// // 사용자 1의 예약 처리
// try {
// long id1 = userReservationService.chairReservation(user1Email, utilizationRequest1);
// ids.add(id1);
// log.info("User 1's reservation successful, id: " + id1);
// } catch (JsonProcessingException e) {
// throw new RuntimeException(e);
// } catch (BaseException baseException) {
// log.error("User 1 reservation failed: " + Arrays.toString(baseException.getStackTrace()));
// }
//
// // 사용자 2의 예약 처리 (순차적으로 실행)
// try {
// long id2 = userReservationService.chairReservation(user2Email, utilizationRequest2);
// ids.add(id2);
// log.info("User 2's reservation successful, id: " + id2);
// } catch (JsonProcessingException e) {
// throw new RuntimeException(e);
// } catch (BaseException baseException) {
// log.error("User 2 reservation failed: " + Arrays.toString(baseException.getStackTrace()));
// }
// // 사용자 1의 예약 처리
// try {
// long id1 = userReservationService.chairReservation(user1Email,
// utilizationRequest1);
// ids.add(id1);
// log.info("User 1's reservation successful, id: " + id1);
// } catch (JsonProcessingException e) {
// throw new RuntimeException(e);
// } catch (BaseException baseException) {
// log.error("User 1 reservation failed: " +
// Arrays.toString(baseException.getStackTrace()));
// }
//
// // 사용자 2의 예약 처리 (순차적으로 실행)
// try {
// long id2 = userReservationService.chairReservation(user2Email,
// utilizationRequest2);
// ids.add(id2);
// log.info("User 2's reservation successful, id: " + id2);
// } catch (JsonProcessingException e) {
// throw new RuntimeException(e);
// } catch (BaseException baseException) {
// log.error("User 2 reservation failed: " +
// Arrays.toString(baseException.getStackTrace()));
// }

// Then
Assertions.assertThat(ids.size()).isEqualTo(1);
Expand Down

0 comments on commit c84d0f5

Please sign in to comment.