Ejemplo n.º 1
0
        public async Task ExceedMaxOpenFiles()
        {
            var streams       = new List <ManualStream> ();
            var streamCreated = new ReusableTaskCompletionSource <bool> ();
            Func <ITorrentFileInfo, FileAccess, ITorrentFileStream> creator = (file, access) => {
                var s = new ManualStream(file, access);
                s.WriteTcs = new ReusableTaskCompletionSource <int> ();
                streams.Add(s);
                streamCreated.SetResult(true);
                return(s);
            };

            using var writer = new DiskWriter(creator, 1);

            var writeTask = writer.WriteAsync(TorrentFile, 0, new byte[100], 0, 100);
            await streamCreated.Task.WithTimeout();

            // There's a limit of 1 concurrent read/write.
            var secondStreamWaiter = streamCreated.Task.AsTask();
            var secondStream       = writer.WriteAsync(Others.First(), 0, new byte[100], 0, 100);

            Assert.ThrowsAsync <TimeoutException> (() => secondStreamWaiter.WithTimeout(100));

            streams[0].WriteTcs.SetResult(1);
            await secondStreamWaiter.WithTimeout();

            streams[1].WriteTcs.SetResult(1);

            await secondStream.WithTimeout();

            Assert.IsTrue(streams[0].Disposed);
            Assert.IsFalse(streams[1].Disposed);
        }