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
using System;
using System.IO;
using System.Linq;
using NUnit.Framework;
using OpenTap.Plugins.BasicSteps;
namespace OpenTap.Engine.UnitTests
{
    [TestFixture]
    public class ArtifactsTest 
    {
        [Test]
        public void ZippingLogFileTest()
        {
            var zip = new ArtifactZipper();
            var txt = new LogResultListener();
            var step = new ArtifactStep();
            var step2 = new ArtifactStep { AsStream = true };
 
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(step);
            plan.ChildTestSteps.Add(step2);
 
            var file = "test-artifact.txt";
            File.WriteAllText(file, "test" + new string('a', 1024 * 1024) + "test");
            var file2 = "test-artifact2.txt";
            File.WriteAllText(file2, "test" + new string('a', 1024 * 1024) + "test");
            step.File = file;
            step2.File = file2;
            
            var run = plan.Execute(new IResultListener[]{zip, txt});
            Assert.AreEqual(4, run.Artifacts.Count());
            Assert.IsTrue(run.Artifacts.Contains(file));
            Assert.IsTrue(run.Artifacts.Contains(file2));
            Assert.IsTrue(run.Artifacts.Any(x => Path.GetExtension(x) == ".zip"));
        }
 
        public class ArtifactMemoryStreamStep : TestStep
        {
            public byte[] payload { get; set; }
            public override void Run()
            {
                var ms = new MemoryStream(payload);
                ms.Seek(0, SeekOrigin.Begin);
                this.PlanRun.PublishArtifact(ms, "MemoryStream Artifact");
                UpgradeVerdict(Verdict.Pass);
            }
        }
 
        class MemoryStreamArtifactListener : ResultListener, IArtifactListener
        {
            public byte[] ExpectedPayload { get; set; }
            public byte[] ActualPayload { get; set; }
            public void OnArtifactPublished(TestRun run, Stream artifactStream, string artifactName)
            {
                var ms = new MemoryStream();
                artifactStream.CopyTo(ms);
                ActualPayload = ms.ToArray();
            }
        }
        
        [Test]
        public void ArtifactMemoryStreamTest([Values(1, 2, 4, 8)] int concurrentListeners, [Values(1, 1 << 10, 1 << 15, 1 << 20)] int streamLength)
        {
            byte[] payload = new byte[streamLength];
            var rand = new Random();
            rand.NextBytes(payload);
            var step = new ArtifactMemoryStreamStep()
            {
                payload = payload,
            };
            var listeners = Enumerable.Range(0, concurrentListeners)
                .Select(x => new MemoryStreamArtifactListener() { ExpectedPayload = step.payload }).ToArray();
            var plan = new TestPlan()
            {
                ChildTestSteps = { step },
            };
            var run = plan.Execute(listeners);
            Assert.That(run.Verdict, Is.EqualTo(Verdict.Pass));
            foreach (var rl in listeners)
                Assert.That(rl.ExpectedPayload, Is.EqualTo(rl.ActualPayload).AsCollection);
        }
 
        [Test]
        public void ZippingLogFileTestParallel()
        {
            var zip = new ArtifactZipper();
            var txt = new LogResultListener();
            var parallel = new ParallelStep();
            
            var plan = new TestPlan();
            plan.ChildTestSteps.Add(parallel);
 
            var file = "test-artifact.txt";
            File.WriteAllText(file, "test" + new string('a', 1024 * 1024) + "test");
            var file2 = "test-artifact2.txt";
            File.WriteAllText(file2, "test" + new string('a', 1024 * 1024) + "test");
            
            for (int i = 0; i < 4; i++)
            {
                var step = new ArtifactStep();
                var step2 = new ArtifactStep { AsStream = true };
                parallel.ChildTestSteps.Add(step);
                parallel.ChildTestSteps.Add(step2);
                step.File = file;
                step2.File = file2;
            }
            
            var run = plan.Execute(new IResultListener[]{zip, txt});
            Assert.AreEqual(4, run.Artifacts.Count());
        }
    }
}