Skip to content

Commit

Permalink
minor renames
Browse files Browse the repository at this point in the history
- rename bytesPerSeconds() -> bytesPerSecond()
- rename TooMuchAnnouncesFailedInARawException -> TooManyAnnouncesFailedInARawException
  • Loading branch information
laur89 committed Sep 8, 2022
1 parent ed45580 commit e476a13
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import static java.util.Optional.ofNullable;
import static java.util.concurrent.TimeUnit.MINUTES;
import static org.apache.commons.lang3.ObjectUtils.getIfNull;

@Slf4j
Expand All @@ -30,7 +30,7 @@ public class BandwidthDispatcher implements BandwidthDispatcherFacade, Runnable
private volatile boolean stop;
private Thread thread;

private static final long TWENTY_MINS_MS = TimeUnit.MINUTES.toMillis(20);
private static final long TWENTY_MINS_MS = MINUTES.toMillis(20);


public BandwidthDispatcher(final int threadPauseIntervalMs, final RandomSpeedProvider randomSpeedProvider) {
Expand Down Expand Up @@ -102,7 +102,7 @@ public void run() {

for (final Map.Entry<InfoHash, TorrentSeedStats> entry : entrySet) {
final long speedInBytesPerSecond = ofNullable(this.speedMap.get(entry.getKey()))
.map(Speed::getBytesPerSeconds)
.map(Speed::getBytesPerSecond)
.orElse(0L);
// Divide by 1000 because of the thread pause interval being in milliseconds
// The multiplication HAS to be done before the division, otherwise we're going to have trailing zeroes
Expand All @@ -129,7 +129,7 @@ public void registerTorrent(final InfoHash infoHash) {
this.lock.writeLock().lock();
try {
this.torrentsSeedStats.put(infoHash, new TorrentSeedStats());
this.speedMap.put(infoHash, new Speed(0));
this.speedMap.put(infoHash, new Speed());
} finally {
this.lock.writeLock().unlock();
}
Expand Down Expand Up @@ -169,12 +169,12 @@ void recomputeSpeeds() {
for (final InfoHash infohash : this.torrentsSeedStats.keySet()) {
this.speedMap.compute(infohash, (hash, speed) -> {
if (speed == null) {
return new Speed(0);
return new Speed();
}
double percentOfSpeedAssigned = this.weightHolder.getTotalWeight() == 0.0
? 0.0
: this.weightHolder.getWeightFor(infohash) / this.weightHolder.getTotalWeight();
speed.setBytesPerSeconds((long) (this.randomSpeedProvider.getCurrentSpeed() * percentOfSpeedAssigned));
speed.setBytesPerSecond((long) (this.randomSpeedProvider.getCurrentSpeed() * percentOfSpeedAssigned));

return speed;
});
Expand All @@ -189,7 +189,7 @@ void recomputeSpeeds() {
final StringBuilder sb = new StringBuilder("All torrents speeds has been refreshed:\n");
final double totalWeight = this.weightHolder.getTotalWeight();
this.speedMap.forEach((infoHash, speed) -> {
final String humanReadableSpeed = FileUtils.byteCountToDisplaySize(speed.getBytesPerSeconds());
final String humanReadableSpeed = FileUtils.byteCountToDisplaySize(speed.getBytesPerSecond());
final double torrentWeight = this.weightHolder.getWeightFor(infoHash);
final double weightInPercent = torrentWeight > 0.0
? totalWeight / torrentWeight * 100
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/araymond/joal/core/bandwith/Speed.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Speed {
private long bytesPerSeconds;
private long bytesPerSecond;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,23 @@
import org.araymond.joal.web.messages.outgoing.MessagePayload;

import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

@Getter
public class SeedingSpeedHasChangedPayload implements MessagePayload {
private final List<SpeedPayload> speeds;

public SeedingSpeedHasChangedPayload(final SeedingSpeedsHasChangedEvent event) {
this.speeds = event.getSpeeds().entrySet().stream()
.map(entry -> new SpeedPayload(entry.getKey(), entry.getValue().getBytesPerSeconds()))
.collect(Collectors.toList());
}

public List<SpeedPayload> getSpeeds() {
return speeds;
.map(entry -> new SpeedPayload(entry.getKey(), entry.getValue().getBytesPerSecond()))
.collect(toList());
}

@Getter
@RequiredArgsConstructor
public static final class SpeedPayload {
private final InfoHash infoHash;
private final Long bytesPerSeconds;
private final Long bytesPerSecond;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class BandwidthDispatcherTest {
@Test
public void shouldRefreshRandomSpeedProviderAndRecomputeSpeed() {
final RandomSpeedProvider speedProvider = Mockito.mock(RandomSpeedProvider.class);
Mockito.doReturn(1000000L).when(speedProvider).getCurrentSpeed();
Mockito.doReturn(1000_000L).when(speedProvider).getCurrentSpeed();

final BandwidthDispatcher bandwidthDispatcher = spy(new BandwidthDispatcher(2, speedProvider));

Expand All @@ -37,7 +37,7 @@ public void shouldRefreshRandomSpeedProviderAndRecomputeSpeed() {
@Test
public void shouldReturnZeroIfInfoHashIsNotRegistered() throws InterruptedException {
final RandomSpeedProvider speedProvider = Mockito.mock(RandomSpeedProvider.class);
Mockito.doReturn(1000000L).when(speedProvider).getCurrentSpeed();
Mockito.doReturn(1000_000L).when(speedProvider).getCurrentSpeed();

final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(2, speedProvider);
bandwidthDispatcher.start();
Expand All @@ -55,7 +55,7 @@ public void shouldReturnZeroIfInfoHashIsNotRegistered() throws InterruptedExcept
@Test
public void shouldNotIncrementRegisteredTorrentsBeforePeersHaveBeenAdded() throws InterruptedException {
final RandomSpeedProvider speedProvider = Mockito.mock(RandomSpeedProvider.class);
Mockito.doReturn(1000000L).when(speedProvider).getCurrentSpeed();
Mockito.doReturn(1000_000L).when(speedProvider).getCurrentSpeed();

final InfoHash infoHash = new InfoHash(new byte[]{12});
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(2, speedProvider);
Expand All @@ -76,7 +76,7 @@ public void shouldNotIncrementRegisteredTorrentsBeforePeersHaveBeenAdded() throw
@Test
public void shouldNotIncrementRegisteredTorrentsWithZeroSeeders() throws InterruptedException {
final RandomSpeedProvider speedProvider = Mockito.mock(RandomSpeedProvider.class);
Mockito.doReturn(1000000L).when(speedProvider).getCurrentSpeed();
Mockito.doReturn(1000_000L).when(speedProvider).getCurrentSpeed();

final InfoHash infoHash = new InfoHash(new byte[]{12});
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(2, speedProvider);
Expand All @@ -98,7 +98,7 @@ public void shouldNotIncrementRegisteredTorrentsWithZeroSeeders() throws Interru
@Test
public void shouldNotIncrementRegisteredTorrentsWithZeroLeechers() throws InterruptedException {
final RandomSpeedProvider speedProvider = Mockito.mock(RandomSpeedProvider.class);
Mockito.doReturn(1000000L).when(speedProvider).getCurrentSpeed();
Mockito.doReturn(1000_000L).when(speedProvider).getCurrentSpeed();

final InfoHash infoHash = new InfoHash(new byte[]{12});
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(2, speedProvider);
Expand All @@ -120,7 +120,7 @@ public void shouldNotIncrementRegisteredTorrentsWithZeroLeechers() throws Interr
@Test
public void shouldRemoveUnregisteredTorrent() throws InterruptedException {
final RandomSpeedProvider speedProvider = Mockito.mock(RandomSpeedProvider.class);
Mockito.doReturn(1000000L).when(speedProvider).getCurrentSpeed();
Mockito.doReturn(1000_000L).when(speedProvider).getCurrentSpeed();

final InfoHash infoHash = new InfoHash(new byte[]{12});
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(2, speedProvider);
Expand All @@ -144,7 +144,7 @@ public void shouldRemoveUnregisteredTorrent() throws InterruptedException {
@Test
public void shouldIncrementRegisteredTorrent() throws InterruptedException {
final RandomSpeedProvider speedProvider = Mockito.mock(RandomSpeedProvider.class);
Mockito.doReturn(1000000L).when(speedProvider).getCurrentSpeed();
Mockito.doReturn(1000_000L).when(speedProvider).getCurrentSpeed();

final InfoHash infoHash = new InfoHash(new byte[]{12});
final InfoHash infoHash2 = new InfoHash(new byte[]{100});
Expand All @@ -169,7 +169,7 @@ public void shouldIncrementRegisteredTorrent() throws InterruptedException {
@Test
public void shouldBeSafeToUpdateTorrentSeedsStatsWhileRegisteringTorrents() throws ExecutionException, InterruptedException {
final RandomSpeedProvider speedProvider = Mockito.mock(RandomSpeedProvider.class);
Mockito.doReturn(1000000L).when(speedProvider).getCurrentSpeed();
Mockito.doReturn(1000_000L).when(speedProvider).getCurrentSpeed();

final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(1, speedProvider);

Expand All @@ -178,7 +178,7 @@ public void shouldBeSafeToUpdateTorrentSeedsStatsWhileRegisteringTorrents() thro
final ExecutorService executorService = Executors.newFixedThreadPool(5);
final Collection<Future<?>> futures = new ArrayList<>();
for (int i = 0; i < 25; ++i) {
final InfoHash infoHash = new InfoHash((i + "").getBytes());
final InfoHash infoHash = new InfoHash((String.valueOf(i)).getBytes());
futures.add(executorService.submit(() -> {
bandwidthDispatcher.registerTorrent(infoHash);
bandwidthDispatcher.updateTorrentPeers(infoHash, 20, 50);
Expand All @@ -195,7 +195,7 @@ public void shouldBeSafeToUpdateTorrentSeedsStatsWhileRegisteringTorrents() thro
@Test
public void shouldNotifyThatSpeedHasChangedAfterRegisteringTorrent() throws InterruptedException {
final RandomSpeedProvider speedProvider = Mockito.mock(RandomSpeedProvider.class);
Mockito.doReturn(10000L).when(speedProvider).getCurrentSpeed();
Mockito.doReturn(10_000L).when(speedProvider).getCurrentSpeed();

final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(2, speedProvider);
bandwidthDispatcher.start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doReturn;

public class DigitRangeTransformedToHexWithoutLeadingZeroAlgorithmTest {

@Test
public void shouldNotBuildWithNullLowerBound() {
assertThatThrownBy(() -> new DigitRangeTransformedToHexWithoutLeadingZeroAlgorithm(null, 10000L))
assertThatThrownBy(() -> new DigitRangeTransformedToHexWithoutLeadingZeroAlgorithm(null, 10_000L))
.isInstanceOf(TorrentClientConfigIntegrityException.class);
}
@Test
Expand Down

0 comments on commit e476a13

Please sign in to comment.