Esempio n. 1
0
        private void MessageUploadAndDownload(IEnumerable <CoreManager> coreManagers)
        {
            _callback.Invoke("----- CoreManager Message Send and Receive Test -----");
            _callback.Invoke("");

            var coreManagerList = coreManagers.ToList();

            Parallel.ForEach(coreManagerList, coreManager =>
            {
                coreManager.Resize((long)1024 * 1024 * 1024 * 32).Wait();
            });

            var hashList     = new LockedHashSet <Hash>();
            var metadataList = new LockedList <Metadata>();

            Parallel.For(0, 30, new ParallelOptions()
            {
                MaxDegreeOfParallelism = 3
            }, i =>
            {
                var random = RandomProvider.GetThreadRandom();

                using (var stream = new BufferStream(_bufferManager))
                {
                    using (var safeBuffer = _bufferManager.CreateSafeBuffer(1024 * 4))
                    {
                        for (long remain = (long)1024 * 1024 * 32; remain > 0; remain = Math.Max(0, remain - safeBuffer.Value.Length))
                        {
                            int length = (int)Math.Min(remain, safeBuffer.Value.Length);

                            random.NextBytes(safeBuffer.Value);
                            stream.Write(safeBuffer.Value, 0, length);
                        }
                    }

                    stream.Seek(0, SeekOrigin.Begin);
                    hashList.Add(new Hash(HashAlgorithm.Sha256, Sha256.ComputeHash(new WrapperStream(stream, true))));

                    stream.Seek(0, SeekOrigin.Begin);
                    using (var tokenSource = new CancellationTokenSource())
                    {
                        metadataList.Add(coreManagerList[i].VolatileSetStream(stream, new TimeSpan(1, 0, 0), tokenSource.Token).Result);
                    }
                }
            });

            var sw = Stopwatch.StartNew();

            Parallel.ForEach(metadataList, metadata =>
            {
                for (; ;)
                {
                    Thread.Sleep(1000);

                    Stream stream = null;

                    try
                    {
                        stream = coreManagerList[0].VolatileGetStream(metadata, 1024 * 1024 * 256);
                        if (stream == null)
                        {
                            continue;
                        }

                        var hash = new Hash(HashAlgorithm.Sha256, Sha256.ComputeHash(stream));
                        if (!hashList.Contains(hash))
                        {
                            throw new ArgumentException("Broken");
                        }

                        _callback.Invoke($"{sw.Elapsed.ToString("hh\\:mm\\:ss")}: Success {NetworkConverter.ToBase64UrlString(metadata.Hash.Value)}");

                        return;
                    }
                    finally
                    {
                        if (stream != null)
                        {
                            stream.Dispose();
                        }
                    }
                }
            });

            _callback.Invoke("----- End -----");
        }