Skip to content

Commit

Permalink
Replaced DateTime.Now with DateTime.UtcNow/MonotonicClock
Browse files Browse the repository at this point in the history
  • Loading branch information
kekekeks committed May 23, 2015
1 parent 11fa059 commit a3a9fec
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/core/Akka.Cluster/ClusterMetricsCollector.cs
Expand Up @@ -623,7 +623,7 @@ internal static class StandardMetrics

public static long NewTimestamp()
{
return DateTime.Now.Ticks;
return DateTime.UtcNow.Ticks;
}

public sealed class SystemMemory
Expand Down
6 changes: 3 additions & 3 deletions src/core/Akka.Persistence/AtLeastOnceDelivery.cs
Expand Up @@ -209,7 +209,7 @@ public void Deliver(ActorPath destination, Func<long, object> deliveryMessageMap
}

var deliveryId = NextDeliverySequenceNr();
var now = IsRecovering ? DateTime.Now - RedeliverInterval : DateTime.Now;
var now = IsRecovering ? DateTime.UtcNow - RedeliverInterval : DateTime.UtcNow;
var delivery = new Delivery(destination, deliveryMessageMapper(deliveryId), now, attempt: 0);

if (IsRecovering)
Expand Down Expand Up @@ -254,7 +254,7 @@ public AtLeastOnceDeliverySnapshot GetDeliverySnapshot()
public void SetDeliverySnapshot(AtLeastOnceDeliverySnapshot snapshot)
{
_deliverySequenceNr = snapshot.DeliveryId;
var now = DateTime.Now;
var now = DateTime.UtcNow;
var unconfirmedDeliveries = snapshot.UnconfirmedDeliveries
.Select(u => new KeyValuePair<long, Delivery>(u.DeliveryId, new Delivery(u.Destination, u.Message, now, 0)));

Expand Down Expand Up @@ -302,7 +302,7 @@ private void Send(long deliveryId, Delivery delivery, DateTime timestamp)

private void RedeliverOverdue()
{
var now = DateTime.Now;
var now = DateTime.UtcNow;
var deadline = now - RedeliverInterval;
var warnings = new List<UnconfirmedDelivery>();

Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Persistence/Snapshot/SnapshotStore.cs
Expand Up @@ -42,7 +42,7 @@ protected override bool Receive(object message)
else if (message is SaveSnapshot)
{
var msg = (SaveSnapshot)message;
var metadata = new SnapshotMetadata(msg.Metadata.PersistenceId, msg.Metadata.SequenceNr, DateTime.Now);
var metadata = new SnapshotMetadata(msg.Metadata.PersistenceId, msg.Metadata.SequenceNr, DateTime.UtcNow);

SaveAsync(metadata, msg.Snapshot).ContinueWith(t => !t.IsFaulted
? (object)new SaveSnapshotSuccess(metadata)
Expand Down
10 changes: 5 additions & 5 deletions src/core/Akka.Remote/Deadline.cs
Expand Up @@ -21,20 +21,20 @@ public Deadline(DateTime when)

public bool IsOverdue
{
get { return DateTime.Now > When; }
get { return DateTime.UtcNow > When; }
}

public bool HasTimeLeft
{
get { return DateTime.Now < When; }
get { return DateTime.UtcNow < When; }
}

public DateTime When { get; private set; }

/// <summary>
/// Warning: creates a new <see cref="TimeSpan"/> instance each time it's used
/// </summary>
public TimeSpan TimeLeft { get { return When - DateTime.Now; } }
public TimeSpan TimeLeft { get { return When - DateTime.UtcNow; } }

#region Overrides

Expand All @@ -60,13 +60,13 @@ public override int GetHashCode()
#region Static members

/// <summary>
/// Returns a deadline that is due <see cref="DateTime.Now"/>
/// Returns a deadline that is due <see cref="DateTime.UtcNow"/>
/// </summary>
public static Deadline Now
{
get
{
return new Deadline(DateTime.Now);
return new Deadline(DateTime.UtcNow);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/core/Akka.Tests/Actor/ActorSystemSpec.cs
Expand Up @@ -11,6 +11,7 @@
using Xunit;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Akka.Tests.Actor
{
Expand Down Expand Up @@ -54,11 +55,10 @@ public void AnActorSystemShouldBeAllowedToBlockUntilExit()
{
var actorSystem = ActorSystem
.Create(Guid.NewGuid().ToString());
var startTime = DateTime.UtcNow;
var st = Stopwatch.StartNew();
var asyncShutdownTask = Task.Delay(TimeSpan.FromSeconds(1)).ContinueWith(_ => actorSystem.Shutdown());
actorSystem.AwaitTermination(TimeSpan.FromSeconds(2)).ShouldBeTrue();
var endTime = DateTime.UtcNow;
Assert.True((endTime - startTime).TotalSeconds >= .9);
Assert.True(st.Elapsed.TotalSeconds >= .9);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka.Tests/Actor/InboxSpec.cs
Expand Up @@ -138,7 +138,7 @@ public void Inbox_have_a_default_and_custom_timeouts()
[Fact]
public void Select_WithClient_should_update_Client_and_copy_the_rest_of_the_properties_BUG_427()
{
var deadline = new DateTime(1919, 5, 24);
var deadline = new TimeSpan(Sys.Scheduler.MonotonicClock.Ticks/2); //Some point in the past
Predicate<object> predicate = o => true;
var actorRef = new EmptyLocalActorRef(((ActorSystemImpl)Sys).Provider, new RootActorPath(new Address("akka", "test")), Sys.EventStream);
var select = new Select(deadline, predicate, actorRef);
Expand Down
3 changes: 2 additions & 1 deletion src/core/Akka/Actor/ChildrenContainer/Internal/ChildStats.cs
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using Akka.Util;

namespace Akka.Actor.Internal
{
Expand Down Expand Up @@ -79,7 +80,7 @@ private bool RetriesInWindowOkay(int retries, int windowInMilliseconds)
// after a restart and if enough restarts happen during this time, it
// denies. Otherwise window closes and the scheme starts over.
var retriesDone = _maxNrOfRetriesCount + 1;
var now = DateTime.Now.Ticks;
var now = MonotonicClock.Elapsed.Ticks;
long windowStart;
if (_restartTimeWindowStartTicks == 0)
{
Expand Down
6 changes: 3 additions & 3 deletions src/core/Akka/Actor/Inbox.Actor.cs
Expand Up @@ -22,7 +22,7 @@ internal class InboxActor : ActorBase

private object _currentMessage;
private Select? _currentSelect;
private Tuple<DateTime, ICancelable> _currentDeadline;
private Tuple<TimeSpan, ICancelable> _currentDeadline;

private int _size;
private ILoggingAdapter _log = Context.GetLogger();
Expand Down Expand Up @@ -120,7 +120,7 @@ protected override bool Receive(object message)
.With<StopWatch>(sw => Context.Unwatch(sw.Target))
.With<Kick>(() =>
{
var now = DateTime.Now;
var now = Context.System.Scheduler.MonotonicClock;
var overdue = _clientsByTimeout.TakeWhile(q => q.Deadline < now);
foreach (var query in overdue)
{
Expand Down Expand Up @@ -169,7 +169,7 @@ protected override bool Receive(object message)
{
_currentDeadline.Item2.Cancel();
}
var cancelable = Context.System.Scheduler.ScheduleTellOnceCancelable(next.Deadline - DateTime.Now, Self, new Kick(), Self);
var cancelable = Context.System.Scheduler.ScheduleTellOnceCancelable(next.Deadline - Context.System.Scheduler.MonotonicClock, Self, new Kick(), Self);

_currentDeadline = Tuple.Create(next.Deadline, cancelable);
}
Expand Down
14 changes: 7 additions & 7 deletions src/core/Akka/Actor/Inbox.cs
Expand Up @@ -16,21 +16,21 @@ namespace Akka.Actor
{
internal interface IQuery
{
DateTime Deadline { get; }
TimeSpan Deadline { get; }
IActorRef Client { get; }
IQuery WithClient(IActorRef client);
}

internal struct Get : IQuery
{
public Get(DateTime deadline, IActorRef client = null)
public Get(TimeSpan deadline, IActorRef client = null)
: this()
{
Deadline = deadline;
Client = client;
}

public DateTime Deadline { get; private set; }
public TimeSpan Deadline { get; private set; }
public IActorRef Client { get; private set; }
public IQuery WithClient(IActorRef client)
{
Expand All @@ -40,15 +40,15 @@ public IQuery WithClient(IActorRef client)

internal struct Select : IQuery
{
public Select(DateTime deadline, Predicate<object> predicate, IActorRef client = null)
public Select(TimeSpan deadline, Predicate<object> predicate, IActorRef client = null)
: this()
{
Deadline = deadline;
Predicate = predicate;
Client = client;
}

public DateTime Deadline { get; private set; }
public TimeSpan Deadline { get; private set; }
public Predicate<object> Predicate { get; set; }
public IActorRef Client { get; private set; }
public IQuery WithClient(IActorRef client)
Expand Down Expand Up @@ -319,7 +319,7 @@ public object ReceiveWhere(Predicate<object> predicate)

public object ReceiveWhere(Predicate<object> predicate, TimeSpan timeout)
{
var task = Receiver.Ask(new Select(DateTime.Now + timeout, predicate), Timeout.InfiniteTimeSpan);
var task = Receiver.Ask(new Select(_system.Scheduler.MonotonicClock + timeout, predicate), Timeout.InfiniteTimeSpan);
return AwaitResult(task, timeout);
}

Expand All @@ -330,7 +330,7 @@ public Task<object> ReceiveAsync()

public Task<object> ReceiveAsync(TimeSpan timeout)
{
return Receiver.Ask(new Get(DateTime.Now + timeout), Timeout.InfiniteTimeSpan);
return Receiver.Ask(new Get(_system.Scheduler.MonotonicClock + timeout), Timeout.InfiniteTimeSpan);
}

public void Dispose()
Expand Down
2 changes: 1 addition & 1 deletion src/core/Akka/Event/LogEvent.cs
Expand Up @@ -21,7 +21,7 @@ public abstract class LogEvent : INoSerializationVerificationNeeded
/// </summary>
protected LogEvent()
{
Timestamp = DateTime.Now;
Timestamp = DateTime.UtcNow;
Thread = Thread.CurrentThread;
}

Expand Down
2 changes: 1 addition & 1 deletion src/examples/TimeServer/TimeServer/Program.cs
Expand Up @@ -33,7 +33,7 @@ public void Handle(string message)
{
if (message.ToLowerInvariant() == "gettime")
{
var time =DateTime.Now.ToLongTimeString();
var time =DateTime.UtcNow.ToLongTimeString();
Sender.Tell(time, Self);
}
else
Expand Down

0 comments on commit a3a9fec

Please sign in to comment.