Skip to content

Commit

Permalink
Improve DeviceMonitor
Browse files Browse the repository at this point in the history
Add AggressiveInlining on single line methods
Use HashCode to create hash code
  • Loading branch information
wherewhere committed Oct 10, 2023
1 parent 2f9fbb8 commit ffb82f8
Show file tree
Hide file tree
Showing 53 changed files with 1,030 additions and 128 deletions.
27 changes: 20 additions & 7 deletions AdvancedSharpAdbClient.Tests/DeviceMonitorTests.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ await RunTestAsync(
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);

Socket.ResponseMessages.Clear();
Socket.Responses.Clear();
Socket.Requests.Clear();

sink.ResetSignals();

// Device disconnects
ManualResetEvent eventWaiter = sink.CreateEventSignal();

Expand All @@ -43,9 +46,10 @@ await RunTestAsync(
() => _ = eventWaiter.WaitOne(1000));

Assert.Empty(monitor.Devices);
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Single(sink.DisconnectedEvents);
Assert.Equal("169.254.109.177:5555", sink.DisconnectedEvents[0].Device.Serial);
}
Expand All @@ -70,13 +74,16 @@ await RunTestAsync(
Assert.Empty(monitor.Devices);
Assert.Empty(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Empty(sink.NotifiedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Empty(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);

Socket.ResponseMessages.Clear();
Socket.Responses.Clear();
Socket.Requests.Clear();

sink.ResetSignals();

// Device disconnects
ManualResetEvent eventWaiter = sink.CreateEventSignal();

Expand All @@ -90,6 +97,7 @@ await RunTestAsync(
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);
Assert.Equal("169.254.109.177:5555", sink.ConnectedEvents[0].Device.Serial);
}
Expand All @@ -114,16 +122,17 @@ await RunTestAsync(
() => monitor.StartAsync());

Assert.Single(monitor.Devices);
Assert.Equal("169.254.109.177:5555", monitor.Devices.ElementAt(0).Serial);
Assert.Equal("169.254.109.177:5555", monitor.Devices[0].Serial);
Assert.Single(sink.ConnectedEvents);
Assert.Equal("169.254.109.177:5555", sink.ConnectedEvents[0].Device.Serial);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);
}

[Fact]
public async void DeviceChanged_TriggeredWhenStatusChangedAsyncTest()
public async void TriggeredWhenStatusChangedAsyncTest()
{
Socket.WaitForNewData = true;

Expand All @@ -140,10 +149,11 @@ await RunTestAsync(
() => monitor.StartAsync());

Assert.Single(monitor.Devices);
Assert.Equal(DeviceState.Offline, monitor.Devices.ElementAt(0).State);
Assert.Equal(DeviceState.Offline, monitor.Devices[0].State);
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);

Socket.ResponseMessages.Clear();
Expand All @@ -162,16 +172,17 @@ await RunTestAsync(
() => eventWaiter.WaitOne(1000));

Assert.Single(monitor.Devices);
Assert.Equal(DeviceState.Online, monitor.Devices.ElementAt(0).State);
Assert.Equal(DeviceState.Online, monitor.Devices[0].State);
Assert.Empty(sink.ConnectedEvents);
Assert.Single(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);
Assert.Equal("169.254.109.177:5555", sink.ChangedEvents[0].Device.Serial);
}

[Fact]
public async void DeviceChanged_NoTriggerIfStatusIsSameAsyncTest()
public async void NoTriggerIfStatusIsSameAsyncTest()
{
Socket.WaitForNewData = true;

Expand All @@ -192,6 +203,7 @@ await RunTestAsync(
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);

Socket.ResponseMessages.Clear();
Expand All @@ -214,6 +226,7 @@ await RunTestAsync(
Assert.Empty(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Empty(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);
}

Expand Down
26 changes: 20 additions & 6 deletions AdvancedSharpAdbClient.Tests/DeviceMonitorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AdvancedSharpAdbClient.Logs;
using System;
using System.Linq;
using System.Threading;
using Xunit;

Expand Down Expand Up @@ -58,8 +59,11 @@ public void DeviceDisconnectedTest()
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);

sink.ResetSignals();

Socket.ResponseMessages.Clear();
Socket.Responses.Clear();
Socket.Requests.Clear();
Expand All @@ -74,9 +78,10 @@ public void DeviceDisconnectedTest()
() => eventWaiter.WaitOne(1000));

Assert.Empty(monitor.Devices);
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Single(sink.DisconnectedEvents);
Assert.Equal("169.254.109.177:5555", sink.DisconnectedEvents[0].Device.Serial);
}
Expand All @@ -101,13 +106,16 @@ public void DeviceConnectedTest()
Assert.Empty(monitor.Devices);
Assert.Empty(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Empty(sink.NotifiedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Empty(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);

Socket.ResponseMessages.Clear();
Socket.Responses.Clear();
Socket.Requests.Clear();

sink.ResetSignals();

// Device disconnects
ManualResetEvent eventWaiter = sink.CreateEventSignal();

Expand All @@ -121,6 +129,7 @@ public void DeviceConnectedTest()
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);
Assert.Equal("169.254.109.177:5555", sink.ConnectedEvents[0].Device.Serial);
}
Expand Down Expand Up @@ -150,11 +159,12 @@ public void StartInitialDeviceListTest()
Assert.Equal("169.254.109.177:5555", sink.ConnectedEvents[0].Device.Serial);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);
}

[Fact]
public void DeviceChanged_TriggeredWhenStatusChangedTest()
public void TriggeredWhenStatusChangedTest()
{
Socket.WaitForNewData = true;

Expand All @@ -175,6 +185,7 @@ public void DeviceChanged_TriggeredWhenStatusChangedTest()
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);

Socket.ResponseMessages.Clear();
Expand All @@ -197,12 +208,13 @@ public void DeviceChanged_TriggeredWhenStatusChangedTest()
Assert.Empty(sink.ConnectedEvents);
Assert.Single(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);
Assert.Equal("169.254.109.177:5555", sink.ChangedEvents[0].Device.Serial);
}

[Fact]
public void DeviceChanged_NoTriggerIfStatusIsSameTest()
public void NoTriggerIfStatusIsSameTest()
{
Socket.WaitForNewData = true;

Expand All @@ -219,10 +231,11 @@ public void DeviceChanged_NoTriggerIfStatusIsSameTest()
monitor.Start);

Assert.Single(monitor.Devices);
Assert.Equal(DeviceState.Offline, monitor.Devices[0].State);
Assert.Equal(DeviceState.Offline, monitor.Devices.ElementAt(0).State);
Assert.Single(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Single(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);

Socket.ResponseMessages.Clear();
Expand All @@ -241,10 +254,11 @@ public void DeviceChanged_NoTriggerIfStatusIsSameTest()
() => eventWaiter.WaitOne(1000));

Assert.Single(monitor.Devices);
Assert.Equal(DeviceState.Offline, monitor.Devices[0].State);
Assert.Equal(DeviceState.Offline, monitor.Devices.ElementAt(0).State);
Assert.Empty(sink.ConnectedEvents);
Assert.Empty(sink.ChangedEvents);
Assert.Single(sink.NotifiedEvents);
Assert.Empty(sink.ListChangedEvents);
Assert.Empty(sink.DisconnectedEvents);
}

Expand Down
10 changes: 8 additions & 2 deletions AdvancedSharpAdbClient.Tests/Dummys/DeviceMonitorSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ public DeviceMonitorSink(DeviceMonitor monitor)
Monitor.DeviceChanged += OnDeviceChanged;
Monitor.DeviceNotified += OnDeviceNotified;
Monitor.DeviceConnected += OnDeviceConnected;
Monitor.DeviceListChanged += OnDeviceListChanged;
Monitor.DeviceDisconnected += OnDeviceDisconnected;

ChangedEvents = [];
NotifiedEvents = [];
ConnectedEvents = [];
ListChangedEvents = [];
DisconnectedEvents = [];
}

Expand All @@ -25,13 +27,16 @@ public void ResetSignals()
ChangedEvents.Clear();
NotifiedEvents.Clear();
ConnectedEvents.Clear();
ListChangedEvents.Clear();
DisconnectedEvents.Clear();
}

public List<DeviceDataConnectEventArgs> DisconnectedEvents { get; init; }

public List<DeviceDataConnectEventArgs> ConnectedEvents { get; init; }
public List<DeviceDataNotifyEventArgs> ListChangedEvents { get; init; }

public List<DeviceDataConnectEventArgs> ConnectedEvents { get; init; }

public List<DeviceDataNotifyEventArgs> NotifiedEvents { get; init; }

public List<DeviceDataChangeEventArgs> ChangedEvents { get; init; }
Expand All @@ -42,12 +47,13 @@ public ManualResetEvent CreateEventSignal()
{
ManualResetEvent signal = new(false);
Monitor.DeviceNotified += (sender, e) => signal.Set();
Monitor.DeviceDisconnected += (sender, e) => signal.Set();
return signal;
}

protected virtual void OnDeviceDisconnected(object sender, DeviceDataConnectEventArgs e) => DisconnectedEvents.Add(e);

protected virtual void OnDeviceListChanged(object sender, DeviceDataNotifyEventArgs e) => ListChangedEvents.Add(e);

protected virtual void OnDeviceConnected(object sender, DeviceDataConnectEventArgs e) => ConnectedEvents.Add(e);

protected virtual void OnDeviceNotified(object sender, DeviceDataNotifyEventArgs e) => NotifiedEvents.Add(e);
Expand Down
2 changes: 2 additions & 0 deletions AdvancedSharpAdbClient/AdbClient.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,11 @@ public async Task<string> DisconnectAsync(DnsEndPoint endpoint, CancellationToke
}

/// <inheritdoc/>
[MethodImpl((MethodImplOptions)256)]
public Task RootAsync(DeviceData device, CancellationToken cancellationToken = default) => RootAsync("root:", device, cancellationToken);

/// <inheritdoc/>
[MethodImpl((MethodImplOptions)256)]
public Task UnrootAsync(DeviceData device, CancellationToken cancellationToken = default) => RootAsync("unroot:", device, cancellationToken);

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions AdvancedSharpAdbClient/AdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -520,9 +521,11 @@ public string Disconnect(DnsEndPoint endpoint)
}

/// <inheritdoc/>
[MethodImpl((MethodImplOptions)256)]
public void Root(DeviceData device) => Root("root:", device);

/// <inheritdoc/>
[MethodImpl((MethodImplOptions)256)]
public void Unroot(DeviceData device) => Root("unroot:", device);

/// <summary>
Expand Down Expand Up @@ -1129,6 +1132,7 @@ public void StopApp(DeviceData device, string packageName)
/// Sets default encoding (default - UTF8).
/// </summary>
/// <param name="encoding">The <see cref="System.Text.Encoding"/> to set.</param>
[MethodImpl((MethodImplOptions)256)]
public static void SetEncoding(Encoding encoding) => Encoding = encoding;

/// <summary>
Expand All @@ -1151,6 +1155,7 @@ protected static void EnsureDevice(DeviceData device)
[GeneratedRegex("<\\?xml(.?)*")]
private static partial Regex GetXmlRegex();
#else
[MethodImpl((MethodImplOptions)256)]
private static Regex GetXmlRegex() => new("<\\?xml(.?)*");
#endif
}
Expand Down
3 changes: 3 additions & 0 deletions AdvancedSharpAdbClient/AdbCommandLineClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;

namespace AdvancedSharpAdbClient
Expand Down Expand Up @@ -128,6 +129,7 @@ public virtual void StartServer()
}

/// <inheritdoc/>
[MethodImpl((MethodImplOptions)256)]
public virtual bool CheckFileExists(string adbPath) => Factories.CheckFileExists(adbPath);

/// <summary>
Expand Down Expand Up @@ -272,6 +274,7 @@ protected virtual int RunProcess(string filename, string command, ICollection<st
[GeneratedRegex(AdbVersionPattern)]
private static partial Regex AdbVersionRegex();
#else
[MethodImpl((MethodImplOptions)256)]
private static Regex AdbVersionRegex() => new(AdbVersionPattern);
#endif
}
Expand Down
2 changes: 2 additions & 0 deletions AdvancedSharpAdbClient/AdbServer.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AdvancedSharpAdbClient.Exceptions;
using System;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;

namespace AdvancedSharpAdbClient
Expand Down Expand Up @@ -61,6 +62,7 @@ public virtual async Task<StartServerResult> StartServerAsync(string adbPath, bo
}

/// <inheritdoc/>
[MethodImpl((MethodImplOptions)256)]
public Task<StartServerResult> RestartServerAsync(CancellationToken cancellationToken = default) => RestartServerAsync(null, cancellationToken);

/// <inheritdoc/>
Expand Down
Loading

0 comments on commit ffb82f8

Please sign in to comment.