/* ========================================================================
* Copyright (c) 2005-2025 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Time.Testing;
namespace Opc.Ua.Client.Tests.Stack.Client.Fakes
{
///
/// Exposes fake-time timer creation and successful schedule changes so tests can wait for timer registration
/// before advancing the clock.
///
///
/// Observations are not retained. Register each wait before starting the operation whose timer should satisfy it.
///
internal sealed class ObservableFakeTimeProvider : FakeTimeProvider
{
///
///
/// Reports the new timer's schedule and wraps it to report subsequent successful schedule changes.
///
public override ITimer CreateTimer(
TimerCallback callback,
object? state,
TimeSpan dueTime,
TimeSpan period)
{
ITimer timer = base.CreateTimer(callback, state, dueTime, period);
NotifyTimer(false, dueTime, period);
return new ObservedTimer(this, timer);
}
///
/// Waits for a subsequently created timer with the specified due time, regardless of its period.
///
/// The exact due time that must be supplied when creating the timer.
public Task WaitForTimerCreatedAsync(TimeSpan dueTime)
{
return WaitForTimerAsync(false, dueTime, dueTime, null);
}
///
/// Waits for a subsequently created timer with an infinite period and a due time in the inclusive range.
/// Returns the due time supplied when that timer was created.
///
/// The inclusive lower bound for the timer's due time.
/// The inclusive upper bound for the timer's due time.
/// The maximum due time is less than the minimum.
public Task WaitForTimerCreatedAsync(TimeSpan minimumDueTime, TimeSpan maximumDueTime)
{
return WaitForTimerAsync(false, minimumDueTime, maximumDueTime, Timeout.InfiniteTimeSpan);
}
///
/// Waits for a subsequent successful timer schedule change matching both the due time and period.
///
/// The exact due time supplied to the timer's change operation.
/// The exact period supplied to the timer's change operation.
public Task WaitForTimerChangedAsync(TimeSpan dueTime, TimeSpan period)
{
return WaitForTimerAsync(true, dueTime, dueTime, period);
}
private Task WaitForTimerAsync(
bool changed,
TimeSpan minimumDueTime,
TimeSpan maximumDueTime,
TimeSpan? period)
{
if (maximumDueTime < minimumDueTime)
{
throw new ArgumentOutOfRangeException(nameof(maximumDueTime));
}
var completion = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
lock (m_lock)
{
m_waiters.Add((changed, minimumDueTime, maximumDueTime, period, completion));
}
return completion.Task;
}
private void NotifyTimer(bool changed, TimeSpan dueTime, TimeSpan period)
{
lock (m_lock)
{
for (int index = m_waiters.Count - 1; index >= 0; index--)
{
var waiter = m_waiters[index];
if (waiter.Changed == changed &&
waiter.MinimumDueTime <= dueTime &&
waiter.MaximumDueTime >= dueTime &&
(!waiter.Period.HasValue || waiter.Period.Value == period))
{
waiter.Completion.TrySetResult(dueTime);
m_waiters.RemoveAt(index);
}
}
}
}
///
/// Forwards timer operations and reports successful schedule changes to the observing clock.
///
/// The clock whose pending waits receive schedule-change observations.
/// The underlying fake-time timer.
private sealed class ObservedTimer(ObservableFakeTimeProvider owner, ITimer timer) : ITimer
{
///
public bool Change(TimeSpan dueTime, TimeSpan period)
{
bool changed = timer.Change(dueTime, period);
if (changed)
{
owner.NotifyTimer(true, dueTime, period);
}
return changed;
}
///
public void Dispose()
{
timer.Dispose();
}
///
public ValueTask DisposeAsync()
{
return timer.DisposeAsync();
}
}
private readonly Lock m_lock = new();
private readonly List<(
bool Changed,
TimeSpan MinimumDueTime,
TimeSpan MaximumDueTime,
TimeSpan? Period,
TaskCompletionSource Completion)> m_waiters = [];
}
}