예제 #1
0
        public async Task RemoveAll_GetByQuery_ReturnsEmpty(int tasksCount)
        {
            using var index = new StringIndex <string>();
            var inputs = new Dictionary <string, IEnumerable <string> >();

            for (var i = 0; i < tasksCount; i++)
            {
                inputs[Guid.NewGuid().ToString()] = new []
                {
                    Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString(),
                                               Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString()
                }
            }
            ;

            var tasks = new List <Task>();

            foreach (var(q, input) in inputs)
            {
                tasks.Add(Task.Run(() => index.Add(q, input)));
            }

            await Task.WhenAll(tasks);

            var removeTasks = inputs.Keys.Select(q => Task.Run(() => index.RemoveAll(q, out _))).ToList();

            await Task.WhenAll(removeTasks);

            var getTasks = inputs.Keys.Select(q => Task.Run(() => new { Query = q, Result = index.Get(q) })).ToList();

            await Task.WhenAll(getTasks);

            foreach (var task in getTasks)
            {
                var(query, result) = (task.Result.Query, task.Result.Result);
                var input = inputs[query].ToList();
                Assert.All(input, x => Assert.DoesNotContain(x, result));
                Assert.Equal(0, result.Count);
            }
        }
예제 #2
0
        public async Task OnRemoveAll_RemoveEventInvoked_ReturnedRemovedValues(int tasksCount)
        {
            using var index = new StringIndex <string>();
            var inputs = new Dictionary <string, IEnumerable <string> >();

            for (var i = 0; i < tasksCount; i++)
            {
                inputs[Guid.NewGuid().ToString()] = new [] { Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }
            }
            ;

            foreach (var(q, input) in inputs)
            {
                index.Add(q, input);
            }

            var triggered = 0;

            index.Removed += (q, values) =>
            {
                var input = inputs[q];
                Assert.All(input, x => Assert.Contains(x, values));
                Interlocked.Increment(ref triggered);
            };

            var tasks = new List <Task>();

            foreach (var(q, _) in inputs)
            {
                tasks.Add(Task.Run(() => index.RemoveAll(q, out _)));
            }

            await Task.WhenAll(tasks);

            Assert.Equal(tasksCount, triggered);
        }
    }