Skip to content

Commit 59923ce

Browse files
committed
Added tests and some slashdocs for ForwardedPortStatus.
1 parent 532dac4 commit 59923ce

File tree

8 files changed

+1061
-16
lines changed

8 files changed

+1061
-16
lines changed

src/Renci.SshNet.Tests.NET35/Renci.SshNet.Tests.NET35.csproj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@
6565
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\Abstractions\ThreadAbstraction_ExecuteThread.cs">
6666
<Link>Classes\Abstraction\ThreadAbstraction_ExecuteThread.cs</Link>
6767
</Compile>
68+
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\ForwardedPortStatusTest_Started.cs">
69+
<Link>Classes\ForwardedPortStatusTest_Started.cs</Link>
70+
</Compile>
71+
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\ForwardedPortStatusTest_Starting.cs">
72+
<Link>Classes\ForwardedPortStatusTest_Starting.cs</Link>
73+
</Compile>
74+
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\ForwardedPortStatusTest_Stopped.cs">
75+
<Link>Classes\ForwardedPortStatusTest_Stopped.cs</Link>
76+
</Compile>
77+
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\ForwardedPortStatusTest_Stopping.cs">
78+
<Link>Classes\ForwardedPortStatusTest_Stopping.cs</Link>
79+
</Compile>
6880
<Compile Include="..\Renci.SshNet.Tests\Classes\Channels\ChannelDirectTcpipTest.cs">
6981
<Link>Classes\Channels\ChannelDirectTcpipTest.cs</Link>
7082
</Compile>
@@ -1286,7 +1298,7 @@
12861298
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
12871299
<ProjectExtensions>
12881300
<VisualStudio>
1289-
<UserProperties ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" ProjectLinkReference="c45379b9-17b1-4e89-bc2e-6d41726413e8" />
1301+
<UserProperties ProjectLinkReference="c45379b9-17b1-4e89-bc2e-6d41726413e8" ProjectLinkerExcludeFilter="\\?desktop(\\.*)?$;\\?silverlight(\\.*)?$;\.desktop;\.silverlight;\.xaml;^service references(\\.*)?$;\.clientconfig;^web references(\\.*)?$" />
12901302
</VisualStudio>
12911303
</ProjectExtensions>
12921304
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

src/Renci.SshNet.Tests/Renci.SshNet.Tests.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@
7373
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\Abstractions\ThreadAbstraction_ExecuteThread.cs">
7474
<Link>Classes\Abstractions\ThreadAbstraction_ExecuteThread.cs</Link>
7575
</Compile>
76+
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\ForwardedPortStatusTest_Started.cs">
77+
<Link>Classes\ForwardedPortStatusTest_Started.cs</Link>
78+
</Compile>
79+
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\ForwardedPortStatusTest_Starting.cs">
80+
<Link>Classes\ForwardedPortStatusTest_Starting.cs</Link>
81+
</Compile>
82+
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\ForwardedPortStatusTest_Stopped.cs">
83+
<Link>Classes\ForwardedPortStatusTest_Stopped.cs</Link>
84+
</Compile>
85+
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\ForwardedPortStatusTest_Stopping.cs">
86+
<Link>Classes\ForwardedPortStatusTest_Stopping.cs</Link>
87+
</Compile>
7688
<Compile Include="..\..\test\Renci.SshNet.Shared.Tests\SshMessageFactoryTest.cs">
7789
<Link>Classes\SshMessageFactoryTest.cs</Link>
7890
</Compile>

src/Renci.SshNet/ForwardedPortStatus.cs

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Threading;
34

45
namespace Renci.SshNet
@@ -19,36 +20,36 @@ private ForwardedPortStatus(int value, string name)
1920
_name = name;
2021
}
2122

22-
public override bool Equals(object obj)
23+
public override bool Equals(object other)
2324
{
24-
if (ReferenceEquals(obj, null))
25+
if (ReferenceEquals(other, null))
2526
return false;
2627

27-
if (ReferenceEquals(this, obj))
28+
if (ReferenceEquals(this, other))
2829
return true;
2930

30-
var forwardedPortStatus = obj as ForwardedPortStatus;
31+
var forwardedPortStatus = other as ForwardedPortStatus;
3132
if (forwardedPortStatus == null)
3233
return false;
3334

3435
return forwardedPortStatus._value == _value;
3536
}
3637

37-
public static bool operator ==(ForwardedPortStatus c1, ForwardedPortStatus c2)
38+
public static bool operator ==(ForwardedPortStatus left, ForwardedPortStatus right)
3839
{
3940
// check if lhs is null
40-
if (ReferenceEquals(c1, null))
41+
if (ReferenceEquals(left, null))
4142
{
4243
// check if both lhs and rhs are null
43-
return (ReferenceEquals(c2, null));
44+
return (ReferenceEquals(right, null));
4445
}
4546

46-
return c1.Equals(c2);
47+
return left.Equals(right);
4748
}
4849

49-
public static bool operator !=(ForwardedPortStatus c1, ForwardedPortStatus c2)
50+
public static bool operator !=(ForwardedPortStatus left, ForwardedPortStatus right)
5051
{
51-
return !(c1==c2);
52+
return !(left==right);
5253
}
5354

5455
public override int GetHashCode()
@@ -61,13 +62,25 @@ public override string ToString()
6162
return _name;
6263
}
6364

65+
/// <summary>
66+
/// Returns a value indicating whether <paramref name="status"/> has been changed to <see cref="Stopping"/>.
67+
/// </summary>
68+
/// <param name="status">The status to transition from.</param>
69+
/// <returns>
70+
/// <c>true</c> if <paramref name="status"/> has been changed to <see cref="Stopping"/>; otherwise, <c>false</c>.
71+
/// </returns>
72+
/// <exception cref="InvalidOperationException">Cannot transition <paramref name="status"/> to <see cref="Stopping"/>.</exception>
73+
/// <remarks>
74+
/// While a transition from <see cref="Stopped"/> to <see cref="Stopping"/> is not possible, this method will
75+
/// return <c>false</c> for any such attempts. This is related to concurrency.
76+
/// </remarks>
6477
public static bool ToStopping(ref ForwardedPortStatus status)
6578
{
6679
// attempt to transition from Started to Stopping
6780
var previousStatus = Interlocked.CompareExchange(ref status, Stopping, Started);
6881
if (previousStatus == Stopping || previousStatus == Stopped)
6982
{
70-
// status is already Stopping or Stopped, so no transition to Stopped is necessary
83+
// status is already Stopping or Stopped, so no transition to Stopping is necessary
7184
return false;
7285
}
7386

@@ -79,14 +92,32 @@ public static bool ToStopping(ref ForwardedPortStatus status)
7992
previousStatus = Interlocked.CompareExchange(ref status, Stopping, Starting);
8093
if (previousStatus == Stopping || previousStatus == Stopped)
8194
{
82-
// status is already Stopping or Stopped, so no transition to Stopped is necessary
95+
// status is already Stopping or Stopped, so no transition to Stopping is necessary
8396
return false;
8497
}
8598

8699
// we've successfully transitioned from Starting to Stopping
87-
return status == Stopping;
100+
if (status == Stopping)
101+
return true;
102+
103+
// there's no valid transition from status to Stopping
104+
throw new InvalidOperationException(string.Format("Forwarded port cannot transition from '{0}' to '{1}'.",
105+
previousStatus,
106+
Stopping));
88107
}
89108

109+
/// <summary>
110+
/// Returns a value indicating whether <paramref name="status"/> has been changed to <see cref="Starting"/>.
111+
/// </summary>
112+
/// <param name="status">The status to transition from.</param>
113+
/// <returns>
114+
/// <c>true</c> if <paramref name="status"/> has been changed to <see cref="Starting"/>; otherwise, <c>false</c>.
115+
/// </returns>
116+
/// <exception cref="InvalidOperationException">Cannot transition <paramref name="status"/> to <see cref="Starting"/>.</exception>
117+
/// <remarks>
118+
/// While a transition from <see cref="Started"/> to <see cref="Starting"/> is not possible, this method will
119+
/// return <c>false</c> for any such attempts. This is related to concurrency.
120+
/// </remarks>
90121
public static bool ToStarting(ref ForwardedPortStatus status)
91122
{
92123
// attemp to transition from Stopped to Starting
@@ -101,8 +132,10 @@ public static bool ToStarting(ref ForwardedPortStatus status)
101132
if (status == Starting)
102133
return true;
103134

104-
// there's no valid transition from Stopping to Starting
105-
throw new InvalidOperationException("Forwarded port is stopping.");
135+
// there's no valid transition from status to Starting
136+
throw new InvalidOperationException(string.Format("Forwarded port cannot transition from '{0}' to '{1}'.",
137+
previousStatus,
138+
Starting));
106139
}
107140
}
108141
}

0 commit comments

Comments
 (0)