public void ClearCanThrowExceptions()
        {
            // Arrange
            var actualLogger  = new TraceSourceLogger(_logger);
            var cacheAccessor = NSubstitute.Substitute.For <ICacheAccessor>();

            cacheAccessor.WhenForAnyArgs(c => c.Clear()).Throw(new InvalidOperationException());
            var storage = new MsalCacheStorage(s_storageCreationProperties, cacheAccessor, actualLogger);

            // Act
            storage.Clear();

            // Assert
            AssertException.Throws <InvalidOperationException>(
                () => storage.Clear(ignoreExceptions: false));
        }
        /// <summary>
        /// Notification that is triggered after token acquisition.
        /// </summary>
        /// <param name="args">Arguments related to the cache item impacted</param>
        public override void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            MsalCacheStorage cacheStorage = GetMsalCacheStorage();

            args.AssertNotNull(nameof(args));

            try
            {
                if (args.HasStateChanged)
                {
                    cacheStorage.WriteData(args.TokenCache.SerializeMsalV3());
                }
            }
            catch (Exception)
            {
                cacheStorage.Clear();
                throw;
            }
            finally
            {
                CrossPlatformLock localDispose = cacheLock;
                cacheLock = null;
                localDispose?.Dispose();
            }
        }
        public void MsalTestClear()
        {
            var store = new MsalCacheStorage(s_storageCreationProperties, logger: _logger);

            Assert.IsTrue(store.HasChanged);
            var tempData = store.ReadData();

            Assert.IsFalse(store.HasChanged);

            var store2 = new MsalCacheStorage(s_storageCreationProperties, logger: _logger);

            Assert.IsNotNull(Exception <ArgumentNullException>(() => store.WriteData(null)));

            byte[] data = { 2, 2, 3 };
            store.WriteData(data);

            Assert.IsFalse(store.HasChanged);
            Assert.IsTrue(store2.HasChanged);

            store2.ReadData();

            Enumerable.SequenceEqual(store.ReadData(), data);
            Assert.IsTrue(File.Exists(CacheFilePath));

            store.Clear();
            Assert.IsFalse(store.HasChanged);
            Assert.IsTrue(store2.HasChanged);

            Assert.IsFalse(store.ReadData().Any());
            Assert.IsFalse(store2.ReadData().Any());
            Assert.IsFalse(File.Exists(CacheFilePath));
        }
        /// <summary>
        /// Notification that is triggered before token acquisition.
        /// </summary>
        /// <param name="args">Arguments related to the cache item impacted</param>
        public override void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {
            MsalCacheStorage cacheStorage = GetMsalCacheStorage();

            args.AssertNotNull(nameof(args));

            try
            {
                cacheLock = new CrossPlatformLock($"{CacheFilePath}.lockfile");

                cacheLock.CreateLockAsync().ConfigureAwait(false);
                args.TokenCache.DeserializeMsalV3(cacheStorage.ReadData());
            }
            catch (Exception)
            {
                cacheStorage.Clear();
                throw;
            }
        }