Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for SingleInstanceHelper and fix formatting from simplifying PR #11863 #12261

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
4c61c63
No code Chnages just formatting
paul1956 Oct 2, 2024
a4224c5
Add tests for SignleInstanceHelper from #11863
paul1956 Oct 2, 2024
2b5f268
Moved to SDK RC2 (#12254)
Tanya-Solyanik Oct 3, 2024
1bcf239
Adds XML Comments to FileSystemProxy and SpecialDirectoriesProxy (#12…
paul1956 Oct 3, 2024
b4f93e3
[main] Update dependencies from dotnet/runtime (#12266)
dotnet-maestro[bot] Oct 3, 2024
756e030
Fix IDE0002 in LogTests.cs (#12265)
paul1956 Oct 3, 2024
0267546
[main] Update dependencies from dotnet/arcade (#12271)
dotnet-maestro[bot] Oct 4, 2024
d049e5c
[main] Update dependencies from dotnet/runtime (#12272)
dotnet-maestro[bot] Oct 4, 2024
d90caed
Improve code coverage for FileLogTraceListener from simplifying PR #1…
paul1956 Oct 4, 2024
a629958
Switch default feed to use wildcards only (#12268)
richlander Oct 4, 2024
03cb5dd
Move local closer to usage
paul1956 Oct 4, 2024
b487081
Merge branch 'master' into FIx-Formatting-in-SingleInstanceHelper-Fro…
paul1956 Oct 4, 2024
6887976
Merge branch 'master' into FIx-Formatting-in-SingleInstanceHelper-Fro…
paul1956 Oct 7, 2024
0119ea3
PR Feedback
paul1956 Oct 7, 2024
a15c1ee
Merge branch 'master' into FIx-Formatting-in-SingleInstanceHelper-Fro…
paul1956 Oct 9, 2024
a82f402
Address PR Feedback
paul1956 Oct 9, 2024
a278a8b
PR Feedback
paul1956 Oct 9, 2024
ef78e8c
PR Feedback inline private Sub, reuse commandLine
paul1956 Oct 10, 2024
02c975c
Merge branch 'master' into FIx-Formatting-in-SingleInstanceHelper-Fro…
paul1956 Oct 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
paul1956 marked this conversation as resolved.
Show resolved Hide resolved
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)
paul1956 marked this conversation as resolved.
Show resolved Hide resolved
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)
paul1956 marked this conversation as resolved.
Show resolved Hide resolved
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,85 @@
' 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()

Private Sub OnStartupNextInstanceMarshallingAdaptor(args As String())
If args.Length = 1 Then
_resultArgs = {"Hello"}
End If
End Sub
paul1956 marked this conversation as resolved.
Show resolved Hide resolved

<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()
Dim pipeServer1 As NamedPipeServerStream = Nothing
TryCreatePipeServer(pipeName, pipeServer1).Should.BeFalse()
Using pipeServer
paul1956 marked this conversation as resolved.
Show resolved Hide resolved
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 clientConnection As Task = WaitForClientConnectionsAsync(
pipeServer,
callback:=AddressOf OnStartupNextInstanceMarshallingAdaptor,
cancellationToken:=tokenSource.Token)

Dim commandLine As String() = {"Hello"}
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