Exemple #1
0
        public void Process(IEnumerable <LocalLogMessage> messages)
        {
            // create a new pipeline stage
            var formatter = new TestFormatter();
            var stage     = CreateStage("File");

            stage.Formatter = formatter;

            // initialize the pipeline stage
            stage.Initialize();

            // process the message and determine the expected output in stdout/stderr
            // (the stage automatically adds a newline after each message)
            var expected = new StringBuilder();

            foreach (var message in messages)
            {
                stage.ProcessMessage(message);
                expected.AppendLine(formatter.Format(message));
            }

            // shut the pipeline stage down to release the file
            stage.Shutdown();

            // the file should be closed and contain the expected output now
            using (var fs = new FileStream(stage.Path, FileMode.Open, FileAccess.Read, FileShare.None))
                using (var reader = new StreamReader(fs))
                {
                    string content = reader.ReadToEnd();
                    Assert.Equal(expected.ToString(), content);
                }
        }
Exemple #2
0
        public void Formatter_SetSuccessfully()
        {
            var stage     = ProcessingPipelineStage.Create <TStage>("Stage", null);
            var formatter = new TestFormatter();

            stage.Formatter = formatter;
            Assert.Same(formatter, stage.Formatter);
        }
Exemple #3
0
        public void Formatter_FailsIfInitialized()
        {
            var stage     = ProcessingPipelineStage.Create <TStage>("Stage", null);
            var formatter = new TestFormatter();

            stage.Initialize();
            Assert.Throws <InvalidOperationException>(() => stage.Formatter = formatter);
            stage.Shutdown();
        }
        public void Process(ConsoleOutputStream defaultStream, List <Tuple <LogLevel, ConsoleOutputStream> > mappings, IEnumerable <LocalLogMessage> messages)
        {
            // create a new pipeline stage
            var formatter = new TestFormatter();
            var stage     = ProcessingPipelineStage.Create <ConsoleWriterPipelineStage>("Console", null);

            stage.DefaultStream = defaultStream;
            stage.Formatter     = formatter;

            // replace i/o streams to avoid mixing up test output with regular console output
            var stdoutStream = Stream.Synchronized(new MemoryStream());             // the stage writes to the stream asynchronously,
            var stderrStream = Stream.Synchronized(new MemoryStream());             // so synchronize access to the stream
            var stdoutWriter = new StreamWriter(stdoutStream);
            var stderrWriter = new StreamWriter(stderrStream);

            stage.OutputStream = stdoutWriter;
            stage.ErrorStream  = stderrWriter;

            // configure the stage
            // build a dictionary that contains the mappings from log level to the corresponding console stream
            var levelToStreamMap = new Dictionary <LogLevel, ConsoleOutputStream>();

            foreach (var mapping in mappings)
            {
                levelToStreamMap[mapping.Item1] = mapping.Item2;
                stage.MapLogLevelToStream(mapping.Item1, mapping.Item2);
            }

            // initialize the pipeline stage
            stage.Initialize();

            // process the message and determine the expected output in stdout/stderr
            var expectedStdout = new StringBuilder();
            var expectedStderr = new StringBuilder();

            foreach (var message in messages)
            {
                stage.ProcessMessage(message);

                if (!levelToStreamMap.TryGetValue(message.LogLevel, out var stream))
                {
                    stream = defaultStream;
                }

                // add formatted message to the expected output
                // (the stage automatically adds a newline after each message)
                string formattedOutput = formatter.Format(message);
                if (stream == ConsoleOutputStream.Stdout)
                {
                    expectedStdout.AppendLine(formattedOutput);
                }
                else
                {
                    expectedStderr.AppendLine(formattedOutput);
                }
            }

            // give the messages some time (500ms) to travel through the pipeline
            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(50);
                if (stdoutStream.Length >= expectedStdout.Length && stderrStream.Length >= expectedStderr.Length)
                {
                    break;
                }
            }

            // the streams should contain the output now
            stdoutStream.Position = 0;
            stderrStream.Position = 0;
            byte[] stdoutData           = new byte[stdoutStream.Length];
            byte[] stderrData           = new byte[stderrStream.Length];
            int    stdoutBytesReadCount = stdoutStream.Read(stdoutData, 0, stdoutData.Length);
            int    stderrBytesReadCount = stderrStream.Read(stderrData, 0, stderrData.Length);

            Assert.Equal(stdoutData.Length, stdoutBytesReadCount);
            Assert.Equal(stderrData.Length, stderrBytesReadCount);
            var    stdoutReader = new StreamReader(new MemoryStream(stdoutData));
            var    stderrReader = new StreamReader(new MemoryStream(stderrData));
            string stdoutOutput = stdoutReader.ReadToEnd();
            string stderrOutput = stderrReader.ReadToEnd();

            Assert.Equal(expectedStdout.ToString(), stdoutOutput);
            Assert.Equal(expectedStderr.ToString(), stderrOutput);

            // shut the pipeline stage down
            stage.Shutdown();
            Assert.False(stage.IsInitialized);
        }