Skip to content

Commit

Permalink
implement and expose ISmartDevice.EndPoint
Browse files Browse the repository at this point in the history
  • Loading branch information
smdn committed Jan 18, 2024
1 parent 997d1ab commit 0741005
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ protected readonly struct NullParameter { }

private IDeviceEndPoint deviceEndPoint; // if null, it indicates a 'disposed' state.

/// <summary>
/// Gets the <see cref="IDeviceEndPoint"/> representing a endpoint of smart device,
/// which will be used to resolve the actual endpoint used to communicate with smart devices.
/// </summary>
/// <exception cref="ObjectDisposedException">The device has been disposed.</exception>
public IDeviceEndPoint EndPoint {
get {
ThrowIfDisposed();

return deviceEndPoint;
}
}

#if SYSTEM_DIAGNOSTICS_CODEANALYSIS_MEMBERNOTNULLWHENATTRIBUTE
[MemberNotNullWhen(false, nameof(deviceEndPoint))]
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ public LoggerScopeEndPointState(EndPoint currentEndPoint, IDeviceEndPoint device

private IDeviceEndPoint deviceEndPoint; // if null, it indicates a 'disposed' state.

/// <summary>
/// Gets the <see cref="IDeviceEndPoint"/> representing a endpoint of smart device,
/// which will be used to resolve the actual endpoint used to communicate with smart devices.
/// </summary>
/// <exception cref="ObjectDisposedException">The device has been disposed.</exception>
public IDeviceEndPoint EndPoint {
get {
ThrowIfDisposed();

return deviceEndPoint;
}
}

#if SYSTEM_DIAGNOSTICS_CODEANALYSIS_MEMBERNOTNULLWHENATTRIBUTE
[MemberNotNullWhen(false, nameof(deviceEndPoint))]
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public async Task Create_WithIPAddress()
ipAddress: IPAddress.Loopback
);

Assert.That(device.EndPoint, Is.Not.Null);
Assert.That(
await device.ResolveEndPointAsync(),
Is.EqualTo(new IPEndPoint(IPAddress.Loopback, KasaClient.DefaultPort))
Expand All @@ -65,6 +66,7 @@ public async Task Create_WithHostName()
host: "localhost"
);

Assert.That(device.EndPoint, Is.Not.Null);
Assert.That(
await device.ResolveEndPointAsync(),
Is.EqualTo(new DnsEndPoint("localhost", KasaClient.DefaultPort))
Expand All @@ -85,6 +87,7 @@ public async Task Create_WithMacAddress_IServiceProvider()
serviceProvider: services.BuildServiceProvider()
);

Assert.That(device.EndPoint, Is.Not.Null);
Assert.That(
await device.ResolveEndPointAsync(),
Is.EqualTo(new IPEndPoint(IPAddress.Loopback, KasaClient.DefaultPort))
Expand All @@ -107,10 +110,13 @@ public void Create_WithMacAddress_IServiceProvider_IDeviceEndPointFactoryNotRegi
[Test]
public async Task Create_WithEndPoint()
{
var endpoint = new StaticDeviceEndPoint(new DnsEndPoint("localhost", 0));

using var device = KasaDevice.Create(
deviceEndPoint: new StaticDeviceEndPoint(new DnsEndPoint("localhost", 0))
deviceEndPoint: endpoint
);

Assert.That(device.EndPoint, Is.EqualTo(endpoint));
Assert.That(
await device.ResolveEndPointAsync(),
Is.EqualTo(new DnsEndPoint("localhost", KasaClient.DefaultPort))
Expand All @@ -127,6 +133,7 @@ public void Dispose()
Assert.DoesNotThrow(device.Dispose, "dispose");
Assert.DoesNotThrow(device.Dispose, "dispose again");

Assert.Throws<ObjectDisposedException>(() => Assert.That(device.EndPoint, Is.Not.Null), nameof(device.EndPoint));
Assert.Throws<ObjectDisposedException>(() => Assert.That(device.IsConnected, Is.False), nameof(device.IsConnected));

Assert.ThrowsAsync<ObjectDisposedException>(async () => await device.ResolveEndPointAsync(), nameof(device.ResolveEndPointAsync));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ public async Task Create_WithIPAddress()
"password"
);

Assert.That(device.EndPoint, Is.Not.Null);
Assert.That(
await device.ResolveEndPointAsync(),
Is.EqualTo(new IPEndPoint(IPAddress.Loopback, TapoClient.DefaultPort))
Expand All @@ -144,6 +145,7 @@ public async Task Create_WithHostName()
"password"
);

Assert.That(device.EndPoint, Is.Not.Null);
Assert.That(
await device.ResolveEndPointAsync(),
Is.EqualTo(new DnsEndPoint("localhost", TapoClient.DefaultPort))
Expand All @@ -166,6 +168,7 @@ public async Task Create_WithMacAddress_IServiceProvider()
serviceProvider: services!.BuildServiceProvider()
);

Assert.That(device.EndPoint, Is.Not.Null);
Assert.That(
await device.ResolveEndPointAsync(),
Is.EqualTo(new IPEndPoint(IPAddress.Loopback, TapoClient.DefaultPort))
Expand All @@ -190,11 +193,14 @@ public void Create_WithMacAddress_IServiceProvider_IDeviceEndPointFactoryNotRegi
[Test]
public async Task Create_WithEndPoint()
{
var endpoint = new StaticDeviceEndPoint(new DnsEndPoint("localhost", 0));

using var device = TapoDevice.Create(
deviceEndPoint: new StaticDeviceEndPoint(new DnsEndPoint("localhost", 0)),
deviceEndPoint: endpoint,
serviceProvider: services!.BuildServiceProvider()
);

Assert.That(device.EndPoint, Is.EqualTo(endpoint));
Assert.That(
await device.ResolveEndPointAsync(),
Is.EqualTo(new DnsEndPoint("localhost", TapoClient.DefaultPort))
Expand Down Expand Up @@ -249,6 +255,8 @@ public void Dispose()

Assert.That(device.Session, Is.Null, nameof(device.Session));

Assert.Throws<ObjectDisposedException>(() => Assert.That(device.EndPoint, Is.Not.Null), nameof(device.EndPoint));

#pragma warning disable CA2012
Assert.ThrowsAsync<ObjectDisposedException>(async () => await device.ResolveEndPointAsync(), nameof(device.ResolveEndPointAsync));
Assert.Throws<ObjectDisposedException>(() => device.ResolveEndPointAsync(), nameof(device.ResolveEndPointAsync));
Expand Down

0 comments on commit 0741005

Please sign in to comment.