Skip to content

Commit

Permalink
Add unit tests for SingleInstanceHelper and fix formatting from simpl…
Browse files Browse the repository at this point in the history
…ifying PR #11863 (#12261)

* No code Chnages just formatting

* Add tests for SignleInstanceHelper from #11863

* Moved to SDK RC2 (#12254)

* Moved to SDK RC2 to get the same build errors in VS and CLI build and be able to fix them.

Before this change the IntPreview version of VS was correctly complaining about a redundant cast(IDE0004) in ToolStrip.cs
    g.DrawLines(SystemPens.ControlText, (ReadOnlySpan<Point>)
        [
            new(verticalBeamStart, _lastInsertionMarkRect.Y), new(verticalBeamStart, _lastInsertionMarkRect.Bottom - 1),
            new(verticalBeamStart + 1, _lastInsertionMarkRect.Y), new(verticalBeamStart + 1, _lastInsertionMarkRect.Bottom - 1)
        ]);

But the CLI build required this cast.

After the upgrade to RC2, IDE0300 - Collection initialization can be simplified - became more robust and required code fixes that use collection expressions applied to the solution.

* Adds XML Comments to FileSystemProxy and SpecialDirectoriesProxy (#12141)

* Add XML Comments related to FileSystemProxy which includes SpecialDirectoriesProxy

* Fix some types

* Add some language keywords

* Update src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/FileSystemProxy.vb

Co-authored-by: Loni Tra <[email protected]>

* Update src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/FileSystemProxy.vb

Co-authored-by: Loni Tra <[email protected]>

* Update src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/FileSystemProxy.vb

Co-authored-by: Loni Tra <[email protected]>

* Update src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/FileSystemProxy.vb

Co-authored-by: Loni Tra <[email protected]>

* PR feedback

* Update all XML comments

* Fix Typo in FileSystemProxy that caused build to fail.

* Update XML Coments

* Update src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/SpecialDirectoriesProxy.vb

Co-authored-by: Tanya Solyanik <[email protected]>

* Update src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/MyServices/SpecialDirectoriesProxy.vb

Co-authored-by: Tanya Solyanik <[email protected]>

* Redo all the comments in SpecialDirectoriesProxy

* Add cref per PR feedback and changed type to Namespace

* PR Feedback to add see cref's

---------

Co-authored-by: Loni Tra <[email protected]>
Co-authored-by: Tanya Solyanik <[email protected]>

* [main] Update dependencies from dotnet/runtime (#12266)

[main] Update dependencies from dotnet/runtime

* Fix IDE0002 in LogTests.cs (#12265)

Fix IDE0002 in LogTests.cs

* [main] Update dependencies from dotnet/arcade (#12271)

[main] Update dependencies from dotnet/arcade

* [main] Update dependencies from dotnet/runtime (#12272)

[main] Update dependencies from dotnet/runtime

* Improve code coverage for FileLogTraceListener from simplifying PR #11863  (#12264)

* Improve code coverage for FileLogTraceListener

* Update src/Microsoft.VisualBasic/tests/UnitTests/Microsoft/VisualBasic/Logging/FileLogTraceListenerTests.cs

Thanks I had no idea about boolData

Co-authored-by: Loni Tra <[email protected]>

---------

Co-authored-by: Loni Tra <[email protected]>

* Switch default feed to use wildcards only (#12268)

* Switch default feed to full wildcard

* Add other wildcards back

* Move local closer to usage

* PR Feedback

* Address PR Feedback

* PR Feedback

* PR Feedback inline private Sub, reuse commandLine

---------

Co-authored-by: Tanya Solyanik <[email protected]>
Co-authored-by: Loni Tra <[email protected]>
Co-authored-by: dotnet-maestro[bot] <42748379+dotnet-maestro[bot]@users.noreply.github.com>
Co-authored-by: Rich Lander <[email protected]>
  • Loading branch information
5 people authored Oct 11, 2024
1 parent 7c5209a commit 3760721
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ Namespace Microsoft.VisualBasic.ApplicationServices
Friend Module SingleInstanceHelpers
Private Const NamedPipeOptions As PipeOptions = PipeOptions.Asynchronous Or PipeOptions.CurrentUserOnly

Private Async Function ReadArgsAsync(pipeServer As NamedPipeServerStream, cancellationToken As CancellationToken) As Task(Of String())
Private Async Function ReadArgsAsync(
pipeServer As NamedPipeServerStream,
cancellationToken As CancellationToken) As Task(Of String())

Const bufferLength As Integer = 1024
Dim buffer As Byte() = New Byte(bufferLength - 1) {}

Using stream As New MemoryStream
While True
Dim buffer As Byte() = New Byte(bufferLength - 1) {}
Dim bytesRead As Integer = Await pipeServer.ReadAsync(
buffer.AsMemory(0, bufferLength),
cancellationToken).ConfigureAwait(False)
buffer:=buffer.AsMemory(start:=0, length:=bufferLength),
cancellationToken) _
.ConfigureAwait(continueOnCapturedContext:=False)
If bytesRead = 0 Then
Exit While
End If
Await stream.WriteAsync(
buffer.AsMemory(0, bytesRead),
cancellationToken).ConfigureAwait(False)
buffer:=buffer.AsMemory(start:=0, length:=bytesRead),
cancellationToken) _
.ConfigureAwait(continueOnCapturedContext:=False)
End While
stream.Seek(0, SeekOrigin.Begin)
Dim serializer As New DataContractSerializer(GetType(String()))
Expand All @@ -37,48 +43,70 @@ Namespace Microsoft.VisualBasic.ApplicationServices
End Using
End Function

Private Async Function WriteArgsAsync(pipeClient As NamedPipeClientStream, args As String(), cancellationToken As CancellationToken) As Task
Private Async Function WriteArgsAsync(
pipeClient As NamedPipeClientStream,
args As String(),
cancellationToken As CancellationToken) As Task

Dim content As Byte()
Using stream As New MemoryStream
Dim serializer As New DataContractSerializer(GetType(String()))
serializer.WriteObject(stream, args)
content = stream.ToArray()
End Using
Await pipeClient.WriteAsync(content.AsMemory(0, content.Length), cancellationToken).ConfigureAwait(False)
Await pipeClient.WriteAsync(
buffer:=content.AsMemory(start:=0, length:=content.Length), cancellationToken) _
.ConfigureAwait(continueOnCapturedContext:=False)
End Function

Friend Async Function SendSecondInstanceArgsAsync(pipeName As String, args As String(), cancellationToken As CancellationToken) As Task
Friend Async Function SendSecondInstanceArgsAsync(
pipeName As String,
args As String(),
cancellationToken As CancellationToken) As Task

Using pipeClient As New NamedPipeClientStream(
serverName:=".",
pipeName:=pipeName,
direction:=PipeDirection.Out,
options:=NamedPipeOptions)
Await pipeClient.ConnectAsync(cancellationToken).ConfigureAwait(False)
Await WriteArgsAsync(pipeClient, args, cancellationToken).ConfigureAwait(False)
serverName:=".",
pipeName:=pipeName,
direction:=PipeDirection.Out,
options:=NamedPipeOptions)

Await pipeClient.ConnectAsync(cancellationToken) _
.ConfigureAwait(continueOnCapturedContext:=False)
Await WriteArgsAsync(pipeClient, args, cancellationToken) _
.ConfigureAwait(continueOnCapturedContext:=False)
End Using
End Function

Friend Function TryCreatePipeServer(pipeName As String, <Out> ByRef pipeServer As NamedPipeServerStream) As Boolean
Friend Function TryCreatePipeServer(
pipeName As String,
<Out> ByRef pipeServer As NamedPipeServerStream) As Boolean

Try
pipeServer = New NamedPipeServerStream(
pipeName:=pipeName,
direction:=PipeDirection.In,
maxNumberOfServerInstances:=1,
transmissionMode:=PipeTransmissionMode.Byte,
options:=NamedPipeOptions)
pipeName:=pipeName,
direction:=PipeDirection.In,
maxNumberOfServerInstances:=1,
transmissionMode:=PipeTransmissionMode.Byte,
options:=NamedPipeOptions)
Return True
Catch ex As Exception
pipeServer = Nothing
Return False
End Try
End Function

Friend Async Function WaitForClientConnectionsAsync(pipeServer As NamedPipeServerStream, callback As Action(Of String()), cancellationToken As CancellationToken) As Task
Friend Async Function WaitForClientConnectionsAsync(
pipeServer As NamedPipeServerStream,
callback As Action(Of String()),
cancellationToken As CancellationToken) As Task

While True
cancellationToken.ThrowIfCancellationRequested()
Await pipeServer.WaitForConnectionAsync(cancellationToken).ConfigureAwait(False)
Await pipeServer.WaitForConnectionAsync(cancellationToken) _
.ConfigureAwait(continueOnCapturedContext:=False)
Try
Dim args() As String = Await ReadArgsAsync(pipeServer, cancellationToken).ConfigureAwait(False)
Dim args() As String = Await ReadArgsAsync(pipeServer, cancellationToken) _
.ConfigureAwait(continueOnCapturedContext:=False)
If args IsNot Nothing Then
callback(args)
End If
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
' Licensed to the .NET Foundation under one or more agreements.
' The .NET Foundation licenses this file to you under the MIT license.

Imports System.IO.Pipes
Imports System.Runtime.CompilerServices
Imports System.Threading
Imports FluentAssertions
Imports Microsoft.VisualBasic.ApplicationServices

Imports Xunit

Namespace Microsoft.VisualBasic.Forms.Tests

Public Class SingleInstanceHelpersTests
Private _resultArgs As String()

<WinFormsFact>
Public Sub TryCreatePipeServerTests()
Dim pipeName As String = GetUniqueText()
Dim pipeServer As NamedPipeServerStream = Nothing
TryCreatePipeServer(pipeName, pipeServer).Should.BeTrue()
Using pipeServer
pipeServer.CanRead.Should.BeTrue()
pipeServer.CanSeek.Should.BeFalse()
pipeServer.CanWrite.Should.BeFalse()
pipeServer.TransmissionMode.Should.Be(PipeTransmissionMode.Byte)
End Using
End Sub

<WinFormsFact>
Public Sub TryCreatePipeServerTwiceTests_Fail()
Dim pipeName As String = GetUniqueText()
Dim pipeServer As NamedPipeServerStream = Nothing
TryCreatePipeServer(pipeName, pipeServer).Should.BeTrue()
Using pipeServer
Dim pipeServer1 As NamedPipeServerStream = Nothing
TryCreatePipeServer(pipeName, pipeServer1).Should.BeFalse()
pipeServer1.Should.BeNull()
End Using
End Sub

<WinFormsFact>
Public Async Function WaitForClientConnectionsAsyncTests() As Task
Dim pipeName As String = GetUniqueText()
Dim pipeServer As NamedPipeServerStream = Nothing

If TryCreatePipeServer(pipeName, pipeServer) Then

Using pipeServer
Dim tokenSource As New CancellationTokenSource()
Dim commandLine As String() = {"Hello"}
Dim clientConnection As Task = WaitForClientConnectionsAsync(
pipeServer,
callback:=Sub(args As String())
If args.Length = 1 Then
_resultArgs = commandLine
End If
End Sub,
cancellationToken:=tokenSource.Token)

Dim awaitable As ConfiguredTaskAwaitable = SendSecondInstanceArgsAsync(
pipeName,
args:=commandLine,
cancellationToken:=tokenSource.Token) _
.ConfigureAwait(continueOnCapturedContext:=False)

awaitable.GetAwaiter().GetResult()
Dim CancelToken As New CancellationToken
Dim buffer As Byte() = New Byte(commandLine.Length) {}
Dim count As Integer = Await pipeServer.ReadAsync(
buffer:=buffer.AsMemory(start:=0, length:=commandLine.Length))

' Ensure the result is set
Do
Await Task.Delay(5)
Loop Until _resultArgs IsNot Nothing
_resultArgs(0).Should.Be("Hello")
Await tokenSource.CancelAsync()
End Using
End If
End Function

End Class
End Namespace

0 comments on commit 3760721

Please sign in to comment.