Add test for Events

This commit is contained in:
anthonyraymond 2017-05-25 23:57:17 +02:00
parent 9e87ced626
commit 0fe901be7e
25 changed files with 345 additions and 52 deletions

View file

@ -4,10 +4,10 @@ import com.turn.ttorrent.common.protocol.TrackerMessage.AnnounceRequestMessage.R
import org.apache.commons.io.FileUtils;
import org.araymond.joal.core.events.*;
import org.araymond.joal.core.events.announce.AnnounceRequestingEvent;
import org.araymond.joal.core.events.filechange.TorrentFileAddedForSeed;
import org.araymond.joal.core.events.seedsession.SeedSessionHasEnded;
import org.araymond.joal.core.events.seedsession.SeedSessionHasStarted;
import org.araymond.joal.core.events.seedsession.SeedSessionWillStart;
import org.araymond.joal.core.events.filechange.TorrentFileAddedForSeedEvent;
import org.araymond.joal.core.events.seedsession.SeedSessionHasEndedEvent;
import org.araymond.joal.core.events.seedsession.SeedSessionHasStartedEvent;
import org.araymond.joal.core.events.seedsession.SeedSessionWillStartEvent;
import org.araymond.joal.core.ttorent.client.bandwidth.TorrentWithStats;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -60,8 +60,8 @@ public class CoreEventListener {
@Async
@Order(Ordered.HIGHEST_PRECEDENCE)
@EventListener
void handleNoMoreTorrents(final NoMoreTorrentsFileAvailable event) {
logger.debug("Event NoMoreTorrentsFileAvailable caught.");
void handleNoMoreTorrents(final NoMoreTorrentsFileAvailableEvent event) {
logger.debug("Event NoMoreTorrentsFileAvailableEvent caught.");
/*logger.warn("There is no more .torrent file, add some more to resume seed.");
this.manager.stop();*/
}
@ -69,30 +69,30 @@ public class CoreEventListener {
@Async
@Order(Ordered.HIGHEST_PRECEDENCE)
@EventListener
void handleNoMoreLeechers(final NoMoreLeechers event) throws IOException {
logger.debug("Event NoMoreLeechers caught.");
void handleNoMoreLeechers(final NoMoreLeechersEvent event) throws IOException {
logger.debug("Event NoMoreLeechersEvent caught.");
//logger.warn("0 peers are currently leeching, moving torrent to archived and restarting seed.");
}
@Async
@Order(Ordered.HIGHEST_PRECEDENCE)
@EventListener
void handleTorrentFileAddedForSeed(final TorrentFileAddedForSeed event) throws IOException {
logger.debug("Event TorrentFileAddedForSeed caught.");
void handleTorrentFileAddedForSeed(final TorrentFileAddedForSeedEvent event) throws IOException {
logger.debug("Event TorrentFileAddedForSeedEvent caught.");
}
@Async
@Order(Ordered.HIGHEST_PRECEDENCE)
@EventListener
void handleSeedSessionWillStart(final SeedSessionWillStart event) {
logger.debug("Event SeedSessionWillStart caught.");
void handleSeedSessionWillStart(final SeedSessionWillStartEvent event) {
logger.debug("Event SeedSessionWillStartEvent caught.");
}
@Async
@Order(Ordered.HIGHEST_PRECEDENCE)
@EventListener
void handleSeedSessionHasStarted(final SeedSessionHasStarted event) {
logger.debug("Event SeedSessionHasStarted caught.");
void handleSeedSessionHasStarted(final SeedSessionHasStartedEvent event) {
logger.debug("Event SeedSessionHasStartedEvent caught.");
// TODO : add a log to tell which BitTorrent client.
// TODO : detailed BitTorrent client log at debug log level
}
@ -100,16 +100,16 @@ public class CoreEventListener {
@Async
@Order(Ordered.HIGHEST_PRECEDENCE)
@EventListener
void handleSeedSessionHasEnded(final SeedSessionHasEnded event) {
logger.debug("Event SeedSessionHasEnded caught.");
void handleSeedSessionHasEnded(final SeedSessionHasEndedEvent event) {
logger.debug("Event SeedSessionHasEndedEvent caught.");
// TODO : log that the seed session is over
}
@Async
@Order(Ordered.HIGHEST_PRECEDENCE)
@EventListener
void handleSomethingHasFuckedUp(final SomethingHasFuckedUp event) {
logger.error("Event SomethingHasFuckedUp caught.", event.getException());
void handleSomethingHasFuckedUp(final SomethingHasFuckedUpEvent event) {
logger.error("Event SomethingHasFuckedUpEvent caught.", event.getException());
// Stop the application
SpringApplication.exit(appContext, () -> 42);
}

View file

@ -5,9 +5,9 @@ import com.turn.ttorrent.common.Torrent;
import org.araymond.joal.core.client.emulated.BitTorrentClient;
import org.araymond.joal.core.client.emulated.BitTorrentClientProvider;
import org.araymond.joal.core.config.JoalConfigProvider;
import org.araymond.joal.core.events.seedsession.SeedSessionHasEnded;
import org.araymond.joal.core.events.seedsession.SeedSessionHasStarted;
import org.araymond.joal.core.events.seedsession.SeedSessionWillStart;
import org.araymond.joal.core.events.seedsession.SeedSessionHasEndedEvent;
import org.araymond.joal.core.events.seedsession.SeedSessionHasStartedEvent;
import org.araymond.joal.core.events.seedsession.SeedSessionWillStartEvent;
import org.araymond.joal.core.torrent.watcher.TorrentFileProvider;
import org.araymond.joal.core.ttorent.client.Client;
import org.araymond.joal.core.ttorent.client.ConnectionHandler;
@ -66,7 +66,7 @@ public class SeedManager {
final BitTorrentClient bitTorrentClient = bitTorrentClientProvider.get();
// TODO : still need to handle exception in this method to prevent crash on startup, particularly NoMoreTorrent
publisher.publishEvent(new SeedSessionWillStart());
publisher.publishEvent(new SeedSessionWillStartEvent());
final String id = bitTorrentClient.getPeerId();
final Peer peer = new Peer(
@ -84,13 +84,13 @@ public class SeedManager {
this.currentClient.share();
publisher.publishEvent(new SeedSessionHasStarted(bitTorrentClient));
publisher.publishEvent(new SeedSessionHasStartedEvent(bitTorrentClient));
}
public void stop() {
if (currentClient != null) {
this.currentClient.stop();
this.publisher.publishEvent(new SeedSessionHasEnded());
this.publisher.publishEvent(new SeedSessionHasEndedEvent());
}
}

View file

@ -1,15 +1,17 @@
package org.araymond.joal.core.events;
import com.google.common.base.Preconditions;
import org.araymond.joal.core.ttorent.client.MockedTorrent;
/**
* Created by raymo on 05/05/2017.
*/
public class NoMoreLeechers {
public class NoMoreLeechersEvent {
private final MockedTorrent torrent;
public NoMoreLeechers(final MockedTorrent torrent) {
public NoMoreLeechersEvent(final MockedTorrent torrent) {
Preconditions.checkNotNull(torrent, "MockedTorrent cannot be null.");
this.torrent = torrent;
}

View file

@ -3,6 +3,6 @@ package org.araymond.joal.core.events;
/**
* Created by raymo on 05/05/2017.
*/
public class NoMoreTorrentsFileAvailable {
public class NoMoreTorrentsFileAvailableEvent {
}

View file

@ -1,13 +1,16 @@
package org.araymond.joal.core.events;
import com.google.common.base.Preconditions;
/**
* Created by raymo on 11/05/2017.
*/
public class SomethingHasFuckedUp {
public class SomethingHasFuckedUpEvent {
private final Throwable exception;
public SomethingHasFuckedUp(final Throwable exception) {
public SomethingHasFuckedUpEvent(final Throwable exception) {
Preconditions.checkNotNull(exception, "Exception cannot be null.");
this.exception = exception;
}

View file

@ -1,5 +1,6 @@
package org.araymond.joal.core.events.announce;
import com.google.common.base.Preconditions;
import com.turn.ttorrent.common.protocol.TrackerMessage.AnnounceRequestMessage.RequestEvent;
import org.araymond.joal.core.ttorent.client.bandwidth.TorrentWithStats;
@ -11,6 +12,8 @@ public class AnnounceRequestingEvent {
private final TorrentWithStats torrent;
public AnnounceRequestingEvent(final RequestEvent event, final TorrentWithStats torrent) {
Preconditions.checkNotNull(event, "RequestEvent cannot be null");
Preconditions.checkNotNull(torrent, "TorrentWithStats cannot be null");
this.event = event;
this.torrent = torrent;
}

View file

@ -1,14 +1,16 @@
package org.araymond.joal.core.events.filechange;
import com.google.common.base.Preconditions;
import org.araymond.joal.core.ttorent.client.MockedTorrent;
/**
* Created by raymo on 06/05/2017.
*/
public class TorrentFileAddedForSeed {
public class TorrentFileAddedForSeedEvent {
private final MockedTorrent torrent;
public TorrentFileAddedForSeed(final MockedTorrent torrent) {
public TorrentFileAddedForSeedEvent(final MockedTorrent torrent) {
Preconditions.checkNotNull(torrent, "MockedTorrent cannot be null.");
this.torrent = torrent;
}

View file

@ -3,5 +3,5 @@ package org.araymond.joal.core.events.seedsession;
/**
* Created by raymo on 06/05/2017.
*/
public class SeedSessionHasEnded {
public class SeedSessionHasEndedEvent {
}

View file

@ -1,15 +1,17 @@
package org.araymond.joal.core.events.seedsession;
import com.google.common.base.Preconditions;
import org.araymond.joal.core.client.emulated.BitTorrentClient;
/**
* Created by raymo on 06/05/2017.
*/
public class SeedSessionHasStarted {
public class SeedSessionHasStartedEvent {
private final BitTorrentClient bitTorrentClient;
public SeedSessionHasStarted(final BitTorrentClient bitTorrentClient) {
public SeedSessionHasStartedEvent(final BitTorrentClient bitTorrentClient) {
Preconditions.checkNotNull(bitTorrentClient, "BitTorrentClient cannot be null");
this.bitTorrentClient = bitTorrentClient;
}

View file

@ -3,6 +3,6 @@ package org.araymond.joal.core.events.seedsession;
/**
* Created by raymo on 06/05/2017.
*/
public class SeedSessionWillStart {
public class SeedSessionWillStartEvent {
}

View file

@ -5,9 +5,9 @@ import com.turn.ttorrent.common.Peer;
import com.turn.ttorrent.common.protocol.TrackerMessage.AnnounceRequestMessage.RequestEvent;
import org.araymond.joal.core.client.emulated.BitTorrentClient;
import org.araymond.joal.core.config.JoalConfigProvider;
import org.araymond.joal.core.events.NoMoreLeechers;
import org.araymond.joal.core.events.NoMoreTorrentsFileAvailable;
import org.araymond.joal.core.events.filechange.TorrentFileAddedForSeed;
import org.araymond.joal.core.events.NoMoreLeechersEvent;
import org.araymond.joal.core.events.NoMoreTorrentsFileAvailableEvent;
import org.araymond.joal.core.events.filechange.TorrentFileAddedForSeedEvent;
import org.araymond.joal.core.events.announce.AnnounceRequestingEvent;
import org.araymond.joal.core.exception.NoMoreTorrentsFileAvailableException;
import org.araymond.joal.core.torrent.watcher.TorrentFileChangeAware;
@ -103,13 +103,13 @@ public class Client implements AnnouncerEventListener, TorrentFileChangeAware {
private void handleNoMoreTorrentToSeed(final NoMoreTorrentsFileAvailableException exception) {
if (this.announcers.isEmpty()) {
this.publisher.publishEvent(new NoMoreTorrentsFileAvailable());
this.publisher.publishEvent(new NoMoreTorrentsFileAvailableEvent());
}
}
@Override
public void onTorrentAdded(final MockedTorrent torrent) {
this.publisher.publishEvent(new TorrentFileAddedForSeed(torrent));
this.publisher.publishEvent(new TorrentFileAddedForSeedEvent(torrent));
if (this.currentState != ClientState.STARTED) {
return;
}
@ -136,7 +136,7 @@ public class Client implements AnnouncerEventListener, TorrentFileChangeAware {
@Override
public void onNoMoreLeecherForTorrent(final Announcer announcer, final TorrentWithStats torrent) {
this.torrentFileProvider.moveToArchiveFolder(torrent.getTorrent());
publisher.publishEvent(new NoMoreLeechers(torrent.getTorrent()));
publisher.publishEvent(new NoMoreLeechersEvent(torrent.getTorrent()));
announcer.stop();
}

View file

@ -4,7 +4,7 @@ import com.turn.ttorrent.client.announce.AnnounceException;
import com.turn.ttorrent.common.Peer;
import org.apache.commons.lang3.NotImplementedException;
import org.araymond.joal.core.client.emulated.BitTorrentClient;
import org.araymond.joal.core.events.SomethingHasFuckedUp;
import org.araymond.joal.core.events.SomethingHasFuckedUpEvent;
import org.araymond.joal.core.ttorent.client.MockedTorrent;
import org.araymond.joal.core.ttorent.client.bandwidth.TorrentWithStats;
import org.slf4j.Logger;
@ -148,7 +148,7 @@ public class Announcer implements Runnable, AnnounceResponseListener {
this.thread.setName("bt-announce(" + this.peer.getShortHexPeerId() + ")");
this.thread.start();
this.thread.setUncaughtExceptionHandler((thread, ex) ->
publisher.publishEvent(new SomethingHasFuckedUp(ex))
publisher.publishEvent(new SomethingHasFuckedUpEvent(ex))
);
}
}

View file

@ -35,8 +35,7 @@ public class BandwidthDispatcher implements Runnable {
Preconditions.checkNotNull(configProvider, "Cannot build without ConfigProvider.");
this.configProvider = configProvider;
this.updateInterval = updateInterval;
// TODO : list size = configProvider.getMaxTorrentToSeedSimultaneously
this.torrents = new ArrayList<>();
this.torrents = new ArrayList<>(configProvider.get().getSimultaneousSeed());
lock = new ReentrantReadWriteLock();
rand = new Random();
}

View file

@ -0,0 +1,29 @@
package org.araymond.joal.core.events;
import org.araymond.joal.core.ttorent.client.MockedTorrent;
import org.junit.Test;
import org.mockito.Mockito;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Created by raymo on 25/05/2017.
*/
public class NoMoreLeechersEventTest {
@Test
public void shouldNotBuildWithoutTorrent() {
assertThatThrownBy(() -> new NoMoreLeechersEvent(null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("MockedTorrent cannot be null");
}
@Test
public void shouldBuild() {
final MockedTorrent torrent = Mockito.mock(MockedTorrent.class);
final NoMoreLeechersEvent event = new NoMoreLeechersEvent(torrent);
assertThat(event.getTorrent()).isEqualTo(torrent);
}
}

View file

@ -0,0 +1,17 @@
package org.araymond.joal.core.events;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Created by raymo on 25/05/2017.
*/
public class NoMoreTorrentsFileAvailableEventTest {
@Test
public void shouldBuild() {
new NoMoreTorrentsFileAvailableEvent();
}
}

View file

@ -0,0 +1,28 @@
package org.araymond.joal.core.events;
import org.junit.Test;
import org.mockito.Mockito;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Created by raymo on 25/05/2017.
*/
public class SomethingHasFuckedUpEventTest {
@Test
public void shouldNotBuildWithoutTorrent() {
assertThatThrownBy(() -> new SomethingHasFuckedUpEvent(null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("Exception cannot be null");
}
@Test
public void shouldBuild() {
final IllegalArgumentException ex = new IllegalArgumentException("test");
final SomethingHasFuckedUpEvent event = new SomethingHasFuckedUpEvent(ex);
assertThat(event.getException()).isEqualTo(ex);
}
}

View file

@ -0,0 +1,40 @@
package org.araymond.joal.core.events.announce;
import com.turn.ttorrent.common.protocol.TrackerMessage;
import com.turn.ttorrent.common.protocol.TrackerMessage.AnnounceRequestMessage.RequestEvent;
import org.araymond.joal.core.ttorent.client.bandwidth.TorrentWithStats;
import org.junit.Test;
import org.mockito.Mockito;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Created by raymo on 25/05/2017.
*/
public class AnnounceRequestingEventTest {
@Test
public void shouldNotBuildWithoutRequestEvent() {
assertThatThrownBy(() -> new AnnounceRequestingEvent(null, Mockito.mock(TorrentWithStats.class)))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("RequestEvent cannot be null");
}
@Test
public void shouldNotBuildWithoutTorrent() {
assertThatThrownBy(() -> new AnnounceRequestingEvent(RequestEvent.COMPLETED, null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("TorrentWithStats cannot be null");
}
@Test
public void shouldBuild() {
final TorrentWithStats torrent = Mockito.mock(TorrentWithStats.class);
final AnnounceRequestingEvent event = new AnnounceRequestingEvent(RequestEvent.COMPLETED, torrent);
assertThat(event.getEvent()).isEqualTo(RequestEvent.COMPLETED);
assertThat(event.getTorrent()).isEqualTo(torrent);
}
}

View file

@ -0,0 +1,29 @@
package org.araymond.joal.core.events.filechange;
import org.araymond.joal.core.ttorent.client.MockedTorrent;
import org.junit.Test;
import org.mockito.Mockito;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Created by raymo on 25/05/2017.
*/
public class TorrentFileAddedForSeedEventTest {
@Test
public void shouldNotBuildWithoutTorrent() {
assertThatThrownBy(() -> new TorrentFileAddedForSeedEvent(null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("MockedTorrent cannot be null");
}
@Test
public void shouldBuild() {
final MockedTorrent torrent = Mockito.mock(MockedTorrent.class);
final TorrentFileAddedForSeedEvent event = new TorrentFileAddedForSeedEvent(torrent);
assertThat(event.getTorrent()).isEqualTo(torrent);
}
}

View file

@ -0,0 +1,17 @@
package org.araymond.joal.core.events.seedsession;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Created by raymo on 25/05/2017.
*/
public class SeedSessionHasEndedEventTest {
@Test
public void shouldBuild() {
new SeedSessionHasEndedEvent();
}
}

View file

@ -0,0 +1,29 @@
package org.araymond.joal.core.events.seedsession;
import org.araymond.joal.core.client.emulated.BitTorrentClient;
import org.junit.Test;
import org.mockito.Mockito;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
/**
* Created by raymo on 25/05/2017.
*/
public class SeedSessionHasStartedEventTest {
@Test
public void shouldNotBuildWithoutTorrent() {
assertThatThrownBy(() -> new SeedSessionHasStartedEvent(null))
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("BitTorrentClient cannot be null");
}
@Test
public void shouldBuild() {
final BitTorrentClient client = Mockito.mock(BitTorrentClient.class);
final SeedSessionHasStartedEvent event = new SeedSessionHasStartedEvent(client);
assertThat(event.getBitTorrentClient()).isEqualTo(client);
}
}

View file

@ -0,0 +1,17 @@
package org.araymond.joal.core.events.seedsession;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Created by raymo on 25/05/2017.
*/
public class SeedSessionWillStartEventTest {
@Test
public void shouldBuild() {
new SeedSessionWillStartEvent();
}
}

View file

@ -184,7 +184,8 @@ public class TorrentFileProviderTest {
final TorrentFileProvider provider = new TorrentFileProvider(resourcePath.toString());
final CountDownLatch createLock = new CountDownLatch(1);
final CountDownLatch deleteLock = new CountDownLatch(1);
provider.registerListener(new CountDownLatchListener(createLock, deleteLock));
final TorrentFileChangeAware listener = new CountDownLatchListener(createLock, deleteLock);
provider.registerListener(listener);
provider.postConstruct();
provider.onFileCreate(torrentFile.toFile());
@ -192,6 +193,7 @@ public class TorrentFileProviderTest {
assertThat(createLock.getCount()).isEqualTo(0);
assertThat(deleteLock.getCount()).isEqualTo(1);
provider.preDestroy();
provider.unRegisterListener(listener);
}
@Test
@ -201,9 +203,11 @@ public class TorrentFileProviderTest {
final TorrentFileProvider provider = new TorrentFileProvider(resourcePath.toString());
final CountDownLatch createLock = new CountDownLatch(1);
final CountDownLatch deleteLock = new CountDownLatch(1);
provider.registerListener(new CountDownLatchListener(createLock, deleteLock));
final TorrentFileChangeAware listener = new CountDownLatchListener(createLock, deleteLock);
provider.registerListener(listener);
provider.onFileCreate(torrentFile.toFile());
provider.unRegisterListener(listener);
assertThat(createLock.getCount()).isEqualTo(1);
assertThat(deleteLock.getCount()).isEqualTo(1);
@ -218,9 +222,11 @@ public class TorrentFileProviderTest {
final CountDownLatch createLock = new CountDownLatch(1);
final CountDownLatch deleteLock = new CountDownLatch(1);
provider.registerListener(new CountDownLatchListener(createLock, deleteLock));
final TorrentFileChangeAware listener = new CountDownLatchListener(createLock, deleteLock);
provider.registerListener(listener);
provider.onFileDelete(torrentFile.toFile());
provider.unRegisterListener(listener);
assertThat(createLock.getCount()).isEqualTo(1);
assertThat(deleteLock.getCount()).isEqualTo(0);
@ -233,7 +239,8 @@ public class TorrentFileProviderTest {
final TorrentFileProvider provider = new TorrentFileProvider(resourcePath.toString());
final CountDownLatch createLock = new CountDownLatch(1);
final CountDownLatch deleteLock = new CountDownLatch(1);
provider.registerListener(new CountDownLatchListener(createLock, deleteLock));
final TorrentFileChangeAware listener = new CountDownLatchListener(createLock, deleteLock);
provider.registerListener(listener);
provider.postConstruct();
provider.onFileChange(torrentFile.toFile());
@ -241,6 +248,29 @@ public class TorrentFileProviderTest {
assertThat(createLock.getCount()).isEqualTo(0);
assertThat(deleteLock.getCount()).isEqualTo(0);
provider.preDestroy();
provider.unRegisterListener(listener);
}
@Test
public void shouldUnRegisterListener() throws IOException {
final Path torrentFile = TorrentFileCreator.create(torrentsPath.resolve("ubuntu.torrent"), TorrentFileCreator.TorrentType.UBUNTU);
final Path torrentFile2 = TorrentFileCreator.create(torrentsPath.resolve("audio.torrent"), TorrentFileCreator.TorrentType.AUDIO);
final TorrentFileProvider provider = new TorrentFileProvider(resourcePath.toString());
final CountDownLatch createLock = new CountDownLatch(2);
final CountDownLatch deleteLock = new CountDownLatch(2);
final TorrentFileChangeAware listener = new CountDownLatchListener(createLock, deleteLock);
provider.registerListener(listener);
provider.postConstruct();
provider.onFileCreate(torrentFile.toFile());
provider.unRegisterListener(listener);
provider.onFileCreate(torrentFile2.toFile());
assertThat(createLock.getCount()).isEqualTo(1);
assertThat(deleteLock.getCount()).isEqualTo(2);
provider.preDestroy();
provider.unRegisterListener(listener);
}

View file

@ -19,6 +19,7 @@ public class MockedTorrentTest {
final MockedTorrent torrent2 = MockedTorrent.fromFile(TorrentFileCreator.getTorrentPath(TorrentFileCreator.TorrentType.UBUNTU).toFile());
assertThat(torrent).isEqualTo(torrent2);
assertThat(torrent.hashCode()).isEqualTo(torrent2.hashCode());
}
@Test
@ -27,6 +28,7 @@ public class MockedTorrentTest {
final MockedTorrent torrent2 = MockedTorrent.fromFile(TorrentFileCreator.getTorrentPath(TorrentFileCreator.TorrentType.AUDIO).toFile());
assertThat(torrent).isNotEqualTo(torrent2);
assertThat(torrent.hashCode()).isNotEqualTo(torrent2.hashCode());
}
}

View file

@ -23,11 +23,14 @@ public class BandwidthDispatcherTest {
@Test
public void shouldBuild() {
final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class);
final AppConfiguration conf = Mockito.mock(AppConfiguration.class);
Mockito.when(configProvider.get()).thenReturn(conf);
Mockito.when(conf.getSimultaneousSeed()).thenReturn(3);
try {
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(configProvider);
} catch (final Throwable ignored) {
fail("should build");
} catch (final Throwable throwable) {
fail("should build", throwable);
}
}
@ -36,6 +39,7 @@ public class BandwidthDispatcherTest {
final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class);
final AppConfiguration conf = Mockito.mock(AppConfiguration.class);
Mockito.when(configProvider.get()).thenReturn(conf);
Mockito.when(conf.getSimultaneousSeed()).thenReturn(3);
Mockito.when(conf.getMinUploadRate()).thenReturn(180);
Mockito.when(conf.getMaxUploadRate()).thenReturn(190);
@ -55,6 +59,7 @@ public class BandwidthDispatcherTest {
final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class);
final AppConfiguration conf = Mockito.mock(AppConfiguration.class);
Mockito.when(configProvider.get()).thenReturn(conf);
Mockito.when(conf.getSimultaneousSeed()).thenReturn(3);
Mockito.when(conf.getMinUploadRate()).thenReturn(0);
Mockito.when(conf.getMaxUploadRate()).thenReturn(1);
@ -74,6 +79,7 @@ public class BandwidthDispatcherTest {
final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class);
final AppConfiguration conf = Mockito.mock(AppConfiguration.class);
Mockito.when(configProvider.get()).thenReturn(conf);
Mockito.when(conf.getSimultaneousSeed()).thenReturn(3);
Mockito.when(conf.getMinUploadRate()).thenReturn(0);
Mockito.when(conf.getMaxUploadRate()).thenReturn(0);
@ -89,6 +95,7 @@ public class BandwidthDispatcherTest {
final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class);
final AppConfiguration conf = Mockito.mock(AppConfiguration.class);
Mockito.when(configProvider.get()).thenReturn(conf);
Mockito.when(conf.getSimultaneousSeed()).thenReturn(3);
Mockito.when(conf.getMinUploadRate()).thenReturn(180);
Mockito.when(conf.getMaxUploadRate()).thenReturn(190);
@ -108,12 +115,12 @@ public class BandwidthDispatcherTest {
);
}
@Test
public void shouldIncrementTorrentStatAndSplitSpeed() throws InterruptedException {
final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class);
final AppConfiguration conf = Mockito.mock(AppConfiguration.class);
Mockito.when(configProvider.get()).thenReturn(conf);
Mockito.when(conf.getSimultaneousSeed()).thenReturn(3);
Mockito.when(conf.getMinUploadRate()).thenReturn(160);
Mockito.when(conf.getMaxUploadRate()).thenReturn(180);
@ -154,4 +161,38 @@ public class BandwidthDispatcherTest {
);
}
@Test
public void shouldNotIncrementUnregisteredTorrent() throws InterruptedException {
final JoalConfigProvider configProvider = Mockito.mock(JoalConfigProvider.class);
final AppConfiguration conf = Mockito.mock(AppConfiguration.class);
Mockito.when(configProvider.get()).thenReturn(conf);
Mockito.when(conf.getSimultaneousSeed()).thenReturn(3);
Mockito.when(conf.getMinUploadRate()).thenReturn(180);
Mockito.when(conf.getMaxUploadRate()).thenReturn(190);
final int updateInterval = 4;
final BandwidthDispatcher bandwidthDispatcher = new BandwidthDispatcher(configProvider, updateInterval);
final TorrentWithStats torrent = new TorrentWithStats(Mockito.mock(MockedTorrent.class));
bandwidthDispatcher.registerTorrent(torrent);
bandwidthDispatcher.start();
Thread.sleep(5);
assertThat(torrent.getUploaded())
.isBetween(
(long) conf.getMinUploadRate() * 1024 / (1000 / updateInterval),
(long) conf.getMaxUploadRate() * 1024 / (1000 / updateInterval)
);
bandwidthDispatcher.unRegisterTorrent(torrent);
Thread.sleep(5);
bandwidthDispatcher.stop();
assertThat(torrent.getUploaded())
.isBetween(
(long) conf.getMinUploadRate() * 1024 / (1000 / updateInterval),
(long) conf.getMaxUploadRate() * 1024 / (1000 / updateInterval)
);
}
}

View file

@ -49,6 +49,7 @@ public class TorrentWithStatsTest {
final TorrentWithStats torrentWithStats = new TorrentWithStats(torrent);
final TorrentWithStats torrentWithStats2 = new TorrentWithStats(torrent);
assertThat(torrentWithStats).isEqualTo(torrentWithStats2);
assertThat(torrentWithStats.hashCode()).isEqualTo(torrentWithStats2.hashCode());
}
@Test
@ -59,6 +60,8 @@ public class TorrentWithStatsTest {
final TorrentWithStats torrentWithStats = new TorrentWithStats(torrent);
final TorrentWithStats torrentWithStats2 = new TorrentWithStats(torrent2);
assertThat(torrentWithStats).isNotEqualTo(torrentWithStats2);
assertThat(torrentWithStats.hashCode()).isNotEqualTo(torrentWithStats2.hashCode());
}
}