Skip to content

Commit

Permalink
test: 메모리 작업 가능 공간 입장 기능 검증
Browse files Browse the repository at this point in the history
  • Loading branch information
lass9436 committed Aug 18, 2024
1 parent 538019b commit 8579281
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.thirdparty.ticketing.global.waitingsystem.memory.running;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.concurrent.ConcurrentHashMap;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

class MemoryRunningCounterTest {

private MemoryRunningCounter runningCounter;

@BeforeEach
void setUp() {
runningCounter = new MemoryRunningCounter(new ConcurrentHashMap<>());
}

@Nested
@DisplayName("카운터 증가 호출 시")
class IncrementTest {

@Test
@DisplayName("주어진 값만큼 값을 증가시킨다.")
void increment() {
// given
long performanceId = 1;
int number = 10;

// when
runningCounter.increment(performanceId, number);

// then
long runningCount = runningCounter.getRunningCounter(performanceId);
assertThat(runningCount).isEqualTo(number);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

Expand Down Expand Up @@ -58,4 +61,96 @@ void startCounterWithZeroValue() {
assertThat(runningCount).isEqualTo(0L);
}
}

@Nested
@DisplayName("작업 가능 인원 조회 시")
class GetAvailableToRunning {

@Test
@DisplayName("0보다 작으면 0을 반환한다.")
void returnZero_WhenLessThanZero() {
// given
long performanceId = 1;
Set<WaitingMember> waitingMembers = new HashSet<>();
for (int i = 0; i < 150; i++) {
waitingMembers.add(
new WaitingMember(
"email" + i + "@email.com", performanceId, i, ZonedDateTime.now()));
}
runningManager.enterRunningRoom(performanceId, waitingMembers);

// when
long availableToRunning = runningManager.getAvailableToRunning(performanceId);

// then
assertThat(availableToRunning).isEqualTo(0);
}

@Test
@DisplayName("0보다 크면 그대로 반환한다.")
void returnAvailable_WhenGreaterThanZero() {
// given
long performanceId = 1;
Set<WaitingMember> waitingMembers = new HashSet<>();
for (int i = 0; i < 20; i++) {
waitingMembers.add(
new WaitingMember(
"email" + i + "@email.com", performanceId, i, ZonedDateTime.now()));
}
runningManager.enterRunningRoom(performanceId, waitingMembers);

// when
long runningCount = runningManager.getAvailableToRunning(performanceId);

// then
assertThat(runningCount).isEqualTo(80);
}
}

@Nested
@DisplayName("작업 가능 공간 입장 호출 시")
class EnterRunningRoomTest {

private Set<WaitingMember> waitingMembers;
private int waitingMemberCount;
private long performanceId;

@BeforeEach
void setUp() {
waitingMemberCount = 20;
performanceId = 1;
waitingMembers = new HashSet<>();
for (int i = 0; i < waitingMemberCount; i++) {
waitingMembers.add(
new WaitingMember(
"email" + i + "@email.com", performanceId, i, ZonedDateTime.now()));
}
}

@Test
@DisplayName("입장 인원만큼 작업 가능 공간 이동 인원 카운터를 증가시킨다.")
void incrementRunningCounter() {
// given

// when
runningManager.enterRunningRoom(performanceId, waitingMembers);

// then
long runningCount = runningManager.getRunningCount(performanceId);
assertThat(runningCount).isEqualTo(waitingMemberCount);
}

@Test
@DisplayName("작업 가능 공간에 사용자를 추가한다.")
void enterRunningRoom() {
// given

// when
runningManager.enterRunningRoom(performanceId, waitingMembers);

// then
Set<String> waitingMembers = room.get(performanceId).keySet();
assertThat(waitingMembers).hasSize(waitingMemberCount);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.time.ZonedDateTime;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

Expand Down Expand Up @@ -100,4 +103,35 @@ void getAvailableToRunning(int membersCount, int expectAvailableToRunning) {
assertThat(availableToRunning).isEqualTo(expectAvailableToRunning);
}
}

@Nested
@DisplayName("작업 가능 공간 입장 호출 시")
class EnterTest {

@Test
@DisplayName("사용자의 이메일 정보를 작업 가능 공간에 넣는다.")
void putEmailInfo() {
// given
long performanceId = 1;
Set<WaitingMember> waitingMembers = new HashSet<>();
for (int i = 0; i < 10; i++) {
waitingMembers.add(
new WaitingMember(
"email" + i + "@email.com", performanceId, i, ZonedDateTime.now()));
}

// when
runningRoom.enter(performanceId, waitingMembers);

// then
String[] emails =
waitingMembers.stream().map(WaitingMember::getEmail).toArray(String[]::new);
// 각 이메일이 runningRoom 에 존재하는지 확인
for (String email : emails) {
assertThat(runningRoom.contains(email, performanceId)).isTrue();
}
// 존재하지 않는 이메일은 false 를 반환하는지 확인
assertThat(runningRoom.contains("[email protected]", performanceId)).isFalse();
}
}
}

0 comments on commit 8579281

Please sign in to comment.