Exemplo n.º 1
2
        public async Task<BlobFingerprint> GetFingerprintAsync(Stream stream, CancellationToken cancellationToken)
        {
            using (var sha3 = new Keccak())
            using (var sha256 = SHA256.Create())
            using (var md5 = MD5.Create())
            {
                var buffers = new[] { AllocateBuffer(), AllocateBuffer() };
                var index = 0;

                var totalLength = 0L;

                var readTask = stream.ReadAsync(buffers[index], 0, buffers[index].Length, cancellationToken);

                for (;;)
                {
                    var length = await readTask.ConfigureAwait(false);

                    if (length < 1)
                        break;

                    var buffer = buffers[index];

                    if (++index >= buffers.Length)
                        index = 0;

                    readTask = stream.ReadAsync(buffers[index], 0, buffers[index].Length, cancellationToken);

                    totalLength += length;

                    sha3.TransformBlock(buffer, 0, length, null, 0);

                    sha256.TransformBlock(buffer, 0, length, null, 0);

                    md5.TransformBlock(buffer, 0, length, null, 0);
                }

                sha3.TransformFinalBlock(buffers[0], 0, 0);

                sha256.TransformFinalBlock(buffers[0], 0, 0);

                md5.TransformFinalBlock(buffers[0], 0, 0);

                foreach (var buffer in buffers)
                    FreeBuffer(buffer);

                var fingerprint = new BlobFingerprint(totalLength, sha3.Hash, sha256.Hash, md5.Hash);

                return fingerprint;
            }
        }
        public void verify_bahaviour_for_concurrent_access_under_identical_keys()
        {
            var keys = new[] {"a", "a"};
            var counter = new ConcurrentStack<int>();
            var storage = new ConcurrentStack<TestItem>();

            // first run
            var threads = MakeThreads(keys);
            threads.ForEach(t => t.Start(new object[] {storage, counter}));
            threads.ForEach(t => t.Join());

            Assert.Equal(1, counter.Count);
            Assert.Equal(2, storage.Count);
            var a = storage.First();
            Assert.Same(storage.First(), storage.Last());

            // cleanups and second run
            storage.Clear();
            counter.Clear();

            threads = MakeThreads(keys);
            threads.ForEach(t => t.Start(new object[] {storage, counter}));
            threads.ForEach(t => t.Join());

            Assert.Equal(0, counter.Count);
            Assert.Equal(2, storage.Count);
            var aa = storage.First();
            Assert.Same(storage.First(), storage.Last());
            Assert.Same(a, aa);
        }
        public void verify_bahaviour_for_concurrent_access_under_different_keys()
        {
            var keys = new[] {"a", "b"};
            var counter = new ConcurrentStack<int>(); // value factory threads
            var storage = new ConcurrentStack<TestItem>(); // cached items

            // first run
            var threads = MakeThreads(keys);
            threads.ForEach(t => t.Start(new object[] {storage, counter}));
            threads.ForEach(t => t.Join());

            Assert.Equal(2, counter.Count);
            Assert.Equal(2, storage.Count);
            Assert.NotSame(storage.First(), storage.Last());
            var a = storage.FirstOrDefault(x => x.Id == "a");
            var b = storage.FirstOrDefault(x => x.Id == "b");

            // cleanups and second run
            storage.Clear();
            counter.Clear();

            threads = MakeThreads(keys);
            threads.ForEach(t => t.Start(new object[] {storage, counter}));
            threads.ForEach(t => t.Join());

            Assert.Equal(0, counter.Count);
            Assert.Equal(2, storage.Count);
            Assert.NotSame(storage.First(), storage.Last());
            var aa = storage.FirstOrDefault(x => x.Id == "a");
            var bb = storage.FirstOrDefault(x => x.Id == "b");
            Assert.Same(a, aa);
            Assert.Same(b, bb);
        }
Exemplo n.º 4
0
        protected JavascriptViewEngine(params string[] locations)
        {
            var defaultLocations = new[] {
                "~/Templates" // default locations is root/Templates/{controller}
            };

            Locations = defaultLocations.Concat((!IsNullOrEmpty(locations)) ? locations : new string[] { }).ToArray();
        }
        public void ShouldHaveInitialAvailableCapacity()
        {
            var dependentSequence = new Mock<Sequence>();

            var dependentSequences = new[] { dependentSequence.Object };

            Assert.IsTrue(_claimStrategy.HasAvailableCapacity(1, dependentSequences));
        }
        public void ShouldSetSequenceToValue()
        {
            var dependentSequence = new Mock<Sequence>();

            var dependentSequences = new[] { dependentSequence.Object };
            const long expectedSequence = 5L;
            _claimStrategy.SetSequence(expectedSequence, dependentSequences);

            Assert.AreEqual(expectedSequence, _claimStrategy.Sequence);
        }
        public void ShouldClaimInitialSequence()
        {
            var dependentSequence = new Mock<Sequence>();

            var dependentSequences = new[] {dependentSequence.Object};
            const long expectedSequence = Sequencer.InitialCursorValue + 1L;

            Assert.AreEqual(expectedSequence, _claimStrategy.IncrementAndGet(dependentSequences));
            Assert.AreEqual(expectedSequence, _claimStrategy.Sequence);
        }
        public void ShouldClaimInitialBatchOfSequences()
        {
            var dependentSequence = new Mock<Sequence>();

            var dependentSequences = new[] { dependentSequence.Object };
            const int batchSize = 5;
            const long expectedSequence = Sequencer.InitialCursorValue + batchSize;

            Assert.AreEqual(expectedSequence, _claimStrategy.IncrementAndGet(batchSize, dependentSequences));
            Assert.AreEqual(expectedSequence, _claimStrategy.Sequence);
        }
        public void ShouldNotHaveAvailableCpapcityWhenBufferIsFull()
        {
            var dependentSequence = new Mock<Sequence>();

            dependentSequence.SetupGet(s => s.Value)
                             .Returns(Sequencer.InitialCursorValue);

            var dependentSequences = new[] { dependentSequence.Object };
            _claimStrategy.SetSequence(BufferSize - 1L, dependentSequences);

            Assert.IsFalse(_claimStrategy.HasAvailableCapacity(1, dependentSequences));
        }
        public void Pause(Action onPause)
        {
            //var log = LogManager.GetLogger(Global.CallerName());

            ReadyState = ReadyStateEnum.PAUSED;
            Action pause = () =>
            {
                //log.Info("paused");
                ReadyState = ReadyStateEnum.PAUSED;
                onPause();
            };

            if (IsPolling || !Writable)
            {
                var total = new[] {0};


                if (IsPolling)
                {
                    //log.Info("we are currently polling - waiting to pause");
                    total[0]++;
                    Once(EVENT_POLL_COMPLETE, new PauseEventPollCompleteListener(total, pause));

                }

                if (!Writable)
                {
                    //log.Info("we are currently writing - waiting to pause");
                    total[0]++;
                    Once(EVENT_DRAIN, new PauseEventDrainListener(total, pause));
                }

            }
            else
            {
                pause();
            }
        }
		public void NodesDiscoveredDuringSniffShouldBeHttps()
		{

			ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) =>
			{
				if (errors == SslPolicyErrors.None)
					return true;

				string certificateHash = certificate.GetCertHashString();
				bool knownThumbprintIsValid = false;
				if (_knownPrints.TryGetValue(certificateHash, out knownThumbprintIsValid))
					return knownThumbprintIsValid;

				var isValid = IsValidCertificate(certificate, chain);
				_knownPrints.AddOrUpdate(certificateHash, isValid, (s, b) => isValid);
				return isValid;

			};
			var uris = new[]
			{
				new Uri("https://localhost:9200")
			};
			var connectionPool = new SniffingConnectionPool(uris, randomizeOnStartup: false);
			var settings = new ConnectionSettings(connectionPool, ElasticsearchConfiguration.DefaultIndex)
				.SniffOnStartup()
				.SetBasicAuthentication("mpdreamz", "blahblah")
				.ExposeRawResponse()
				//.SetPingTimeout(1000)
				.SetTimeout(2000);
			var client = new ElasticClient(settings);

			var results = client.NodesInfo();
			results.IsValid.Should().BeTrue("{0}", results.ConnectionStatus.ToString());
			results.ConnectionStatus.NumberOfRetries.Should().Be(0);
			var uri = new Uri(results.ConnectionStatus.RequestUrl);
			uri.Port.Should().Be(9200);
			uri.Scheme.Should().Be("https");

			results = client.NodesInfo();
			results.IsValid.Should().BeTrue("{0}", results.ConnectionStatus.ToString());
			results.ConnectionStatus.NumberOfRetries.Should().Be(0);
			uri = new Uri(results.ConnectionStatus.RequestUrl);
			uri.Port.Should().Be(9201);
			uri.Scheme.Should().Be("https");
		}
        public void ShouldNotReturnNextClaimSequenceUntilBufferHasReserve()
        {
            var dependentSequence = new Sequence(Sequencer.InitialCursorValue);
            var dependentSequences = new[] { dependentSequence };
            _claimStrategy.SetSequence(BufferSize - 1L, dependentSequences);

            var done = new Volatile.Boolean(false);
            var beforeLatch = new ManualResetEvent(false);
            var afterLatch = new ManualResetEvent(false);

            new Thread(() =>
                           {
                               beforeLatch.Set();

                               Assert.AreEqual(_claimStrategy.BufferSize,
                                               _claimStrategy.IncrementAndGet(dependentSequences));

                               done.WriteFullFence(true);
                               afterLatch.Set();
                           }).Start();

            beforeLatch.WaitOne();

            Thread.Sleep(1000);
            Assert.IsFalse(done.ReadFullFence());

            dependentSequence.Value = dependentSequence.Value + 1L;

            afterLatch.WaitOne();
            Assert.AreEqual(_claimStrategy.BufferSize, _claimStrategy.Sequence);
        }
        public void ShouldSerialisePublishingOnTheCursorWhenTwoThreadsArePublishing()
        {
            var dependentSequence = new Sequence(Sequencer.InitialCursorValue);
            var dependentSequences = new[] { dependentSequence };

            var threadSequences = new ConcurrentDictionary<string, long>();
            var cursor = new SequenceStub(Sequencer.InitialCursorValue, threadSequences);

            var mre = new ManualResetEvent(false);

            var t1 = new Thread(
                () =>
                    {
                        var sequence = _claimStrategy.IncrementAndGet(dependentSequences);
                        mre.Set();

                        Thread.Sleep(1000);

                        _claimStrategy.SerialisePublishing(sequence, cursor, 1);
                    });

            var t2 = new Thread(
                () =>
                    {
                        mre.WaitOne();
                        var sequence = _claimStrategy.IncrementAndGet(dependentSequences);

                        _claimStrategy.SerialisePublishing(sequence, cursor, 1);
                    });

            t1.Name = "tOne";
            t2.Name = "tTwo";
            t1.Start();
            t2.Start();
            t1.Join();
            t2.Join();

            Assert.AreEqual(0, threadSequences["tOne"]);
            Assert.AreEqual(1, threadSequences["tTwo"]);
        }
        public void ShouldSerializePublishingOnTheCursor()
        {
            var dependentSequence = new Sequence(Sequencer.InitialCursorValue);
            var dependentSequences = new[] {dependentSequence};

            var sequence = _claimStrategy.IncrementAndGet(dependentSequences);

            var cursor = new Sequence(Sequencer.InitialCursorValue);
            _claimStrategy.SerialisePublishing(sequence, cursor, 1);

            Assert.AreEqual(sequence, cursor.Value);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Return execution mode combinations, for testing multiple calls to WithExecutionMode
        /// </summary>
        /// <returns>Entries for test data.
        /// Both entries are a ParallelExecutionMode in a Cartesian join.</returns>
        public static IEnumerable<object[]> AllExecutionModes_Multiple()
        {
            ParallelExecutionMode[] modes = new[] { ParallelExecutionMode.Default, ParallelExecutionMode.ForceParallelism };

            foreach (ParallelMergeOptions first in modes)
            {
                foreach (ParallelMergeOptions second in modes)
                {
                    yield return new object[] { first, second };
                }
            }
        }