Example #1
0
 public void Push_InitialServerResponseServerNotAvailable_NotAvailableMessage()
 {
     using (var e = new TestEnvironment("hgresumetest", ApiServerType.Dummy))
         using (var provider = GetTransportProviderForTest(e))
         {
             e.LocalAddAndCommit();
             var serverMessage = "The server is down for scheduled maintenance";
             e.ApiServer.AddResponse(ApiResponses.NotAvailable(serverMessage));
             e.ApiServer.AddResponse(ApiResponses.PushComplete());
             var transport = provider.Transport;
             Assert.Throws <HgResumeOperationFailed>(transport.Push);
             Assert.That(e.Progress.AllMessages, Contains.Item("Server temporarily unavailable: " + serverMessage));
             Assert.That(e.Progress.AllMessages, Has.No.Member("The pull operation completed successfully"));
         }
 }
 public void Push_InitialServerResponseServerNotAvailable_NotAvailableMessage()
 {
     using (var e = new TestEnvironment("hgresumetest", ApiServerType.Dummy))
         using (var provider = GetTransportProviderForTest(e))
         {
             e.LocalAddAndCommit();
             var serverMessage    = "The server is down for scheduled maintenance";
             var revisionResponse = ApiResponses.Custom(HttpStatusCode.OK);
             revisionResponse.Content = Encoding.UTF8.GetBytes("0:default").ToArray();
             e.ApiServer.AddResponse(revisionResponse);
             e.ApiServer.AddResponse(ApiResponses.NotAvailable(serverMessage));
             e.ApiServer.AddResponse(ApiResponses.PushComplete());
             var transport = provider.Transport;
             transport.Push();
             Assert.That(e.Progress.AllMessages, Contains.Item("Server temporarily unavailable: " + serverMessage));
             Assert.That(e.Progress.AllMessages, Has.No.Member("The pull operation completed successfully"));
         }
 }
 public void Pull_GetRevisionServerResponseServerNotAvailable_NotAvailableMessageAndFails()
 {
     using (var e = new TestEnvironment("hgresumetest", ApiServerType.Dummy))
         using (var provider = GetTransportProviderForTest(e))
         {
             e.LocalAddAndCommit();
             var serverMessage = "The server is down for scheduled maintenance";
             e.ApiServer.AddResponse(ApiResponses.NotAvailable(serverMessage));
             e.ApiServer.AddResponse(ApiResponses.NotAvailable(serverMessage));
             var revisionResponse = ApiResponses.Custom(HttpStatusCode.OK);
             revisionResponse.Content = Encoding.UTF8.GetBytes((e.Local.Repository.GetTip().Number.Hash + ":").ToArray());
             e.ApiServer.AddResponse(revisionResponse);
             e.ApiServer.AddResponse(ApiResponses.PullNoChange());
             var transport = provider.Transport;
             Assert.Throws(typeof(HgResumeOperationFailed), () => transport.Pull());
             Assert.That(e.Progress.AllMessages, Contains.Item("Server temporarily unavailable: " + serverMessage));
             Assert.That(e.Progress.AllMessages, Has.No.Member("No changes"));
         }
 }
Example #4
0
        public HgResumeApiResponse Execute(string method, HgResumeApiParameters request, byte[] bytesToWrite, int secondsBeforeTimeout)
        {
            ValidateParameters(method, request, bytesToWrite, secondsBeforeTimeout);
            _executeCount++;
            if (method == "finishPullBundle")
            {
                if (CurrentTip != OriginalTip)
                {
                    PrepareBundle(HgResumeTransport.GetHashStringsFromRevisions(_repo.BranchingHelper.GetBranches()));
                    return(ApiResponses.Reset());                    // repo changed in between pulls
                }
                return(ApiResponses.Custom(HttpStatusCode.OK));
            }
            if (method == "getRevisions")
            {
                IEnumerable <string> revisions = _repo.GetAllRevisions().Select(rev => rev.Number.Hash + ':' + rev.Branch);
                return(ApiResponses.Revisions(string.Join("|", revisions.ToArray())));
            }
            if (method == "pullBundleChunk")
            {
                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
                               ));
                }
                if (Array.BinarySearch(request.BaseHashes, 0, request.BaseHashes.Length, CurrentTip) >= 0)
                {
                    return(ApiResponses.PullNoChange());
                }

                var bundleFileInfo = new FileInfo(_helper.BundlePath);
                if (bundleFileInfo.Exists && bundleFileInfo.Length == 0 || !bundleFileInfo.Exists)
                {
                    PrepareBundle(request.BaseHashes);
                }
                //int offset = Convert.ToInt32(request["offset"]);
                //int chunkSize = Convert.ToInt32(request["chunkSize"]);
                var bundleFile = new FileInfo(_helper.BundlePath);
                if (request.StartOfWindow >= bundleFile.Length)
                {
                    return(ApiResponses.Failed("offset greater than bundleSize"));
                }
                var chunk = _helper.GetChunk(request.StartOfWindow, request.ChunkSize);
                return(ApiResponses.PullOk(Convert.ToInt32(bundleFile.Length), chunk));
            }
            return(ApiResponses.Custom(HttpStatusCode.InternalServerError));
        }
Example #5
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));
        }