chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using NUnit.Framework;
using System.IO;
using System.Linq;
using Tap.Shared;
 
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class SessionLogTest
    {
        TraceSource log =  OpenTap.Log.CreateSource("SessionLogTest");
        [Test]
        public void RenameSessionLogFile()
        {
            Log.Flush();
 
            var currentName = SessionLogs.GetSessionLogFilePath();
            SessionLogs.Rename("Log1.txt");
            SessionLogs.Rename("LogTest/Log2.txt");
            string inlog = "This is written to log2";
            log.Debug(inlog);
            Log.Flush();
            SessionLogs.Flush();
            var file = File.Open("LogTest/Log2.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            using (var reader = new StreamReader(file))
            {
                var part = reader.ReadToEnd();
                StringAssert.Contains(inlog, part);
            }
 
            Assert.AreEqual(Path.GetFullPath(SessionLogs.GetSessionLogFilePath()), Path.GetFullPath("LogTest/Log2.txt"));
            SessionLogs.Rename(currentName);
            Assert.AreEqual(currentName, SessionLogs.GetSessionLogFilePath());
            Assert.IsFalse(File.Exists("Log1.txt"));
            Assert.IsFalse(File.Exists("LogTest/Log2.txt"));
            Assert.IsTrue(Directory.Exists("LogTest"));
        }
 
         /// <summary>
         ///  Test the log rollover due to large file works.
         /// </summary>
        [Test]
        public void LogRolloverTest()
        {
            Log.Flush();
            var currentName = SessionLogs.GetSessionLogFilePath();
            
            var prev = SessionLogs.MaxTotalSizeOfSessionLogFiles;
            var prevLogFileMaxSize = SessionLogs.LogFileMaxSize;
            try
            {
                SessionLogs.MaxTotalSizeOfSessionLogFiles = 300_000;
                SessionLogs.LogFileMaxSize = 10_000;
                var guid = Guid.NewGuid();
                SessionLogs.Rename($"TestLogs/{guid}-RolloverTest.txt", true);
                // this should result in about 30 log files, but the number of files is limited to 20.
                for (int i = 0; i < 300; i++)
                {
                    log.Info(new string('?', 1000));
                    log.Flush();
                }
                var files1 = Directory.GetFiles("TestLogs/").Where(file => file.Contains(guid.ToString())).ToArray();
                Assert.AreEqual(20, files1.Length);
                var totalLength1 = files1.Select(file => new FileInfo(file).Length).Sum();
                // totalLength of log files should be around 20 * 10000. Limited by max number of files (20).
                
                Assert.IsTrue(totalLength1 - 20 * 10000 < 20 * 1000); // allow for an overshoot of 1kb per file.
                
                SessionLogs.LogFileMaxSize = 30_000;
                guid = Guid.NewGuid();
                SessionLogs.Rename($"TestLogs/{guid}-RolloverTest.txt", true);
                for (int i = 0; i < 300; i++)
                {
                    log.Info(new string('?', 1000));
                    log.Flush();
                }
                var files2 = Directory.GetFiles("TestLogs/").Where(file => file.Contains(guid.ToString())).ToArray();
                
                var totalLength2 = files2.Select(file => new FileInfo(file).Length).Sum();
                // now totalLength should be around 300000 limited by the maximally combined size.
                
                Assert.IsTrue(Math.Abs(totalLength2 - 300000) < 30000); // allow for an overshoot of 10kb.
 
                // any file from before should have been removed.
                Assert.IsFalse(files1.Any(File.Exists));
                
                guid = Guid.NewGuid();
                SessionLogs.Rename($"TestLogs/{guid}-RolloverTest.txt", true);
                foreach (var file in files2)
                {
                    File.Delete(file);
                }
                
                // now generate logs so there should be exactly two files.
                for (int i = 0; i < 7; i++)
                {
                    log.Info(new string('?', 5000));
                    log.Flush();
                }
                var files3 = Directory.GetFiles("TestLogs/").Where(file => file.Contains(guid.ToString())).ToArray();
                var totalLength3 = files3.Sum(x => new FileInfo(x).Length);
                Assert.IsTrue(Math.Abs(totalLength3 - 35000) < 5000);
                Assert.AreEqual(2, files3.Length);
                
                var activePath = files3.OrderBy(x => new FileInfo(x).Length).First();
                var currentLogFile = SessionLogs.GetSessionLogFilePath();
                Assert.IsTrue(PathUtils.AreEqual(activePath, currentLogFile));
                
 
            }
            finally
            {
                SessionLogs.MaxTotalSizeOfSessionLogFiles = prev;
                SessionLogs.LogFileMaxSize = prevLogFileMaxSize;
                SessionLogs.Rename(currentName);
 
            }
        }
    }
}