-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathRunnerTestUtils.cs
140 lines (117 loc) · 4.27 KB
/
RunnerTestUtils.cs
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Security.Cryptography;
using System.Text;
using System.Threading.Channels;
using Tes.Runner.Transfer;
namespace Tes.Runner.Test;
public class RunnerTestUtils
{
static readonly Random Random = new();
public static async Task<string> CreateTempFileAsync()
{
var file = $"{Guid.NewGuid()}.tmp";
await using var fs = File.Create(file);
fs.Close();
return file;
}
public static string CalculateMd5(string file)
{
using var md5 = MD5.Create();
using var stream = File.OpenRead(file);
var hash = md5.ComputeHash(stream);
return Convert.ToBase64String(hash);
}
public static void DeleteFileIfExists(string file)
{
if (File.Exists(file))
{
File.Delete(file);
}
}
public static async Task<List<T>> ReadAllPipelineBuffersAsync<T>(IAsyncEnumerable<T> source)
{
var pipelineBuffers = new List<T>();
await foreach (var item in source)
{
pipelineBuffers.Add(item);
}
return pipelineBuffers;
}
public static string AddRandomDataAndReturnMd5(byte[] data)
{
Random.NextBytes(data);
using var md5 = MD5.Create();
var hash = md5.ComputeHash(data);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
public static string GetRootHashFromSortedHashList(List<String> hashList)
{
var hashListContent = string.Join("", hashList);
using var md5 = MD5.Create();
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(hashListContent));
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
public static async Task<string> CreateTempFileWithContentAsync(int numberOfMiB, int extraBytes = 0)
{
var file = Guid.NewGuid().ToString();
await using var fs = File.Create($"{file}.tmp", BlobSizeUtils.MiB);
var data = new byte[BlobSizeUtils.MiB];
Random.NextBytes(data);
for (var blocks = 0; blocks < numberOfMiB; blocks++)
{
await fs.WriteAsync(data, 0, BlobSizeUtils.MiB);
}
if (extraBytes > 0)
{
var extraData = new byte[extraBytes];
Random.NextBytes(extraData);
await fs.WriteAsync(extraData, 0, extraBytes);
}
fs.Close();
return fs.Name;
}
public static async Task AddPipelineBuffersAndCompleteChannelAsync(Channel<PipelineBuffer> pipelineBuffers,
int numberOfParts, Uri blobUrl, int blockSize, long fileSize, string fileName)
{
var buffers = CreatePipelineBuffers(numberOfParts, blobUrl, blockSize, fileSize, fileName);
foreach (var buffer in buffers)
{
await pipelineBuffers.Writer.WriteAsync(buffer);
}
pipelineBuffers.Writer.Complete();
}
public static List<PipelineBuffer> CreatePipelineBuffers(int numberOfParts, Uri blobUrl, int blockSize,
long fileSize, string fileName)
{
var pipelineBuffers = new List<PipelineBuffer>();
for (var partOrdinal = 0; partOrdinal < numberOfParts; partOrdinal++)
{
var buffer = new PipelineBuffer()
{
BlobUrl = blobUrl,
Offset = (long)partOrdinal * blockSize,
Length = blockSize,
FileName = fileName,
Ordinal = partOrdinal,
NumberOfParts = numberOfParts,
FileSize = fileSize,
Data = new byte[blockSize]
};
if (partOrdinal == numberOfParts - 1)
{
buffer.Length = (int)(fileSize - buffer.Offset);
}
pipelineBuffers.Add(buffer);
}
return pipelineBuffers;
}
public static async Task AddProcessedBufferAsync(Channel<ProcessedBuffer> processedBuffer, string fileName, int numberOfParts, long fileSize)
{
for (var i = 0; i < numberOfParts; i++)
{
var processedPart = new ProcessedBuffer(fileName, null, fileSize, i, numberOfParts, Channel.CreateUnbounded<FileStream>(), null, 0, null);
await processedBuffer.Writer.WriteAsync(processedPart);
}
}
}