public void Push_MultiChunkBundleAndUnBundleFails_Fail()
 {
     using (var e = new TestEnvironment("hgresumetest", ApiServerType.Dummy))
         using (var provider = GetTransportProviderForTest(e))
         {
             e.LocalAddAndCommit();
             e.ApiServer.AddResponse(ApiResponses.Revisions(e.Local.Repository.GetTip().Number.Hash + ':' + e.Local.Repository.GetTip().Branch));
             e.LocalAddAndCommit();
             e.ApiServer.AddResponse(ApiResponses.PushAccepted(1));
             e.ApiServer.AddResponse(ApiResponses.PushAccepted(2));
             e.ApiServer.AddResponse(ApiResponses.Reset());
             var transport = provider.Transport;
             Assert.That(() => transport.Push(), Throws.Exception.TypeOf <HgResumeOperationFailed>());
         }
 }
        public void Push_2DifferentApiServers_HgRepoFileUpdatedWithBothEntries()
        {
            using (var e1 = new TestEnvironment("api1", ApiServerType.Dummy))
                using (var provider = GetTransportProviderForTest(e1))
                {
                    var api2       = new DummyApiServerForTest("api3");
                    var transport2 = new HgResumeTransport(e1.Local.Repository, "api3", api2, e1.MultiProgress);

                    // push to ApiServer 1
                    e1.LocalAddAndCommit();
                    var revisionResponse = ApiResponses.Revisions(e1.Local.Repository.GetTip().Number.Hash + ':' + e1.Local.Repository.GetTip().Branch);
                    e1.ApiServer.AddResponse(revisionResponse);
                    e1.LocalAddAndCommit();
                    e1.ApiServer.AddResponse(ApiResponses.PushComplete());
                    var tipHash1  = e1.Local.Repository.GetTip().Number.Hash;
                    var transport = provider.Transport;
                    transport.Push();
                    e1.ApiServer.AddResponse(ApiResponses.PushComplete());              // finishPushBundle

                    // push to ApiServer 2
                    api2.AddResponse(revisionResponse);
                    api2.AddResponse(ApiResponses.PushComplete());
                    transport2.Push();

                    // check contents of remoteRepoDb
                    string dbStoragePath = transport.PathToLocalStorage;
                    string dbFilePath    = Path.Combine(dbStoragePath, HgResumeTransport.RevisionCacheFilename);
                    var    cacheContents = HgResumeTransport.ReadServerRevisionCache(dbFilePath);
                    Assert.True(cacheContents.Count == 2, "should be two api server entries in the cache.");
                    Assert.True(cacheContents.SingleOrDefault(x => x.RemoteId == e1.ApiServer.Host).Revision.Number.Hash == tipHash1, "Cache contents incorrect");
                    Assert.True(cacheContents.SingleOrDefault(x => x.RemoteId == api2.Host).Revision.Number.Hash == tipHash1, "Cache contents incorrect");

                    // second push
                    e1.LocalAddAndCommit();
                    e1.LocalAddAndCommit();
                    string tipHash2 = e1.Local.Repository.GetTip().Number.Hash;
                    e1.ApiServer.AddResponse(ApiResponses.PushAccepted(1));
                    e1.ApiServer.AddResponse(ApiResponses.PushComplete());
                    transport.Push();

                    cacheContents = HgResumeTransport.ReadServerRevisionCache(dbFilePath);
                    Assert.True(cacheContents.Count == 2, "should be two api server entries in the cache.");
                    Assert.True(cacheContents.SingleOrDefault(x => x.RemoteId == e1.ApiServer.Host).Revision.Number.Hash == tipHash2, "Cache contents incorrect");
                    Assert.True(cacheContents.SingleOrDefault(x => x.RemoteId == api2.Host).Revision.Number.Hash == tipHash1, "Cache contents incorrect");

                    Assert.That(e1.Progress.AllMessages, Contains.Item("Finished sending"));
                }
        }
        public void Push_2PushesAndRemoteRepoDbFileUpdated_Success()
        {
            using (var e = new TestEnvironment("hgresumetest", ApiServerType.Dummy))
                using (var provider = GetTransportProviderForTest(e))
                {
                    e.LocalAddAndCommit();
                    e.ApiServer.AddResponse(ApiResponses.Revisions(e.Local.Repository.GetTip().Number.Hash + ':' + e.Local.Repository.GetTip().Branch));
                    e.LocalAddAndCommit();
                    e.ApiServer.AddResponse(ApiResponses.PushComplete());
                    var tipHash   = e.Local.Repository.GetTip().Number.Hash;
                    var transport = provider.Transport;
                    transport.Push();

                    string dbStoragePath = transport.PathToLocalStorage;
                    string dbFilePath    = Path.Combine(dbStoragePath, HgResumeTransport.RevisionCacheFilename);

                    var cacheContents = HgResumeTransport.ReadServerRevisionCache(dbFilePath);
                    Assert.True(cacheContents.Count == 1, "should only be one entry in the cache.");
                    Assert.True(cacheContents.FirstOrDefault().RemoteId == e.ApiServer.Host &&
                                cacheContents.FirstOrDefault().Revision.Number.Hash == tipHash, "Cache contents incorrect");

                    Assert.That(e.Progress.AllMessages, Contains.Item("Finished sending"));

                    e.LocalAddAndCommit();
                    e.LocalAddAndCommit();
                    var tipHash2 = e.Local.Repository.GetTip().Number.Hash;
                    e.ApiServer.AddResponse(ApiResponses.PushAccepted(1));
                    e.ApiServer.AddResponse(ApiResponses.PushComplete());
                    transport.Push();
                    cacheContents = HgResumeTransport.ReadServerRevisionCache(dbFilePath);
                    Assert.True(cacheContents.Count == 1, "should only be one entry in the cache.");
                    Assert.True(cacheContents.FirstOrDefault().RemoteId == e.ApiServer.Host &&
                                cacheContents.FirstOrDefault().Revision.Number.Hash == tipHash2, "Cache contents incorrect");
                    Assert.That(e.Progress.AllMessages, Contains.Item("Finished sending"));
                }
        }
Exemple #4
0
        public HgResumeApiResponse Execute(string method, HgResumeApiParameters request, byte[] bytesToWrite, int secondsBeforeTimeout)
        {
            ValidateParameters(method, request, bytesToWrite, secondsBeforeTimeout);
            if (method == "getRevisions")
            {
                IEnumerable <Revision> revisions = _repo.GetAllRevisions();

                if (revisions.Count() == 0)
                {
                    return(ApiResponses.Revisions("0:default"));
                }
                var revisionStrings = _repo.GetAllRevisions().Select(rev => rev.Number.Hash + ':' + rev.Branch);
                return(ApiResponses.Revisions(string.Join("|", revisionStrings.ToArray())));
            }
            if (method == "finishPushBundle")
            {
                return(ApiResponses.PushComplete());
            }
            if (method == "pushBundleChunk")
            {
                _executeCount++;
                if (_cancelCount == _executeCount)
                {
                    _progress.CancelRequested = true;
                    return(ApiResponses.Failed(""));
                }
                if (_failCount == _executeCount)
                {
                    return(ApiResponses.Failed(""));
                }
                if (_timeoutList.Contains(_executeCount))
                {
                    return(null);
                }
                if (_serverUnavailableList.Any(i => i.ExecuteCount == _executeCount))
                {
                    return(ApiResponses.NotAvailable(
                               _serverUnavailableList.Where(i => i.ExecuteCount == _executeCount).First().Message
                               ));
                }
                _helper = new PullStorageManager(_localStorage.Path, request.TransId);

                //int bundleSize = Convert.ToInt32(parameters["bundleSize"]);
                //int offset = Convert.ToInt32(parameters["offset"]);
                //int chunkSize = bytesToWrite.Length;

                _helper.WriteChunk(request.StartOfWindow, bytesToWrite);

                if (request.StartOfWindow + request.ChunkSize == request.BundleSize)
                {
                    if (_repo.Unbundle(_helper.BundlePath))
                    {
                        return(ApiResponses.PushComplete());
                    }
                    return(ApiResponses.Reset());
                }
                if (request.StartOfWindow + request.ChunkSize < request.BundleSize)
                {
                    return(ApiResponses.PushAccepted(_helper.StartOfWindow));
                }
                return(ApiResponses.Failed("offset + chunkSize > bundleSize !"));
            }
            return(ApiResponses.Custom(HttpStatusCode.InternalServerError));
        }