Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use System.TimeProvider to control time #1432

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 6 additions & 4 deletions SteamKit2/SteamKit2/Steam/CMClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,12 @@ public CMClient( SteamConfiguration configuration, string identifier )

ID = identifier;

heartBeatFunc = new ScheduledFunction( () =>
{
Send( new ClientMsgProtobuf<CMsgClientHeartBeat>( EMsg.ClientHeartBeat ) );
} );
heartBeatFunc = new ScheduledFunction(
xPaw marked this conversation as resolved.
Show resolved Hide resolved
configuration.TimeProvider,
() =>
{
Send( new ClientMsgProtobuf<CMsgClientHeartBeat>( EMsg.ClientHeartBeat ) );
} );
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions SteamKit2/SteamKit2/Steam/SteamClient/AsyncJobManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class AsyncJobManager
internal ConcurrentDictionary<JobID, AsyncJob> asyncJobs;
internal ScheduledFunction jobTimeoutFunc;

public AsyncJobManager()
public AsyncJobManager( TimeProvider timeProvider )
{
asyncJobs = new ConcurrentDictionary<JobID, AsyncJob>();

jobTimeoutFunc = new ScheduledFunction( CancelTimedoutJobs, TimeSpan.FromSeconds( 1 ) );
jobTimeoutFunc = new ScheduledFunction( timeProvider, CancelTimedoutJobs, TimeSpan.FromSeconds( 1 ) );
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public interface ISteamConfigurationBuilder
/// <returns>A builder with modified configuration.</returns>
ISteamConfigurationBuilder WithServerListProvider(IServerListProvider provider);

/// <summary>
/// Configures the <see cref="TimeProvider"/> for this <see cref="SteamConfiguration" />.
/// This can be changed for testing and diagnostic purposes.
/// </summary>
/// <param name="timeProvider">The time provider to use.</param>
/// <returns>A builder with modified configuration.</returns>
ISteamConfigurationBuilder WithTimeProvider(TimeProvider timeProvider);

/// <summary>
/// Configures the Universe that this <see cref="SteamConfiguration" /> belongs to.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ internal static SteamConfiguration CreateDefault()
/// </summary>
public IServerListProvider ServerListProvider => state.ServerListProvider;

/// <summary>
/// The <see cref="TimeProvider"/> used to control the flow of time.
/// This can be changed for testing and diagnostic purposes.
/// </summary>
public TimeProvider TimeProvider => state.TimeProvider;

/// <summary>
/// The Universe to connect to. This should always be <see cref="EUniverse.Public"/> unless
/// you work at Valve and are using this internally. If this is you, hello there.
Expand All @@ -106,7 +112,7 @@ internal static SteamConfiguration CreateDefault()
public Uri WebAPIBaseAddress => state.WebAPIBaseAddress;

/// <summary>
/// An API key to be used for authorized requests.
/// An API key to be used for authorized requests.
/// Keys can be obtained from https://steamcommunity.com/dev or the Steamworks Partner site.
/// </summary>
public string WebAPIKey => state.WebAPIKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public static SteamConfigurationState CreateDefaultState()

ServerListProvider = new MemoryServerListProvider(),

TimeProvider = TimeProvider.System,

Universe = EUniverse.Public,

WebAPIBaseAddress = WebAPI.DefaultBaseAddress
Expand Down Expand Up @@ -98,6 +100,13 @@ public ISteamConfigurationBuilder WithServerListProvider(IServerListProvider pro
return this;
}

public ISteamConfigurationBuilder WithTimeProvider(TimeProvider timeProvider)
{
ArgumentNullException.ThrowIfNull(timeProvider);
state.TimeProvider = timeProvider;
return this;
}

public ISteamConfigurationBuilder WithUniverse(EUniverse universe)
{
state.Universe = universe;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct SteamConfigurationState
public IMachineInfoProvider MachineInfoProvider;
public ProtocolTypes ProtocolTypes;
public IServerListProvider ServerListProvider;
public TimeProvider TimeProvider;
public EUniverse Universe;
public Uri WebAPIBaseAddress;
public string WebAPIKey;
Expand Down
2 changes: 1 addition & 1 deletion SteamKit2/SteamKit2/Steam/SteamClient/SteamClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public SteamClient( SteamConfiguration configuration, string identifier )
this.processStartTime = process.StartTime;
}

jobManager = new AsyncJobManager();
jobManager = new AsyncJobManager( configuration.TimeProvider );
}


Expand Down
10 changes: 5 additions & 5 deletions SteamKit2/SteamKit2/Util/ScheduledFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ class ScheduledFunction
Action func;

bool bStarted;
Timer timer;
ITimer timer;

public ScheduledFunction( Action func )
: this( func, TimeSpan.FromMilliseconds( -1 ) )
public ScheduledFunction( TimeProvider timeProvider, Action func )
: this( timeProvider, func, TimeSpan.FromMilliseconds( -1 ) )
{
}

public ScheduledFunction( Action func, TimeSpan delay )
public ScheduledFunction( TimeProvider timeProvider, Action func, TimeSpan delay )
{
this.func = func;
this.Delay = delay;

timer = new Timer( Tick, null, TimeSpan.FromMilliseconds( -1 ), delay );
timer = timeProvider.CreateTimer( Tick, null, TimeSpan.FromMilliseconds( -1 ), delay );
}
~ScheduledFunction()
{
Expand Down
17 changes: 17 additions & 0 deletions SteamKit2/Tests/SteamConfigurationFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public void PublicUniverse()
Assert.Equal(EUniverse.Public, configuration.Universe);
}

[Fact]
public void TimeProviderIsSystemTime()
{
Assert.Equal(TimeProvider.System, configuration.TimeProvider);
}

[Fact]
public void WebAPIAddress()
{
Expand All @@ -114,6 +120,7 @@ public SteamConfigurationConfiguredObjectFacts()
.WithMachineInfoProvider(new CustomMachineInfoProvider())
.WithProtocolTypes(ProtocolTypes.WebSocket | ProtocolTypes.Udp)
.WithServerListProvider(new CustomServerListProvider())
.WithTimeProvider(new CustomTimeProvider())
.WithUniverse(EUniverse.Internal)
.WithWebAPIBaseAddress(new Uri("http://foo.bar.com/api/"))
.WithWebAPIKey("T0PS3kR1t"));
Expand Down Expand Up @@ -177,6 +184,12 @@ public void ProtocolsAreConfigured()
Assert.Equal(ProtocolTypes.WebSocket | ProtocolTypes.Udp, configuration.ProtocolTypes);
}

[Fact]
public void TimeProviderIsConfigured()
{
Assert.IsType<CustomTimeProvider>(configuration.TimeProvider);
}

[Fact]
public void UniverseIsConfigured()
{
Expand Down Expand Up @@ -213,5 +226,9 @@ Task<IEnumerable<ServerRecord>> IServerListProvider.FetchServerListAsync()
Task IServerListProvider.UpdateServerListAsync(IEnumerable<ServerRecord> endpoints)
=> throw new NotImplementedException();
}

class CustomTimeProvider : TimeProvider
{
}
}
}