public async Task ReadableStreamTestReadRandomBytes() { using (var expected = new MemoryStream()) { for (var i = 0; i < 10000; i++) { expected.WriteByte((byte)(i % byte.MaxValue)); } expected.Position = 0; using (var stream = await SerializableBytes.CreateReadableStreamAsync(expected, CancellationToken.None)) { Assert.Equal(expected.Length, stream.Length); var random = new Random(0); for (var i = 0; i < 100; i++) { var position = random.Next((int)expected.Length); expected.Position = position; stream.Position = position; Assert.Equal(expected.ReadByte(), stream.ReadByte()); } } } }
public async Task ReadableStreamTestReadChunks() { using (var expected = new MemoryStream()) { for (var i = 0; i < 10000; i++) { expected.WriteByte((byte)(i % byte.MaxValue)); } expected.Position = 0; using (var stream = await SerializableBytes.CreateReadableStreamAsync(expected, CancellationToken.None)) { Assert.Equal(expected.Length, stream.Length); stream.Position = 0; int index = 0; int count; var bytes = new byte[1000]; while ((count = stream.Read(bytes, 0, bytes.Length)) > 0) { for (var i = 0; i < count; i++) { Assert.Equal((byte)(index % byte.MaxValue), bytes[i]); index++; } } Assert.Equal(index, stream.Length); } } }
private static async Task <(ErgonProject project, DiagnosticLog log)> LoadProjectAsync( string path, ErgonProjectCollection projectCollection, CancellationToken cancellationToken) { var log = new DiagnosticLog(); try { var loadedProjects = projectCollection.GetLoadedProjects(path); if (loadedProjects != null && loadedProjects.Count > 0) { Debug.Assert(loadedProjects.Count == 1); return(loadedProjects.First(), log); } using (var stream = FileUtilities.OpenAsyncRead(path)) using (var readStream = await SerializableBytes.CreateReadableStreamAsync(stream, cancellationToken).ConfigureAwait(false)) using (var reader = new StreamReader(readStream)) { var project = ErgonProject.FromString(await reader.ReadToEndAsync(), path); projectCollection.Add(project); // todo: assign project.path = path return(project, log); } } catch (Exception e) { log.Add(e, path); return(project : null, log); } }
public async Task <(MSB.Evaluation.Project project, DiagnosticLog log)> LoadProjectAsync( string path, IDictionary <string, string> globalProperties, CancellationToken cancellationToken) { var log = new DiagnosticLog(); try { var projectCollection = _projectCollection ?? new MSB.Evaluation.ProjectCollection(s_defaultGlobalProperties); var project = FindProject(path, globalProperties, projectCollection, cancellationToken); if (project == null) { using (var stream = FileUtilities.OpenAsyncRead(path)) using (var readStream = await SerializableBytes.CreateReadableStreamAsync(stream, cancellationToken).ConfigureAwait(false)) using (var xmlReader = XmlReader.Create(readStream, s_xmlReaderSettings)) { var xml = MSB.Construction.ProjectRootElement.Create(xmlReader, projectCollection); // When constructing a project from an XmlReader, MSBuild cannot determine the project file path. Setting the // path explicitly is necessary so that the reserved properties like $(MSBuildProjectDirectory) will work. xml.FullPath = path; project = new MSB.Evaluation.Project(xml, globalProperties, toolsVersion: null, projectCollection); } } return(project, log); } catch (Exception e) { log.Add(e, path); return(project : null, log); } }
private static async Task <(MSB.Evaluation.Project?project, DiagnosticLog log)> LoadProjectAsync( string path, MSB.Evaluation.ProjectCollection?projectCollection, CancellationToken cancellationToken) { var log = new DiagnosticLog(); try { var loadedProjects = projectCollection?.GetLoadedProjects(path); if (loadedProjects != null && loadedProjects.Count > 0) { Debug.Assert(loadedProjects.Count == 1); return(loadedProjects.First(), log); } using var stream = FileUtilities.OpenAsyncRead(path); using var readStream = await SerializableBytes.CreateReadableStreamAsync(stream, cancellationToken).ConfigureAwait(false); using var xmlReader = XmlReader.Create(readStream, s_xmlReaderSettings); var xml = MSB.Construction.ProjectRootElement.Create(xmlReader, projectCollection); // When constructing a project from an XmlReader, MSBuild cannot determine the project file path. Setting the // path explicitly is necessary so that the reserved properties like $(MSBuildProjectDirectory) will work. xml.FullPath = path; var project = new MSB.Evaluation.Project(xml, globalProperties: null, toolsVersion: null, projectCollection); return(project, log); } catch (Exception e) { log.Add(e, path); return(project : null, log); } }
public async Task ReadableStreamTestReadAByteAtATime() { using var expected = new MemoryStream(); for (var i = 0; i < 10000; i++) { expected.WriteByte((byte)(i % byte.MaxValue)); } expected.Position = 0; using var stream = await SerializableBytes.CreateReadableStreamAsync(expected, CancellationToken.None); Assert.Equal(expected.Length, stream.Length); expected.Position = 0; stream.Position = 0; for (var i = 0; i < expected.Length; i++) { Assert.Equal(expected.ReadByte(), stream.ReadByte()); } }