Skip to content

Commit

Permalink
Add IsProcessing to SyncService
Browse files Browse the repository at this point in the history
  • Loading branch information
wherewhere committed Mar 18, 2024
1 parent b9b5260 commit ba24d53
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 28 deletions.
63 changes: 60 additions & 3 deletions AdvancedSharpAdbClient.Tests/SyncServiceTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public async void StatAsyncTest()
async () =>
{
using SyncService service = new(Socket, Device);
return await service.StatAsync("/fstab.donatello");
FileStatistics value = await service.StatAsync("/fstab.donatello");
Assert.False(service.IsProcessing);
Assert.False(service.IsOutdate);
return value;
});

Assert.Equal(UnixFileStatus.Regular, value.FileMode.GetFileType());
Expand Down Expand Up @@ -56,7 +59,10 @@ public async void GetListingAsyncTest()
async () =>
{
using SyncService service = new(Socket, Device);
return await service.GetDirectoryListingAsync("/storage");
List<FileStatistics> value = await service.GetDirectoryListingAsync("/storage");
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
return value;
});

Assert.Equal(4, value.Count);
Expand Down Expand Up @@ -110,7 +116,10 @@ public async void GetAsyncListingTest()
async () =>
{
using SyncService service = new(Socket, Device);
return await service.GetDirectoryAsyncListing("/storage").ToListAsync();
List<FileStatistics> value = await service.GetDirectoryAsyncListing("/storage").ToListAsync();
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
return value;
});

Assert.Equal(4, value.Count);
Expand Down Expand Up @@ -171,6 +180,8 @@ await RunTestAsync(
{
using SyncService service = new(Socket, Device);
await service.PullAsync("/fstab.donatello", stream, null);
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
});

// Make sure the data that has been sent to the stream is the expected data
Expand Down Expand Up @@ -207,6 +218,48 @@ await RunTestAsync(
{
using SyncService service = new(Socket, Device);
await service.PushAsync(stream, "/sdcard/test", UnixFileStatus.StickyBit | UnixFileStatus.UserWrite | UnixFileStatus.OtherRead, new DateTime(2015, 11, 2, 23, 0, 0, DateTimeKind.Utc), null);
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
});
}

/// <summary>
/// Tests the <see cref="SyncService.IsProcessing"/> field.
/// </summary>
[Fact]
public async void IsProcessingAsyncTest()
{
await RunTestAsync(
OkResponses(2),
[".", "..", "sdcard0", "emulated"],
["host:transport:169.254.109.177:5555", "sync:"],
[(SyncCommand.LIST, "/storage")],
[SyncCommand.DENT, SyncCommand.DENT, SyncCommand.DENT, SyncCommand.DENT, SyncCommand.DONE],
[
[233, 65, 0, 0, 0, 0, 0, 0, 152, 130, 56, 86],
[237, 65, 0, 0, 0, 0, 0, 0, 152, 130, 56, 86],
[255, 161, 0, 0, 24, 0, 0, 0, 152, 130, 56, 86],
[109, 65, 0, 0, 0, 0, 0, 0, 152, 130, 56, 86]
],
null,
async () =>
{
using SyncService service = new(Socket, Device);
await foreach (FileStatistics stat in service.GetDirectoryAsyncListing("/storage"))
{
Assert.False(service.IsOutdate);
Assert.True(service.IsProcessing);
_ = await Assert.ThrowsAsync<InvalidOperationException>(() => service.PushAsync((Stream)null, null, default, default));
_ = await Assert.ThrowsAsync<InvalidOperationException>(() => service.PullAsync(null, (Stream)null));
#if WINDOWS10_0_17763_0_OR_GREATER
_ = await Assert.ThrowsAsync<InvalidOperationException>(() => service.PushAsync((IInputStream)null, null, default, default));
_ = await Assert.ThrowsAsync<InvalidOperationException>(() => service.PullAsync(null, (IOutputStream)null));
#endif
_ = await Assert.ThrowsAsync<InvalidOperationException>(() => service.GetDirectoryListingAsync(null));
_ = await Assert.ThrowsAsync<InvalidOperationException>(() => service.GetDirectoryAsyncListing(null).ToListAsync().AsTask());
}
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
});
}

Expand Down Expand Up @@ -240,6 +293,8 @@ await RunTestAsync(
{
using SyncService service = new(Socket, Device);
await service.PullAsync("/fstab.donatello", stream, null);
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
});

IBuffer buffer = await stream.GetInputStreamAt(0).ReadAsync(new byte[(int)stream.Size].AsBuffer(), (uint)stream.Size, InputStreamOptions.None);
Expand Down Expand Up @@ -278,6 +333,8 @@ await RunTestAsync(
{
using SyncService service = new(Socket, Device);
await service.PushAsync(stream, "/sdcard/test", (UnixFileStatus)644, new DateTime(2015, 11, 2, 23, 0, 0, DateTimeKind.Utc), null);
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
});
}
#endif
Expand Down
49 changes: 47 additions & 2 deletions AdvancedSharpAdbClient.Tests/SyncServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public void StatTest()
() =>
{
using SyncService service = new(Socket, Device);
return service.Stat("/fstab.donatello");
FileStatistics value = service.Stat("/fstab.donatello");
Assert.False(service.IsProcessing);
Assert.False(service.IsOutdate);
return value;
});

Assert.Equal(UnixFileStatus.Regular, value.FileMode.GetFileType());
Expand Down Expand Up @@ -66,7 +69,10 @@ public void GetListingTest()
() =>
{
using SyncService service = new(Socket, Device);
return service.GetDirectoryListing("/storage").ToArray();
FileStatistics[] value = service.GetDirectoryListing("/storage").ToArray();
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
return value;
});

Assert.Equal(4, value.Length);
Expand Down Expand Up @@ -127,6 +133,8 @@ public void PullTest()
{
using SyncService service = new(Socket, Device);
service.Pull("/fstab.donatello", stream);
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
});

// Make sure the data that has been sent to the stream is the expected data
Expand Down Expand Up @@ -163,6 +171,43 @@ .. BitConverter.GetBytes(content.Length),
{
using SyncService service = new(Socket, Device);
service.Push(stream, "/sdcard/test", (UnixFileStatus)644, new DateTime(2015, 11, 2, 23, 0, 0, DateTimeKind.Utc));
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
});
}

/// <summary>
/// Tests the <see cref="SyncService.IsProcessing"/> field.
/// </summary>
[Fact]
public void IsProcessingTest()
{
RunTest(
OkResponses(2),
[".", "..", "sdcard0", "emulated"],
["host:transport:169.254.109.177:5555", "sync:"],
[(SyncCommand.LIST, "/storage")],
[SyncCommand.DENT, SyncCommand.DENT, SyncCommand.DENT, SyncCommand.DENT, SyncCommand.DONE],
[
[233, 65, 0, 0, 0, 0, 0, 0, 152, 130, 56, 86],
[237, 65, 0, 0, 0, 0, 0, 0, 152, 130, 56, 86],
[255, 161, 0, 0, 24, 0, 0, 0, 152, 130, 56, 86],
[109, 65, 0, 0, 0, 0, 0, 0, 152, 130, 56, 86]
],
null,
() =>
{
using SyncService service = new(Socket, Device);
foreach (FileStatistics stat in service.GetDirectoryListing("/storage"))
{
Assert.False(service.IsOutdate);
Assert.True(service.IsProcessing);
_ = Assert.Throws<InvalidOperationException>(() => service.Push(null, null, default, default));
_ = Assert.Throws<InvalidOperationException>(() => service.Pull(null, null));
_ = Assert.Throws<InvalidOperationException>(() => service.GetDirectoryListing(null).FirstOrDefault());
}
Assert.False(service.IsProcessing);
Assert.True(service.IsOutdate);
});
}
}
Expand Down
16 changes: 11 additions & 5 deletions AdvancedSharpAdbClient/AdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace AdvancedSharpAdbClient
[DebuggerDisplay($"{nameof(AdbClient)} \\{{ {nameof(EndPoint)} = {{{nameof(EndPoint)}}} }}")]
public partial class AdbClient : IAdbClient, ICloneable<IAdbClient>, ICloneable
#if WINDOWS_UWP || WINDOWS10_0_17763_0_OR_GREATER
, IAdbClient.IWinRT
, IAdbClient.IWinRT, ICloneable<IAdbClient.IWinRT>
#endif
{
/// <summary>
Expand Down Expand Up @@ -1133,14 +1133,20 @@ public IEnumerable<string> GetFeatureSet(DeviceData device)
public override string ToString() => $"The {nameof(AdbClient)} communicate with adb server at {EndPoint}";

/// <summary>
/// Creates a new <see cref="AdbClient"/> object that is a copy of the current instance with new <see cref="EndPoint"/>.
/// Creates a new <see cref="IAdbClient"/> object that is a copy of the current instance with new <see cref="EndPoint"/>.
/// </summary>
/// <param name="endPoint">The new <see cref="EndPoint"/> to use.</param>
/// <returns>A new <see cref="AdbClient"/> object that is a copy of this instance with new <see cref="EndPoint"/>.</returns>
public AdbClient Clone(EndPoint endPoint) => new(endPoint, AdbSocketFactory);
/// <returns>A new <see cref="IAdbClient"/> object that is a copy of this instance with new <see cref="EndPoint"/>.</returns>
public virtual IAdbClient Clone(EndPoint endPoint) => new AdbClient(endPoint, AdbSocketFactory);

/// <inheritdoc/>
public IAdbClient Clone() => new AdbClient(EndPoint, AdbSocketFactory);
public IAdbClient Clone() => Clone(EndPoint);

#if WINDOWS_UWP || WINDOWS10_0_17763_0_OR_GREATER
/// <inheritdoc/>
IAdbClient.IWinRT ICloneable<IAdbClient.IWinRT>.Clone() => Clone(EndPoint) is IAdbClient.IWinRT client ? client
: throw new NotSupportedException($"The {nameof(Clone)} method does not return a {nameof(IAdbClient.IWinRT)} object.");
#endif

/// <inheritdoc/>
object ICloneable.Clone() => Clone();
Expand Down
10 changes: 5 additions & 5 deletions AdvancedSharpAdbClient/AdbServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,16 @@ public AdbServerStatus GetStatus()
public override string ToString() => $"The {nameof(AdbServer)} communicate with adb at {EndPoint}";

/// <summary>
/// Creates a new <see cref="AdbServer"/> object that is a copy of the current instance with new <see cref="EndPoint"/>.
/// Creates a new <see cref="IAdbServer"/> object that is a copy of the current instance with new <see cref="EndPoint"/>.
/// </summary>
/// <param name="endPoint">The new <see cref="EndPoint"/> to use.</param>
/// <returns>A new <see cref="AdbServer"/> object that is a copy of this instance with new <see cref="EndPoint"/>.</returns>
public AdbServer Clone(EndPoint endPoint) => new(endPoint, AdbSocketFactory, AdbCommandLineClientFactory);
/// <returns>A new <see cref="IAdbServer"/> object that is a copy of this instance with new <see cref="EndPoint"/>.</returns>
public virtual IAdbServer Clone(EndPoint endPoint) => new AdbServer(endPoint, AdbSocketFactory, AdbCommandLineClientFactory);

/// <inheritdoc/>
public IAdbServer Clone() => new AdbServer(EndPoint, AdbSocketFactory, AdbCommandLineClientFactory);
public IAdbServer Clone() => Clone(EndPoint);

/// <inheritdoc/>
object ICloneable.Clone() => Clone();
object ICloneable.Clone() => Clone(EndPoint);
}
}
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient/AdbSocket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ public override string ToString() =>
public void Close() => Socket.Dispose();

/// <inheritdoc/>
public IAdbSocket Clone()
public virtual IAdbSocket Clone()
{
if (Socket is not ICloneable<ITcpSocket> cloneable)
{
Expand Down
2 changes: 1 addition & 1 deletion AdvancedSharpAdbClient/DeviceMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public override string ToString() =>
.ToString();

/// <inheritdoc/>
public IDeviceMonitor Clone() =>
public virtual IDeviceMonitor Clone() =>
Socket is not ICloneable<IAdbSocket> cloneable
? throw new NotSupportedException($"{Socket.GetType()} does not support cloning.")
: new DeviceMonitor(cloneable.Clone(), logger);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public static string ToPermissionCode(this UnixFileStatus mode)
#else
char[] code = new char[10];
#endif
BitArray array = new([(int)mode]);
BitArray array = new(new[] { (int)mode });

code[9] = array[0]
? array[9] ? 't' : 'x'
Expand Down
Loading

0 comments on commit ba24d53

Please sign in to comment.