Skip to content

Commit

Permalink
Allow to set HiResClock Enabled/Disabled only once. Fixes #1269 (#1341)
Browse files Browse the repository at this point in the history
fixes #1269 
- Add a reset method for testing
  • Loading branch information
AlinMoldovean committed Mar 26, 2021
1 parent 4f861a2 commit 2095723
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ public virtual async Task Validate(ApplicationType applicationType)
// toggle the state of the hi-res clock.
HiResClock.Disabled = m_disableHiResClock;

if (m_disableHiResClock)
if (HiResClock.Disabled)
{
if (m_serverConfiguration != null)
{
Expand Down
30 changes: 26 additions & 4 deletions Stack/Opc.Ua.Core/Types/Utils/HiResClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,45 @@ public static bool Disabled
// do not enable if unsupported
if (Stopwatch.IsHighResolution)
{
if (s_Default.m_disabled && !value)
// check if already initialized.
if (!s_Default.m_initialized)
{
// reset baseline
s_Default = new HiResClock();
if (s_Default.m_disabled && !value)
{
// reset baseline
s_Default = new HiResClock();
}
else
{
s_Default.m_disabled = value;
}

s_Default.m_initialized = true;
}
else
{
s_Default.m_disabled = value;
// do not allow to set the value multiple times since
// it affects the notifications for existing monitored items.
}
}
}
}

/// <summary>
/// Reset the baseline and allow a new initialization.
/// </summary>
public static void Reset()
{
// reset baseline
s_Default = new HiResClock();
}

/// <summary>
/// Constructs a HiRes clock class.
/// </summary>
private HiResClock()
{
m_initialized = false;
m_offset = DateTime.UtcNow.Ticks;
if (!Stopwatch.IsHighResolution)
{
Expand Down Expand Up @@ -130,6 +151,7 @@ private HiResClock()
private double m_ticksPerMillisecond;
private decimal m_ratio;
private bool m_disabled;
private bool m_initialized;
}

}
7 changes: 5 additions & 2 deletions Tests/Opc.Ua.Core.Tests/Types/Utils/HiResClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ public class HiResClockTests
[OneTimeTearDown]
protected void OneTimeTearDown()
{
HiResClock.Disabled = false;
HiResClock.Reset();
}

[TearDown]
protected void TearDown()
{
HiResClock.Disabled = false;
HiResClock.Reset();
}
#endregion

Expand All @@ -75,6 +75,9 @@ public void HiResParameters()
Assert.AreEqual(TimeSpan.TicksPerSecond, HiResClock.Frequency);
Assert.AreEqual(TimeSpan.TicksPerMillisecond, HiResClock.TicksPerMillisecond);
HiResClock.Disabled = false;
Assert.True(HiResClock.Disabled);
HiResClock.Reset();
HiResClock.Disabled = false;
Assert.False(HiResClock.Disabled);
}

Expand Down

0 comments on commit 2095723

Please sign in to comment.