Search in sources :

Example 1 with WaitFor

use of jetbrains.buildServer.util.WaitFor in project teamcity-torrent-plugin by JetBrains.

the class TorrentTrackerConfiguratorTest method test_torrent_expire_timeout.

public void test_torrent_expire_timeout() throws IOException, TrackerMessage.MessageValidationException, InterruptedException {
    System.setProperty(TorrentConfiguration.TRACKER_TORRENT_EXPIRE_TIMEOUT, "5");
    myConfigurator.getConfigurationWatcher().checkForModifications();
    assertEquals(5, myConfigurator.getTrackerTorrentExpireTimeoutSec());
    final String uriCompleted = "http://localhost:8111/trackerAnnounce.html" + "?info_hash=12345678901234567890" + "&peer_id=ABCDEFGHIJKLMNOPQRST" + "&ip=172.20.240.249" + "&port=6884" + "&downloaded=1234" + "&left=0" + "&event=" + "completed";
    final String uriCompleted2 = "http://localhost:8111/trackerAnnounce.html" + "?info_hash=12345678901234567890" + "&peer_id=BBCDEFGHIJKLMNOPQRST" + "&ip=172.20.240.249" + "&port=6881" + "&downloaded=1234" + "&left=0" + "&event=" + "completed";
    // assertEquals(uriCompleted, uriCompleted2);
    final AtomicReference<byte[]> response = new AtomicReference<byte[]>();
    final TrackerRequestProcessor.RequestHandler requestHandler = new TrackerRequestProcessor.RequestHandler() {

        public void serveResponse(int code, String description, ByteBuffer responseData) {
            response.set(responseData.array());
        }
    };
    myTrackerManager.getTrackerService().process(uriCompleted, "http://localhost:8111/", requestHandler);
    assertEquals(1, myTrackerManager.getTorrents().size());
    final AtomicInteger complete = new AtomicInteger(100);
    final AtomicInteger peersSize = new AtomicInteger(100);
    final String torrentHash = "3132333435363738393031323334353637383930";
    new WaitFor(15 * 1000) {

        @Override
        protected boolean condition() {
            try {
                myTrackerManager.getTrackerService().process(uriCompleted2, "http://localhost:8111/", requestHandler);
                final HTTPAnnounceResponseMessage parse = (HTTPAnnounceResponseMessage) HTTPTrackerMessage.parse(new ByteArrayInputStream(response.get()));
                complete.set(parse.getComplete());
                peersSize.set(parse.getPeers().size());
                final TrackedTorrent trackedTorrent = myTrackerManager.getTorrents().get(torrentHash);
                return parse.getComplete() == 1 && trackedTorrent.getPeers().size() == 1;
            } catch (IOException e) {
                e.printStackTrace();
            } catch (TrackerMessage.MessageValidationException e) {
                e.printStackTrace();
            }
            return false;
        }
    };
    final TrackedTorrent trackedTorrent = myTrackerManager.getTorrents().get(torrentHash);
    for (PeerUID peerUID : trackedTorrent.getPeers().keySet()) {
        if (peerUID.getTorrentHash().equals("4142434445464748494A4B4C4D4E4F5051525354"))
            fail();
    }
    new WaitFor(15 * 1000) {

        @Override
        protected boolean condition() {
            return !myTrackerManager.getTorrents().containsKey(torrentHash);
        }
    };
    assertNotContains(myTrackerManager.getTorrents().keySet(), torrentHash);
}
Also used : HTTPAnnounceResponseMessage(com.turn.ttorrent.common.protocol.http.HTTPAnnounceResponseMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) HTTPTrackerMessage(com.turn.ttorrent.common.protocol.http.HTTPTrackerMessage) TrackerMessage(com.turn.ttorrent.common.protocol.TrackerMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WaitFor(jetbrains.buildServer.util.WaitFor) ByteArrayInputStream(java.io.ByteArrayInputStream) TrackerRequestProcessor(com.turn.ttorrent.tracker.TrackerRequestProcessor) TrackedTorrent(com.turn.ttorrent.tracker.TrackedTorrent) PeerUID(com.turn.ttorrent.common.PeerUID)

Example 2 with WaitFor

use of jetbrains.buildServer.util.WaitFor in project teamcity-torrent-plugin by JetBrains.

the class TorrentTrackerConfiguratorTest method setUp.

@BeforeMethod
@Override
protected void setUp() throws Exception {
    super.setUp();
    Mockery m = new Mockery();
    final AddressChecker addressChecker = m.mock(AddressChecker.class);
    m.checking(new Expectations() {

        {
            allowing(addressChecker).isBadAddress(with(any(String.class)));
            will(returnValue(false));
        }
    });
    myTrackerManager = new TorrentTrackerManager(myConfigurator, new ExecutorServices() {

        @NotNull
        public ScheduledExecutorService getNormalExecutorService() {
            return ExecutorsFactory.newFixedScheduledExecutor("aaa", 1);
        }

        @NotNull
        public ExecutorService getLowPriorityExecutorService() {
            return null;
        }
    }, myDispatcher, addressChecker);
    myDispatcher.getMulticaster().serverStartup();
    new WaitFor(5 * 1000) {

        @Override
        protected boolean condition() {
            return !myTorrentsSeeder.getTorrentsSeeder().isStopped();
        }
    };
    assertFalse(myTorrentsSeeder.getTorrentsSeeder().isStopped());
}
Also used : Expectations(org.jmock.Expectations) AddressChecker(com.turn.ttorrent.tracker.AddressChecker) WaitFor(jetbrains.buildServer.util.WaitFor) ExecutorServices(jetbrains.buildServer.serverSide.executors.ExecutorServices) Mockery(org.jmock.Mockery) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with WaitFor

use of jetbrains.buildServer.util.WaitFor in project teamcity-torrent-plugin by JetBrains.

the class AgentTorrentsManagerTest method testAnnounceAllOnAgentStarted.

public void testAnnounceAllOnAgentStarted() throws IOException, URISyntaxException, InterruptedException, NoSuchAlgorithmException {
    Tracker tracker = new Tracker(6969);
    try {
        final List<String> torrentHashes = new ArrayList<String>();
        final List<File> createdFiles = new ArrayList<File>();
        final int torrentsCount = 10;
        tracker.start(true);
        for (int i = 0; i < torrentsCount; i++) {
            final File artifactFile = createTempFile(65535);
            createdFiles.add(artifactFile);
            File torrentDir = createTempDir();
            final Torrent torrent = TorrentCreator.create(artifactFile, tracker.getAnnounceURI(), "tc-plugin-test");
            final File torrentFile = new File(torrentDir, artifactFile.getName() + ".torrent");
            torrentHashes.add(torrent.getHexInfoHash());
            TorrentUtil.saveTorrentToFile(torrent, torrentFile);
            myTorrentsManager.getTorrentsSeeder().registerSrcAndTorrentFile(artifactFile, torrentFile, false);
        }
        Mock buildAgentMock = mock(BuildAgent.class);
        myTorrentsManager.agentStarted((BuildAgent) buildAgentMock.proxy());
        new WaitFor(3 * 1000) {

            @Override
            protected boolean condition() {
                return myTorrentsManager.getTorrentsSeeder().getNumberOfSeededTorrents() == torrentsCount;
            }
        };
        List<String> seededHashes = new ArrayList<String>();
        List<File> seededFiles = new ArrayList<File>();
        for (AnnounceableTorrent st : myTorrentsManager.getTorrentsSeeder().getClient().getAnnounceableTorrents()) {
            seededHashes.add(st.getHexInfoHash());
            AnnounceableFileTorrent torrent = myTorrentsManager.getTorrentsSeeder().getClient().getAnnounceableFileTorrent(st.getHexInfoHash());
            Torrent metadata = Torrent.load(new File(torrent.getDotTorrentFilePath()));
            seededFiles.add(new File(torrent.getDownloadDirPath(), metadata.getName()));
        }
        assertSameElements(torrentHashes, seededHashes);
        assertSameElements(createdFiles, seededFiles);
    } finally {
        myTorrentsManager.agentShutdown();
        tracker.stop();
    }
}
Also used : Tracker(com.turn.ttorrent.tracker.Tracker) Torrent(com.turn.ttorrent.common.Torrent) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) AnnounceableFileTorrent(com.turn.ttorrent.common.AnnounceableFileTorrent) WaitFor(jetbrains.buildServer.util.WaitFor) ArrayList(java.util.ArrayList) File(java.io.File) AnnounceableTorrent(com.turn.ttorrent.common.AnnounceableTorrent) Mock(org.jmock.Mock)

Aggregations

WaitFor (jetbrains.buildServer.util.WaitFor)3 AnnounceableFileTorrent (com.turn.ttorrent.common.AnnounceableFileTorrent)1 AnnounceableTorrent (com.turn.ttorrent.common.AnnounceableTorrent)1 PeerUID (com.turn.ttorrent.common.PeerUID)1 Torrent (com.turn.ttorrent.common.Torrent)1 TrackerMessage (com.turn.ttorrent.common.protocol.TrackerMessage)1 HTTPAnnounceResponseMessage (com.turn.ttorrent.common.protocol.http.HTTPAnnounceResponseMessage)1 HTTPTrackerMessage (com.turn.ttorrent.common.protocol.http.HTTPTrackerMessage)1 AddressChecker (com.turn.ttorrent.tracker.AddressChecker)1 TrackedTorrent (com.turn.ttorrent.tracker.TrackedTorrent)1 Tracker (com.turn.ttorrent.tracker.Tracker)1 TrackerRequestProcessor (com.turn.ttorrent.tracker.TrackerRequestProcessor)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ExecutorServices (jetbrains.buildServer.serverSide.executors.ExecutorServices)1