Ejemplo n.º 1
0
 /// <summary>
 /// Creates a new instance of the <see cref="Enumerator"/> class.
 /// </summary>
 /// <param name="collection">Collection the enumerator will iterate over.</param>
 public Enumerator(FileBackedLogMessageCollection collection)
 {
     mCollection = collection;
     mCollectionChangeCounter = collection.mChangeCounter;
     mIndex = -1;
 }
        private void CreateTemporaryCollection(
            string temporaryDirectoryPath,
            bool deleteAutomatically,
            LogFilePurpose purpose,
            LogFileWriteMode mode,
            bool populate)
        {
            string effectiveTemporaryFolderPath = temporaryDirectoryPath ?? Path.GetTempPath().TrimEnd(Path.DirectorySeparatorChar);
            string backingFilePath;

            var messages = populate ? mFixture.GetLogMessages_Random_10K() : null;

            using (var collection = FileBackedLogMessageCollection.CreateTemporaryCollection(deleteAutomatically, temporaryDirectoryPath, purpose, mode, messages))
            {
                Assert.True(File.Exists(collection.FilePath));
                Assert.Equal(effectiveTemporaryFolderPath, Path.GetDirectoryName(collection.FilePath));

                using (var eventWatcher = collection.AttachEventWatcher())
                {
                    // check collection specific properties
                    // ---------------------------------------------------------------------------------------------------------------
                    Assert.Equal(20, collection.MaxCachePageCount);
                    Assert.Equal(100, collection.CachePageCapacity);

                    if (messages != null)
                    {
                        Assert.Equal(messages.Length, collection.Count);
                        Assert.Equal(messages, collection);
                    }
                    else
                    {
                        Assert.Equal(0, collection.Count);
                        Assert.Empty(collection);
                    }


                    // check log file specific properties
                    // ---------------------------------------------------------------------------------------------------------------
                    Assert.NotNull(collection.LogFile);
                    Assert.NotNull(collection.FilePath);
                    Assert.Equal(collection.LogFile.FilePath, collection.FilePath);

                    // check properties exposed by IList implementation
                    // ---------------------------------------------------------------------------------------------------------------
                    {
                        var list = (IList)collection;
                        Assert.False(list.IsReadOnly);
                        Assert.False(list.IsFixedSize);
                        Assert.False(list.IsSynchronized);
                        Assert.NotSame(collection, list.SyncRoot);                         // sync root must not be the same as the collection to avoid deadlocks

                        if (messages != null)
                        {
                            Assert.Equal(messages.Length, list.Count);
                            Assert.Equal(messages, list);
                        }
                        else
                        {
                            Assert.Equal(0, list.Count);
                            Assert.Empty(collection);
                        }
                    }

                    // check properties exposed by IList<T> implementation
                    // ---------------------------------------------------------------------------------------------------------------
                    {
                        var list = (IList <LogMessage>)collection;
                        Assert.False(list.IsReadOnly);
                        if (messages != null)
                        {
                            Assert.Equal(messages.Length, list.Count);
                            Assert.Equal(messages, list.Cast <LogFileMessage>());
                        }
                        else
                        {
                            Assert.Equal(0, list.Count);
                            Assert.Empty(collection);
                        }
                    }

                    // no events should have been raised
                    eventWatcher.CheckInvocations();
                }

                backingFilePath = collection.FilePath;
            }

            // the file should not persist after disposing the collection, if auto-deletion is enabled
            Assert.Equal(deleteAutomatically, !File.Exists(backingFilePath));

            // delete the file, if it still exists to avoid polluting the output directory
            File.Delete(backingFilePath);
        }