Example #1
0
        public async Task FuzzConcurrentAccess()
        {
            const int threadCount             = 5;
            const int iterationCountPerThread = 25;

            var writtenValues = new List <int>();
            var random        = new Random();
            var failures      = new List <Exception>();

            using (var container = TemporaryDirectory.CreateNew())
            {
                var path = container.File("key-path");
                await ConcurrentOperations.Run(threadCount, () => {
                    var sut = new AtomicFile <int>(path, new RandomlyFailingSerialiser <int>(new StubSerialiser(), 0.05));
                    for (var i = 0; i < iterationCountPerThread; i++)
                    {
                        try
                        {
                            var value = random.Next(100);
                            sut.Write(value);
                            lock (writtenValues) writtenValues.Add(value);
                        }
                        catch (FileLockedException)
                        {
                            // We expect locking conflicts to represent the bulk of failures.
                        }
                        catch (Exception ex)
                        {
                            lock (failures) failures.Add(ex);
                        }
                        try
                        {
                            sut.Read();
                        }
                        catch (Exception) { }
                    }
                });

                var finalValue = new AtomicFile <int>(path, new StubSerialiser()).Read();

                // At least one write should succeed.
                Assume.That(writtenValues.Count, Is.GreaterThan(1));

                // Due to races it's not guaranteed that the last entry in the list is the last one successfully written.
                // However, this should be vanishingly unlikely because it would require that another Write call ran to
                // completion between `sut.Write(value);` and `lock (writtenValues)`.
                Assert.That(finalValue, Is.EqualTo(writtenValues.Last()));
            }
        }
Example #2
0
 public ProcessArgumentBuilder BuildWindowsArguments(ProcessArgumentBuilder args)
 {
     //var args = new ProcessArgumentBuilder();
     args.Append("/Y");
     if (Pattern.IsDefined())
     {
         args.AppendSwitchQuoted("/Pattern", ":", Pattern);
     }
     if (DestinationKey.IsDefined())
     {
         args.AppendSwitchQuotedSecret("/DestKey", ":", DestinationKey);
     }
     if (DestinationSAS.IsDefined())
     {
         args.AppendSwitchQuotedSecret("/DestSAS", ":", DestinationSAS);
     }
     if (SourceKey.IsDefined())
     {
         args.AppendSwitchQuotedSecret("/SourceKey", ":", SourceKey);
     }
     if (SourceSAS.IsDefined())
     {
         args.AppendSwitchQuotedSecret("/SourceSAS", ":", SourceSAS);
     }
     if (Recursive)
     {
         args.Append("/S");
     }
     if (BlobType != null)
     {
         args.AppendSwitchQuoted("/BlobType", ":", BlobType.ToString());
     }
     if (UseChecksum)
     {
         args.Append("/CheckMD5");
     }
     if (LogFile != null)
     {
         args.AppendSwitchQuoted("/V", ":", LogFile.FullPath);
     }
     if (ParameterFiles.Any())
     {
         foreach (var file in ParameterFiles)
         {
             args.AppendSwitchQuoted("@", ":", file.FullPath);
         }
     }
     if (FileHandlingBehaviour != null)
     {
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ArchiveOnly))
         {
             args.Append("/A");
         }
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ExcludeNewerSource))
         {
             args.Append("/XN");
         }
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ExcludeOlderSource))
         {
             args.Append("/XO");
         }
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.UpdateLastModified))
         {
             args.Append("/MT");
         }
     }
     if (Delimiter != null)
     {
         args.AppendSwitchQuoted("/Delimiter", ":", Delimiter.ToString());
     }
     if (ConcurrentOperations != 0)
     {
         args.AppendSwitchQuoted("/NC", ":", ConcurrentOperations.ToString());
     }
     if (TargetContentType.IsDefined())
     {
         args.AppendSwitchQuoted("/SetContentType", ":", TargetContentType);
     }
     if (PayloadFormat != PayloadFormat.Default)
     {
         args.AppendSwitchQuoted("/PayloadFormat", ":", PayloadFormat.ToString());
     }
     return(args);
 }
Example #3
0
 public ProcessArgumentBuilder BuildLinuxArguments(ProcessArgumentBuilder args)
 {
     args.Append("--quiet"); // equivalent to /Y for some reason
     if (Pattern.IsDefined())
     {
         args.AppendSwitchQuoted("--include", Pattern);
     }
     if (DestinationKey.IsDefined())
     {
         args.AppendSwitchQuotedSecret("--dest-key", DestinationKey);
     }
     if (DestinationSAS.IsDefined())
     {
         args.AppendSwitchQuotedSecret("--dest-sas", DestinationSAS);
     }
     if (SourceKey.IsDefined())
     {
         args.AppendSwitchQuotedSecret("--source-key", SourceKey);
     }
     if (SourceSAS.IsDefined())
     {
         args.AppendSwitchQuotedSecret("--source-sas", SourceSAS);
     }
     if (Recursive)
     {
         args.Append("--recursive");
     }
     if (BlobType != null)
     {
         args.AppendSwitch("--blob-type", BlobType.ToString().ToLower());
     }
     if (UseChecksum)
     {
         args.Append("--check-md5");
     }
     if (ParameterFiles.Any())
     {
         foreach (var file in ParameterFiles)
         {
             args.AppendSwitchQuoted("--config-file", file.FullPath);
         }
     }
     if (FileHandlingBehaviour != null)
     {
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ExcludeNewerSource))
         {
             args.Append("--exclude-newer");
         }
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ExcludeOlderSource))
         {
             args.Append("--exclude-older");
         }
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.UpdateLastModified))
         {
             args.Append("--preserve-last-modified-time");
         }
     }
     if (Delimiter != null)
     {
         args.AppendSwitchQuoted("--delimiter", Delimiter.ToString());
     }
     if (ConcurrentOperations != 0)
     {
         args.AppendSwitchQuoted("--parallel-level", ConcurrentOperations.ToString());
     }
     if (TargetContentType.IsDefined())
     {
         args.AppendSwitchQuoted("--set-content-type", TargetContentType);
     }
     return(args);
 }
Example #4
0
 public ProcessArgumentBuilder BuildWindowsArguments(ProcessArgumentBuilder args)
 {
     //var args = new ProcessArgumentBuilder();
     args.Append("/Y");
     if (Pattern.IsDefined())
     {
         args.AppendSwitchQuoted("/Pattern", ":", Pattern);
     }
     if (DestinationKey.IsDefined())
     {
         args.AppendSwitchQuotedSecret("/DestKey", ":", DestinationKey);
     }
     if (DestinationSAS.IsDefined())
     {
         args.AppendSwitchQuotedSecret("/DestSAS", ":", DestinationSAS);
     }
     if (SourceKey.IsDefined())
     {
         args.AppendSwitchQuotedSecret("/SourceKey", ":", SourceKey);
     }
     if (SourceSAS.IsDefined())
     {
         args.AppendSwitchQuotedSecret("/SourceSAS", ":", SourceSAS);
     }
     if (Recursive)
     {
         args.Append("/S");
     }
     if (BlobType != null)
     {
         args.AppendSwitchQuoted("/BlobType", ":", BlobType.ToString());
     }
     if (UseChecksum)
     {
         args.Append("/CheckMD5");
     }
     if (LogFile != null)
     {
         args.AppendSwitchQuoted("/V", ":", LogFile.FullPath);
     }
     if (ParameterFiles.Any())
     {
         foreach (var file in ParameterFiles)
         {
             args.AppendSwitchQuoted("@", ":", file.FullPath);
         }
     }
     if (FileHandlingBehaviour != null)
     {
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ArchiveOnly))
         {
             args.Append("/A");
         }
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ExcludeNewerSource))
         {
             args.Append("/XN");
         }
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ExcludeOlderSource))
         {
             args.Append("/XO");
         }
         if (FileHandlingBehaviour.Value.HasFlag(FileHandling.UpdateLastModified))
         {
             args.Append("/MT");
         }
     }
     if (Delimiter != null)
     {
         args.AppendSwitchQuoted("/Delimiter", ":", Delimiter.ToString());
     }
     if (ConcurrentOperations != 0)
     {
         args.AppendSwitchQuoted("/NC", ":", ConcurrentOperations.ToString());
     }
     if (TargetContentType != null)
     {
         // Could be present but empty
         if (TargetContentType.Equals(String.Empty))
         {
             // If you specify this option without a value, then AzCopy sets each blob or file's content type according to its file extension.
             // https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy#setcontenttypecontent-type
             args.AppendSwitchQuoted("/SetContentType", String.Empty);
         }
         else
         {
             args.AppendSwitchQuoted("/SetContentType", ":", TargetContentType);
         }
     }
     if (PayloadFormat != PayloadFormat.Default)
     {
         args.AppendSwitchQuoted("/PayloadFormat", ":", PayloadFormat.ToString());
     }
     return(args);
 }
Example #5
0
        public ProcessArgumentBuilder BuildLinuxArguments(ProcessArgumentBuilder args)
        {
            args.Append("--quiet"); // equivalent to /Y for some reason
            if (Pattern.IsDefined())
            {
                args.AppendSwitchQuoted("--include", Pattern);
            }
            if (DestinationKey.IsDefined())
            {
                args.AppendSwitchQuotedSecret("--dest-key", DestinationKey);
            }
            if (DestinationSAS.IsDefined())
            {
                args.AppendSwitchQuotedSecret("--dest-sas", DestinationSAS);
            }
            if (SourceKey.IsDefined())
            {
                args.AppendSwitchQuotedSecret("--source-key", SourceKey);
            }
            if (SourceSAS.IsDefined())
            {
                args.AppendSwitchQuotedSecret("--source-sas", SourceSAS);
            }
            if (Recursive)
            {
                args.Append("--recursive");
            }
            if (BlobType != null)
            {
                args.AppendSwitch("--blob-type", BlobType.ToString().ToLower());
            }
            if (UseChecksum)
            {
                args.Append("--check-md5");
            }
            if (ParameterFiles.Any())
            {
                foreach (var file in ParameterFiles)
                {
                    args.AppendSwitchQuoted("--config-file", file.FullPath);
                }
            }
            if (FileHandlingBehaviour != null)
            {
                if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ExcludeNewerSource))
                {
                    args.Append("--exclude-newer");
                }
                if (FileHandlingBehaviour.Value.HasFlag(FileHandling.ExcludeOlderSource))
                {
                    args.Append("--exclude-older");
                }
                if (FileHandlingBehaviour.Value.HasFlag(FileHandling.UpdateLastModified))
                {
                    args.Append("--preserve-last-modified-time");
                }
            }
            if (Delimiter != null)
            {
                args.AppendSwitchQuoted("--delimiter", Delimiter.ToString());
            }
            if (ConcurrentOperations != 0)
            {
                args.AppendSwitchQuoted("--parallel-level", ConcurrentOperations.ToString());
            }

            if (TargetContentType != null)
            {
                // Could be present but empty
                if (TargetContentType.Equals(String.Empty))
                {
                    // If you specify this option without a value, then AzCopy sets each blob or file's content type according to its file extension.
                    // https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy#setcontenttypecontent-type
                    args.AppendSwitchQuoted("--set-content-type", String.Empty);
                }
                else
                {
                    args.AppendSwitchQuoted("--set-content-type", TargetContentType);
                }
            }

            return(args);
        }