Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
242 changes: 145 additions & 97 deletions AdvancedSharpAdbClient.Tests/AdbClientTests.Async.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public async Task GetVersionAsyncTest()
{
Version = new AdbCommandLineStatus(adbVersion, fileVersion, filePath)
};
AdbCommandLineStatus status = await commandLine.GetVersionAsync();
AdbCommandLineStatus status = await commandLine.GetVersionAsync(TestContext.Current.CancellationToken);
Assert.Equal(adbVersion, status.AdbVersion);
Assert.Equal(fileVersion, status.FileVersion);
Assert.Equal(filePath, status.FilePath);
Expand All @@ -36,7 +36,7 @@ public async Task GetVersionAsyncNullTest()
{
Version = default
};
_ = await Assert.ThrowsAsync<AdbException>(() => commandLine.GetVersionAsync());
_ = await Assert.ThrowsAsync<AdbException>(() => commandLine.GetVersionAsync(TestContext.Current.CancellationToken));
}

/// <summary>
Expand All @@ -49,7 +49,7 @@ public async Task GetOutdatedVersionAsyncTest()
{
Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.1"])
};
_ = await Assert.ThrowsAsync<AdbException>(() => commandLine.GetVersionAsync());
_ = await Assert.ThrowsAsync<AdbException>(() => commandLine.GetVersionAsync(TestContext.Current.CancellationToken));
}

/// <summary>
Expand All @@ -60,7 +60,7 @@ public async Task StartServerAsyncTest()
{
DummyAdbCommandLineClient commandLine = new();
Assert.False(commandLine.ServerStarted);
await commandLine.StartServerAsync();
await commandLine.StartServerAsync(TestContext.Current.CancellationToken);
Assert.True(commandLine.ServerStarted);
}
}
Expand Down
26 changes: 13 additions & 13 deletions AdvancedSharpAdbClient.Tests/AdbServerTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public async Task GetStatusAsyncNotRunningTest()

AdbServer adbServer = new(endPoint => adbSocketMock, adbCommandLineClientFactory);

AdbServerStatus status = await adbServer.GetStatusAsync();
AdbServerStatus status = await adbServer.GetStatusAsync(TestContext.Current.CancellationToken);
Assert.False(status.IsRunning);
Assert.Null(status.Version);
}
Expand All @@ -35,7 +35,7 @@ public async Task GetStatusAsyncRunningTest()
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0020");

AdbServerStatus status = await adbServer.GetStatusAsync();
AdbServerStatus status = await adbServer.GetStatusAsync(TestContext.Current.CancellationToken);

Assert.Empty(socket.Responses);
Assert.Empty(socket.ResponseMessages);
Expand All @@ -56,7 +56,7 @@ public async Task GetStatusAsyncOtherSocketExceptionTest()

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

_ = await Assert.ThrowsAsync<SocketException>(async () => await adbServer.GetStatusAsync());
_ = await Assert.ThrowsAsync<SocketException>(() => adbServer.GetStatusAsync(TestContext.Current.CancellationToken));
}

/// <summary>
Expand All @@ -69,7 +69,7 @@ public async Task GetStatusAsyncOtherExceptionTest()

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

_ = await Assert.ThrowsAsync<Exception>(async () => await adbServer.GetStatusAsync());
_ = await Assert.ThrowsAsync<Exception>(() => adbServer.GetStatusAsync(TestContext.Current.CancellationToken));
}

/// <summary>
Expand All @@ -82,7 +82,7 @@ public async Task StartServerAsyncAlreadyRunningTest()
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0020");

StartServerResult result = await adbServer.StartServerAsync(null, false);
StartServerResult result = await adbServer.StartServerAsync(null, false, TestContext.Current.CancellationToken);

Assert.Equal(StartServerResult.AlreadyRunning, result);

Expand All @@ -99,7 +99,7 @@ public async Task StartServerAsyncOutdatedRunningNoExecutableTest()
socket.Responses.Enqueue(AdbResponse.OK);
socket.ResponseMessages.Enqueue("0010");

AggregateException exception = await Assert.ThrowsAsync<AggregateException>(async () => await adbServer.StartServerAsync(null, false));
AggregateException exception = await Assert.ThrowsAsync<AggregateException>(() => adbServer.StartServerAsync(null, false, TestContext.Current.CancellationToken));
Assert.IsType<AdbException>(exception.InnerException);
}

Expand All @@ -113,7 +113,7 @@ public async Task StartServerAsyncNotRunningNoExecutableTest()

adbServer = new AdbServer(adbSocketFactory, adbCommandLineClientFactory);

AggregateException exception = await Assert.ThrowsAsync<AggregateException>(async () => await adbServer.StartServerAsync(null, false));
AggregateException exception = await Assert.ThrowsAsync<AggregateException>(() => adbServer.StartServerAsync(null, false, TestContext.Current.CancellationToken));
Assert.IsType<AdbException>(exception.InnerException);
}

Expand All @@ -129,7 +129,7 @@ public async Task StartServerAsyncOutdatedRunningTest()
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.32"]);

Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.StartServerAsync(ServerName, false);
_ = await adbServer.StartServerAsync(ServerName, false, TestContext.Current.CancellationToken);

Assert.True(commandLineClient.ServerStarted);

Expand All @@ -152,7 +152,7 @@ public async Task StartServerAsyncNotRunningTest()

Assert.False(commandLineClient.ServerStarted);

StartServerResult result = await adbServer.StartServerAsync(ServerName, false);
StartServerResult result = await adbServer.StartServerAsync(ServerName, false, TestContext.Current.CancellationToken);
Copy link

Copilot AI Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assignment to result is useless, since its value is never read.

Suggested change
StartServerResult result = await adbServer.StartServerAsync(ServerName, false, TestContext.Current.CancellationToken);
_ = await adbServer.StartServerAsync(ServerName, false, TestContext.Current.CancellationToken);

Copilot uses AI. Check for mistakes.

Assert.True(commandLineClient.ServerStarted);
}
Expand All @@ -169,7 +169,7 @@ public async Task StartServerAsyncIntermediateRestartRequestedRunningTest()
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.32"]);

Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.StartServerAsync(ServerName, true);
_ = await adbServer.StartServerAsync(ServerName, true, TestContext.Current.CancellationToken);

Assert.True(commandLineClient.ServerStarted);

Expand All @@ -190,7 +190,7 @@ public async Task StartServerAsyncIntermediateRestartNotRequestedRunningTest()
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.32"]);

Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.StartServerAsync(ServerName, false);
_ = await adbServer.StartServerAsync(ServerName, false, TestContext.Current.CancellationToken);

Assert.False(commandLineClient.ServerStarted);

Expand All @@ -210,7 +210,7 @@ public async Task RestartServerAsyncTest()
commandLineClient.Version = AdbCommandLineStatus.GetVersionFromOutput(["Android Debug Bridge version 1.0.32"]);

Assert.False(commandLineClient.ServerStarted);
_ = await adbServer.RestartServerAsync(ServerName);
_ = await adbServer.RestartServerAsync(ServerName, TestContext.Current.CancellationToken);

Assert.True(commandLineClient.ServerStarted);

Expand All @@ -225,7 +225,7 @@ public async Task RestartServerAsyncTest()
[Fact]
public async Task StopServerAsyncTest()
{
await adbServer.StopServerAsync();
await adbServer.StopServerAsync(TestContext.Current.CancellationToken);

Assert.Single(socket.Requests);
Assert.Equal("host:kill", socket.Requests[0]);
Expand Down
46 changes: 25 additions & 21 deletions AdvancedSharpAdbClient.Tests/AdbSocketTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,37 @@ public partial class AdbSocketTests
[Fact]
public async Task SendSyncDATARequestAsyncTest() =>
await RunTestAsync(
socket => socket.SendSyncRequestAsync(SyncCommand.DATA, 2, default),
[(byte)'D', (byte)'A', (byte)'T', (byte)'A', 2, 0, 0, 0]);
(socket, ctx) => socket.SendSyncRequestAsync(SyncCommand.DATA, 2, ctx),
[(byte)'D', (byte)'A', (byte)'T', (byte)'A', 2, 0, 0, 0],
TestContext.Current.CancellationToken);

/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequestAsync(SyncCommand, string, UnixFileStatus, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task SendSyncSENDRequestAsyncTest() =>
await RunTestAsync(
socket => socket.SendSyncRequestAsync(SyncCommand.SEND, "/test", UnixFileStatus.GroupMask | UnixFileStatus.StickyBit | UnixFileStatus.UserExecute | UnixFileStatus.OtherExecute, default),
[(byte)'S', (byte)'E', (byte)'N', (byte)'D', 9, 0, 0, 0, (byte)'/', (byte)'t', (byte)'e', (byte)'s', (byte)'t', (byte)',', (byte)'6', (byte)'3', (byte)'3']);
(socket, ctx) => socket.SendSyncRequestAsync(SyncCommand.SEND, "/test", UnixFileStatus.GroupMask | UnixFileStatus.StickyBit | UnixFileStatus.UserExecute | UnixFileStatus.OtherExecute, ctx),
[(byte)'S', (byte)'E', (byte)'N', (byte)'D', 9, 0, 0, 0, (byte)'/', (byte)'t', (byte)'e', (byte)'s', (byte)'t', (byte)',', (byte)'6', (byte)'3', (byte)'3'],
TestContext.Current.CancellationToken);

/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequestAsync(SyncCommand, string, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task SendSyncDENTRequestAsyncTest() =>
await RunTestAsync(
socket => socket.SendSyncRequestAsync(SyncCommand.DENT, "/data", default),
[(byte)'D', (byte)'E', (byte)'N', (byte)'T', 5, 0, 0, 0, (byte)'/', (byte)'d', (byte)'a', (byte)'t', (byte)'a']);
(socket, ctx) => socket.SendSyncRequestAsync(SyncCommand.DENT, "/data", ctx),
[(byte)'D', (byte)'E', (byte)'N', (byte)'T', 5, 0, 0, 0, (byte)'/', (byte)'d', (byte)'a', (byte)'t', (byte)'a'],
TestContext.Current.CancellationToken);

/// <summary>
/// Tests the <see cref="AdbSocket.SendSyncRequestAsync(SyncCommand, string, CancellationToken)"/> method.
/// </summary>
[Fact]
public async Task SendSyncNullRequestAsyncTest() =>
_ = await Assert.ThrowsAsync<ArgumentNullException>(() =>
RunTestAsync(socket => socket.SendSyncRequestAsync(SyncCommand.DATA, null, default), []));
RunTestAsync((socket, ctx) => socket.SendSyncRequestAsync(SyncCommand.DATA, null, ctx), [], TestContext.Current.CancellationToken));

/// <summary>
/// Tests the <see cref="AdbSocket.ReadSyncResponseAsync(CancellationToken)"/> method.
Expand All @@ -60,7 +63,7 @@ public async Task ReadSyncResponseAsync()

tcpSocket.InputStream.Position = 0;

Assert.Equal(SyncCommand.DENT, await socket.ReadSyncResponseAsync());
Assert.Equal(SyncCommand.DENT, await socket.ReadSyncResponseAsync(TestContext.Current.CancellationToken));
}

/// <summary>
Expand All @@ -81,7 +84,7 @@ public async Task ReadStringAsyncTest()

tcpSocket.InputStream.Position = 0;

Assert.Equal("Hello", await socket.ReadStringAsync());
Assert.Equal("Hello", await socket.ReadStringAsync(TestContext.Current.CancellationToken));
}

/// <summary>
Expand All @@ -103,7 +106,7 @@ public async Task ReadFailStringAsyncTest()

tcpSocket.InputStream.Position = 0;

Assert.Equal("Hello", await socket.ReadStringAsync());
Assert.Equal("Hello", await socket.ReadStringAsync(TestContext.Current.CancellationToken));
}

/// <summary>
Expand All @@ -124,7 +127,7 @@ public async Task ReadSyncStringAsyncTest()

tcpSocket.InputStream.Position = 0;

Assert.Equal("Hello", await socket.ReadSyncStringAsync());
Assert.Equal("Hello", await socket.ReadSyncStringAsync(TestContext.Current.CancellationToken));
}

/// <summary>
Expand All @@ -143,7 +146,7 @@ public async Task ReadAdbOkayResponseAsyncTest()

tcpSocket.InputStream.Position = 0;

AdbResponse response = await socket.ReadAdbResponseAsync();
AdbResponse response = await socket.ReadAdbResponseAsync(TestContext.Current.CancellationToken);
Assert.True(response.IOSuccess);
Assert.Equal(string.Empty, response.Message);
Assert.True(response.Okay);
Expand All @@ -168,7 +171,7 @@ public async Task ReadAdbFailResponseAsyncTest()

tcpSocket.InputStream.Position = 0;

_ = await Assert.ThrowsAsync<AdbException>(() => socket.ReadAdbResponseAsync());
_ = await Assert.ThrowsAsync<AdbException>(() => socket.ReadAdbResponseAsync(TestContext.Current.CancellationToken));
}

/// <summary>
Expand All @@ -187,13 +190,13 @@ public async Task ReadAsyncTest()
data[i] = (byte)i;
}

await tcpSocket.InputStream.WriteAsync(data);
await tcpSocket.InputStream.WriteAsync(data, TestContext.Current.CancellationToken);
tcpSocket.InputStream.Position = 0;

// Buffer has a capacity of 101, but we'll only want to read 100 bytes
byte[] received = new byte[101];

await socket.ReadAsync(received, 100);
await socket.ReadAsync(received, 100, TestContext.Current.CancellationToken);

for (int i = 0; i < 100; i++)
{
Expand All @@ -219,13 +222,13 @@ public async Task ReadAsyncMemoryTest()
data[i] = (byte)i;
}

await tcpSocket.InputStream.WriteAsync(data);
await tcpSocket.InputStream.WriteAsync(data, TestContext.Current.CancellationToken);
tcpSocket.InputStream.Position = 0;

// Buffer has a capacity of 101, but we'll only want to read 100 bytes
byte[] received = new byte[101];

await socket.ReadAsync(received.AsMemory(0, 100));
await socket.ReadAsync(received.AsMemory(0, 100), TestContext.Current.CancellationToken);

for (int i = 0; i < 100; i++)
{
Expand All @@ -241,16 +244,17 @@ public async Task ReadAsyncMemoryTest()
[Fact]
public async Task SendAdbRequestAsyncTest() =>
await RunTestAsync(
socket => socket.SendAdbRequestAsync("Test", default),
"0004Test"u8.ToArray());
(socket, ctx) => socket.SendAdbRequestAsync("Test", ctx),
"0004Test"u8.ToArray(),
TestContext.Current.CancellationToken);

private static async Task RunTestAsync(Func<IAdbSocket, Task> test, byte[] expectedDataSent)
private static async Task RunTestAsync(Func<IAdbSocket, CancellationToken, Task> test, byte[] expectedDataSent, CancellationToken cancellationToken = default)
{
using DummyTcpSocket tcpSocket = new();
using AdbSocket socket = new(tcpSocket);

// Run the test.
await test(socket);
await test(socket, cancellationToken);

// Validate the data that was sent over the wire.
Assert.Equal(expectedDataSent, tcpSocket.GetBytesSent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
<PackageReference Include="NSubstitute" Version="5.3.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" PrivateAssets="all">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit.v3" Version="3.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading