ListRepositoryTests.cs
  1  // Copyright (c) Microsoft Corporation
  2  // The Microsoft Corporation licenses this file to you under the MIT license.
  3  // See the LICENSE file in the project root for more information.
  4  
  5  using System.Threading.Tasks;
  6  
  7  using Microsoft.VisualStudio.TestTools.UnitTesting;
  8  using Wox.Infrastructure.Storage;
  9  
 10  namespace Microsoft.Plugin.Program.UnitTests.Storage
 11  {
 12      [TestClass]
 13      public class ListRepositoryTests
 14      {
 15          [TestMethod]
 16          public void ContainsShouldReturnTrueWhenListIsInitializedWithItem()
 17          {
 18              // Arrange
 19              var itemName = "originalItem1";
 20              ListRepository<string> repository = new ListRepository<string>() { itemName };
 21  
 22              // Act
 23              var result = repository.Contains(itemName);
 24  
 25              // Assert
 26              Assert.IsTrue(result);
 27          }
 28  
 29          [TestMethod]
 30          public void ContainsShouldReturnTrueWhenListIsUpdatedWithAdd()
 31          {
 32              // Arrange
 33              ListRepository<string> repository = new ListRepository<string>();
 34  
 35              // Act
 36              var itemName = "newItem";
 37              repository.Add(itemName);
 38              var result = repository.Contains(itemName);
 39  
 40              // Assert
 41              Assert.IsTrue(result);
 42          }
 43  
 44          [TestMethod]
 45          public void ContainsShouldReturnFalseWhenListIsUpdatedWithRemove()
 46          {
 47              // Arrange
 48              var itemName = "originalItem1";
 49              ListRepository<string> repository = new ListRepository<string>() { itemName };
 50  
 51              // Act
 52              repository.Remove(itemName);
 53              var result = repository.Contains(itemName);
 54  
 55              // Assert
 56              Assert.IsFalse(result);
 57          }
 58  
 59          [TestMethod]
 60          public async Task AddShouldNotThrowWhenBeingIterated()
 61          {
 62              // Arrange
 63              ListRepository<string> repository = new ListRepository<string>();
 64              var numItems = 1000;
 65              for (var i = 0; i < numItems; ++i)
 66              {
 67                  repository.Add($"OriginalItem_{i}");
 68              }
 69  
 70              // Act - Begin iterating on one thread
 71              var iterationTask = Task.Run(() =>
 72              {
 73                  var remainingIterations = 10000;
 74                  while (remainingIterations > 0)
 75                  {
 76                      foreach (var item in repository)
 77                      {
 78                          // keep iterating
 79                      }
 80  
 81                      --remainingIterations;
 82                  }
 83              });
 84  
 85              // Act - Insert on another thread
 86              var addTask = Task.Run(() =>
 87             {
 88                 for (var i = 0; i < numItems; ++i)
 89                 {
 90                     repository.Add($"NewItem_{i}");
 91                 }
 92             });
 93  
 94              // Assert that this does not throw.  Collections that aren't synchronized will throw an invalidoperatioexception if the list is modified while enumerating
 95              await Task.WhenAll(new Task[] { iterationTask, addTask }).ConfigureAwait(false);
 96          }
 97  
 98          [TestMethod]
 99          public async Task RemoveShouldNotThrowWhenBeingIterated()
100          {
101              // Arrange
102              ListRepository<string> repository = new ListRepository<string>();
103              var numItems = 1000;
104              for (var i = 0; i < numItems; ++i)
105              {
106                  repository.Add($"OriginalItem_{i}");
107              }
108  
109              // Act - Begin iterating on one thread
110              var iterationTask = Task.Run(() =>
111              {
112                  var remainingIterations = 10000;
113                  while (remainingIterations > 0)
114                  {
115                      foreach (var item in repository)
116                      {
117                          // keep iterating
118                      }
119  
120                      --remainingIterations;
121                  }
122              });
123  
124              // Act - Remove on another thread
125              var addTask = Task.Run(() =>
126              {
127                  for (var i = 0; i < numItems; ++i)
128                  {
129                      repository.Remove($"OriginalItem_{i}");
130                  }
131              });
132  
133              // Assert that this does not throw.  Collections that aren't synchronized will throw an invalidoperatioexception if the list is modified while enumerating
134              await Task.WhenAll(new Task[] { iterationTask, addTask }).ConfigureAwait(false);
135          }
136      }
137  }