Skip to content

Commit

Permalink
Fixes in homework 7
Browse files Browse the repository at this point in the history
  • Loading branch information
arhostcode committed Nov 29, 2023
1 parent 50e8b36 commit 364e959
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 39 deletions.
18 changes: 8 additions & 10 deletions src/main/java/edu/hw7/task3/AbstractPersonDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,26 @@ public void delete(int id) {
@Override
@Nullable
public List<Person> findByName(String name) {
if (!cachedNames.containsKey(name)) {
return null;
}
return cachedNames.get(name).stream().toList();
return getFromCache(cachedNames, name);
}

@Override
@Nullable
public List<Person> findByAddress(String address) {
if (!cachedAddresses.containsKey(address)) {
return null;
}
return cachedAddresses.get(address).stream().toList();
return getFromCache(cachedAddresses, address);
}

@Override
@Nullable
public List<Person> findByPhone(String phone) {
if (!cachedPhones.containsKey(phone)) {
return getFromCache(cachedPhones, phone);
}

private List<Person> getFromCache(Map<String, List<Person>> cache, String key) {
if (!cache.containsKey(key)) {
return null;
}
return cachedPhones.get(phone).stream().toList();
return cache.get(key);
}

}
9 changes: 1 addition & 8 deletions src/main/java/edu/hw7/task4/MonteCarloPiComputer.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.hw7.task4;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadLocalRandom;
Expand All @@ -26,15 +25,9 @@ public static double computePIParallel(long n, int threadCount) {

// Not using atomics here to speed up the process
Future<Long>[] futures = new Future[threadCount];
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for (int thread = 0; thread < threadCount; thread++) {
futures[thread] = executorService.submit(() -> {
long countInThread = countRandomCollisions(n / threadCount);
countDownLatch.countDown();
return countInThread;
});
futures[thread] = executorService.submit(() -> countRandomCollisions(n / threadCount));
}
countDownLatch.await();
for (int i = 0; i < threadCount; i++) {
count += futures[i].get();
}
Expand Down
41 changes: 20 additions & 21 deletions src/test/java/edu/hw7/task3/PersonDatabaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,36 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import lombok.SneakyThrows;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.assertAll;

public class PersonDatabaseTest {

@Test
@DisplayName("Тестирование LockCachingPersonDatabase")
public void lockDatabase_shouldReturnCorrectPerson_whenPersonAdded() {
PersonDatabase personDatabase = new LockCachingPersonDatabase();
testDatabase(personDatabase);
}

@Test
@DisplayName("Тестирование SynchronizedCachingPersonDatabase")
public void synchronizedDatabase_shouldReturnCorrectPerson_whenPersonAdded() {
PersonDatabase personDatabase = new SynchronizedCachingPersonDatabase() {
@SneakyThrows
@Override
public synchronized void add(Person person) {
Thread.sleep(3000); // Imitation of slow database
super.add(person);
}
};
testDatabase(personDatabase);
private static Stream<Arguments> databaseArguments() {
return Stream.of(
Arguments.of(new LockCachingPersonDatabase()),
Arguments.of(new SynchronizedCachingPersonDatabase() {
@SneakyThrows
@Override
public synchronized void add(Person person) {
Thread.sleep(3000); // Imitation of slow database
super.add(person);
}
})
);
}

@SneakyThrows
private static void testDatabase(PersonDatabase personDatabase) {
@ParameterizedTest
@DisplayName("Тестирование PersonDatabase")
@MethodSource("databaseArguments")
public void personDatabase_shouldReturnCorrectPerson_whenPersonAdded(AbstractPersonDatabase personDatabase) {
var executorService = Executors.newFixedThreadPool(5);
var person = new Person(1, "Ivan", "Moscow", "1234567890");
executorService.execute(() -> personDatabase.add(person));
Expand Down

0 comments on commit 364e959

Please sign in to comment.