コード例 #1
0
 public void GetChunk_OffsetOutOfRange_EmptyByteArray()
 {
     using (var e = new PushStorageEnvForTest())
     {
         string text         = "sample";
         var    bundleHelper = new PushStorageManager(e.PullDataFolderPath, "randomHash");
         File.WriteAllText(bundleHelper.BundlePath, text);
         byte[] chunk = bundleHelper.GetChunk(10, 5);                 // offset is greater than string length
         Assert.That(chunk.Length, Is.EqualTo(0));
     }
 }
コード例 #2
0
 public void GetChunk_LengthTooLarge_ReturnsAdjustedByteArray()
 {
     using (var e = new PushStorageEnvForTest())
     {
         string text         = "sample";
         var    bundleHelper = new PushStorageManager(e.PullDataFolderPath, "randomHash");
         File.WriteAllText(bundleHelper.BundlePath, text);
         byte[] chunk = bundleHelper.GetChunk(3, 10);                 // offset is greater than string length
         Assert.That(Encoding.UTF8.GetString(chunk), Is.EqualTo("ple"));
     }
 }
コード例 #3
0
 public void GetChunk_MiddleBytes_Ok()
 {
     using (var e = new PushStorageEnvForTest())
     {
         string text         = "This is some sample text to be used by the push bundle helper tests";
         var    bundleHelper = new PushStorageManager(e.PullDataFolderPath, "randomHash");
         File.WriteAllText(bundleHelper.BundlePath, text);
         byte[] chunk = bundleHelper.GetChunk(10, 5);
         Assert.That(Encoding.UTF8.GetString(chunk), Is.EqualTo("me sa"));
     }
 }
コード例 #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));
        }