Ejemplo n.º 1
0
        public async Task CouldReadDataStreamWhileWritingFromManyThreads()
        {
            var map = new SortedMap <int, int>();

            var count  = 1_000_000;
            var rounds = 5;

            var writeTask = Task.Run(async() =>
            {
                using (Benchmark.Run("Write", count * rounds, true))
                {
                    for (int j = 0; j < rounds; j++)
                    {
                        var t1 = Task.Run(async() =>
                        {
                            try
                            {
                                for (int i = j * count; i < (j + 1) * count; i++)
                                {
                                    await map.TryAddLast(i, i);
                                    Thread.SpinWait(10);
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                throw;
                            }
                        });
                        await t1;
                    }
                }
            });

            AsyncCursor <int, int, SortedMapCursor <int, int> > cursor = null;
            var cnt      = 0L;
            var readTask = Task.Run(async() =>
            {
                for (int r = 0; r < 1; r++)
                {
                    using (cursor = new AsyncCursor <int, int, SortedMapCursor <int, int> >(map.GetCursor()))
                    {
                        using (Benchmark.Run("Read", count * rounds, true))
                        {
                            try
                            {
                                while (await cursor.MoveNextAsync())
                                {
                                    Interlocked.Increment(ref cnt);
                                }
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                throw;
                            }


                            // Left from coreclr 19161 tests, TODO remove when everything works OK
                            // here is a strong reference to cursor with side effects of printing to console
                            // Console.WriteLine("Last value: " + cursor.Current.Key);
                            // another strong reference after while loop, we dereference it's value and return from task
                            // lastKey1 = cursor.CurrentKey;
                        }
                    }
                }
            });



            var monitor = true;
            var t       = Task.Run(async() =>
            {
                try
                {
                    while (monitor)
                    {
                        await Task.Delay(1000);
                        Console.WriteLine($"Key {cursor.CurrentKey}");
                        cursor.TryComplete(false);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            });

            await writeTask;
            await map.Complete();

            map.NotifyUpdate(true);
            Console.WriteLine("Read after map complete:" + Interlocked.Read(ref cnt));
            await readTask;

            Console.WriteLine("Read after finish:" + Interlocked.Read(ref cnt));
            // Console.WriteLine("Last key: " + lastKey);

            Benchmark.Dump();

            map.Dispose();
            monitor = false;
        }