Code formatting
This commit is contained in:
@@ -4,9 +4,9 @@ interface
|
||||
|
||||
{$align on}
|
||||
uses
|
||||
{$ifndef NO_FASTMM}
|
||||
{$ifndef NO_FASTMM}
|
||||
FastMM5,
|
||||
{$endif}
|
||||
{$endif}
|
||||
Winapi.Windows;
|
||||
|
||||
type
|
||||
@@ -33,7 +33,8 @@ type
|
||||
end;
|
||||
|
||||
TMycAtomicStack<T> = record
|
||||
private type
|
||||
private
|
||||
type
|
||||
PItem = ^TItem;
|
||||
TItem = packed record
|
||||
Next: TSListEntry;
|
||||
|
||||
+18
-14
@@ -4,22 +4,24 @@ interface
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
Myc.Signals, Myc.TaskManager, Myc.Futures;
|
||||
Myc.Signals,
|
||||
Myc.TaskManager,
|
||||
Myc.Futures;
|
||||
|
||||
type
|
||||
TMycFuture<T> = class abstract( TInterfacedObject, IMycFuture<T> )
|
||||
TMycFuture<T> = class abstract(TInterfacedObject, IMycFuture<T>)
|
||||
protected
|
||||
function GetResult: T; virtual; abstract;
|
||||
function GetDone: TState; virtual; abstract;
|
||||
end;
|
||||
|
||||
TMycNullFuture<T> = class( TMycFuture<T> )
|
||||
TMycNullFuture<T> = class(TMycFuture<T>)
|
||||
protected
|
||||
function GetResult: T; override;
|
||||
function GetDone: TState; override;
|
||||
end;
|
||||
|
||||
TMycGateFuncFuture<T> = class( TMycFuture<T> )
|
||||
TMycGateFuncFuture<T> = class(TMycFuture<T>)
|
||||
private
|
||||
FInit: TState.TSubscription;
|
||||
FDone: IMycLatch;
|
||||
@@ -28,7 +30,7 @@ type
|
||||
function GetResult: T; override;
|
||||
function GetDone: TState; override;
|
||||
public
|
||||
constructor Create( const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T> );
|
||||
constructor Create(const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T>);
|
||||
destructor Destroy; override;
|
||||
end;
|
||||
|
||||
@@ -43,39 +45,41 @@ end;
|
||||
|
||||
function TMycNullFuture<T>.GetResult: T;
|
||||
begin
|
||||
Result := Default ( T );
|
||||
Result := Default(T);
|
||||
end;
|
||||
|
||||
{ TMycGateFuncFuture<T> }
|
||||
|
||||
constructor TMycGateFuncFuture<T>.Create( const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T> );
|
||||
constructor TMycGateFuncFuture<T>.Create(const ATaskManager: IMycTaskManager; const AGate: IMycState; AProc: TFunc<T>);
|
||||
begin
|
||||
inherited Create;
|
||||
|
||||
FDone := TLatch.Construct( 1 );
|
||||
FDone := TLatch.Construct(1);
|
||||
|
||||
// Subscribe the job execution to AGate.
|
||||
// The job will run when AGate notifies the subscriber returned by Run.
|
||||
FInit := ATaskManager.CreateTask(
|
||||
FInit :=
|
||||
ATaskManager.CreateTask(
|
||||
AGate,
|
||||
procedure
|
||||
begin
|
||||
try
|
||||
try
|
||||
Self.FResult := AProc( );
|
||||
Self.FResult := AProc();
|
||||
except
|
||||
Self.FResult := Default ( T ); // Set result to Default(T) on error
|
||||
Self.FResult := Default(T); // Set result to Default(T) on error
|
||||
raise; // Re-raise for TaskFactory to handle
|
||||
end;
|
||||
finally
|
||||
Self.FDone.Notify; // Signal that this future is done (successfully or with error)
|
||||
end;
|
||||
end );
|
||||
end
|
||||
);
|
||||
end;
|
||||
|
||||
destructor TMycGateFuncFuture<T>.Destroy;
|
||||
begin
|
||||
Assert( FDone.State.IsSet );
|
||||
Assert(FDone.State.IsSet);
|
||||
|
||||
FInit.Unsubscribe;
|
||||
inherited Destroy;
|
||||
@@ -88,7 +92,7 @@ end;
|
||||
|
||||
function TMycGateFuncFuture<T>.GetResult: T;
|
||||
begin
|
||||
Assert( FDone.State.IsSet, 'Result is not yet available.' );
|
||||
Assert(FDone.State.IsSet, 'Result is not yet available.');
|
||||
Result := FResult;
|
||||
end;
|
||||
|
||||
|
||||
+11
-11
@@ -8,21 +8,21 @@ uses
|
||||
Myc.Lazy;
|
||||
|
||||
type
|
||||
TMycLazy<T> = class abstract( TInterfacedObject, IMycLazy<T> )
|
||||
TMycLazy<T> = class abstract(TInterfacedObject, IMycLazy<T>)
|
||||
protected
|
||||
function GetChanged: IMycState; virtual; abstract;
|
||||
public
|
||||
function Pop( out Res: T ): Boolean; virtual; abstract;
|
||||
function Pop(out Res: T): Boolean; virtual; abstract;
|
||||
end;
|
||||
|
||||
TMycNullLazy<T> = class( TMycLazy<T> )
|
||||
TMycNullLazy<T> = class(TMycLazy<T>)
|
||||
protected
|
||||
function GetChanged: IMycState; override;
|
||||
public
|
||||
function Pop( out Res: T ): Boolean; override;
|
||||
function Pop(out Res: T): Boolean; override;
|
||||
end;
|
||||
|
||||
TMycFuncLazy<T> = class( TMycLazy<T> )
|
||||
TMycFuncLazy<T> = class(TMycLazy<T>)
|
||||
private
|
||||
FChanged: IMycDirty;
|
||||
FChangeState: TState.TSubscription;
|
||||
@@ -32,7 +32,7 @@ type
|
||||
public
|
||||
constructor Create(const AChanged: TState; const AProc: TFunc<T>);
|
||||
destructor Destroy; override;
|
||||
function Pop( out Res: T ): Boolean; override;
|
||||
function Pop(out Res: T): Boolean; override;
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -44,9 +44,9 @@ begin
|
||||
Result := TState.Null;
|
||||
end;
|
||||
|
||||
function TMycNullLazy<T>.Pop( out Res: T ): Boolean;
|
||||
function TMycNullLazy<T>.Pop(out Res: T): Boolean;
|
||||
begin
|
||||
Res := Default ( T );
|
||||
Res := Default(T);
|
||||
Result := true;
|
||||
end;
|
||||
|
||||
@@ -57,7 +57,7 @@ begin
|
||||
inherited Create;
|
||||
FChanged := TDirty.Construct;
|
||||
FProc := AProc;
|
||||
FChangeState := AChanged.Subscribe( FChanged );
|
||||
FChangeState := AChanged.Subscribe(FChanged);
|
||||
end;
|
||||
|
||||
destructor TMycFuncLazy<T>.Destroy;
|
||||
@@ -71,12 +71,12 @@ begin
|
||||
Result := FChanged.State;
|
||||
end;
|
||||
|
||||
function TMycFuncLazy<T>.Pop( out Res: T ): Boolean;
|
||||
function TMycFuncLazy<T>.Pop(out Res: T): Boolean;
|
||||
begin
|
||||
Result := FChanged.State.IsSet;
|
||||
if Result then
|
||||
begin
|
||||
if Assigned( FProc ) then
|
||||
if Assigned(FProc) then
|
||||
Res := FProc;
|
||||
FChanged.Reset;
|
||||
end;
|
||||
|
||||
+37
-34
@@ -3,7 +3,8 @@ unit Myc.Core.Notifier;
|
||||
interface
|
||||
|
||||
uses
|
||||
System.SysUtils, System.SyncObjs;
|
||||
System.SysUtils,
|
||||
System.SyncObjs;
|
||||
|
||||
type
|
||||
// Low-level implementation for thread-safe multicast events.
|
||||
@@ -30,7 +31,7 @@ type
|
||||
FList: PItem; // Head of the linked list for additional receivers beyond the first one.
|
||||
|
||||
class function AllocItem: PItem; static; inline; // Allocates and initializes memory for a new TItem.
|
||||
class procedure FreeItem( Item: PItem ); static; inline; // Frees memory previously allocated for a TItem.
|
||||
class procedure FreeItem(Item: PItem); static; inline; // Frees memory previously allocated for a TItem.
|
||||
|
||||
public
|
||||
procedure Create; // Initializes the notification list, preparing it for use.
|
||||
@@ -39,9 +40,11 @@ type
|
||||
procedure Unadvise(Tag: TTag); // Unregisters a specific receiver using the tag obtained from Advise.
|
||||
procedure UnadviseAll; // Unregisters all currently advised receivers.
|
||||
procedure Lock; inline; // Acquires an exclusive lock for thread-safe operations on the list.
|
||||
procedure Release; inline;// Releases the previously acquired exclusive lock.
|
||||
procedure Release; inline; // Releases the previously acquired exclusive lock.
|
||||
function IsLocked: Boolean; inline; // Checks if the list is currently locked by any thread.
|
||||
procedure Notify(Func: TPredicate<T>); // Iterates through registered receivers and invokes the predicate; removes receiver if predicate returns false.
|
||||
procedure Notify(
|
||||
Func: TPredicate<T>
|
||||
); // Iterates through registered receivers and invokes the predicate; removes receiver if predicate returns false.
|
||||
end;
|
||||
|
||||
implementation
|
||||
@@ -56,7 +59,7 @@ procedure TMycNotifyList<T>.Destroy;
|
||||
begin
|
||||
// Because refcounting is thread-safe, this will always be entered once after all references
|
||||
// to Self are dropped. No locking needed!
|
||||
Assert( not IsLocked );
|
||||
Assert(not IsLocked);
|
||||
FFirst := FFirst and not 3;
|
||||
UnadviseAll;
|
||||
end;
|
||||
@@ -65,24 +68,24 @@ function TMycNotifyList<T>.Advise(const Receiver: T): TTag;
|
||||
var
|
||||
Item: PItem;
|
||||
begin
|
||||
Assert( IsLocked );
|
||||
Assert(IsLocked);
|
||||
|
||||
if FFirst=0 then
|
||||
if FFirst = 0 then
|
||||
begin
|
||||
IInterface( FFirst ) := Receiver;
|
||||
exit( PPointer(@Receiver)^ );
|
||||
IInterface(FFirst) := Receiver;
|
||||
exit(PPointer(@Receiver)^);
|
||||
end;
|
||||
|
||||
Item := AllocItem;
|
||||
Item.Receiver := Receiver;
|
||||
Item.Prev := nil;
|
||||
Item.Next := FList;
|
||||
if Item.Next<>nil then
|
||||
if Item.Next <> nil then
|
||||
Item.Next.Prev := Item;
|
||||
|
||||
FList := Item;
|
||||
|
||||
exit( Item );
|
||||
exit(Item);
|
||||
end;
|
||||
|
||||
procedure TMycNotifyList<T>.Lock;
|
||||
@@ -93,19 +96,19 @@ begin
|
||||
YieldProcessor;
|
||||
continue;
|
||||
end;
|
||||
until TInterlocked.BitTestAndClear( PNativeUint( @FFirst )^, 0 );
|
||||
until TInterlocked.BitTestAndClear(PNativeUint(@FFirst)^, 0);
|
||||
|
||||
Assert( IsLocked, 'Locking failed' );
|
||||
Assert(IsLocked, 'Locking failed');
|
||||
end;
|
||||
|
||||
class function TMycNotifyList<T>.AllocItem: PItem;
|
||||
begin
|
||||
Result := AllocMem( sizeof( TItem ) );
|
||||
Result := AllocMem(sizeof(TItem));
|
||||
end;
|
||||
|
||||
class procedure TMycNotifyList<T>.FreeItem(Item: PItem);
|
||||
begin
|
||||
FreeMem( Item, sizeof( TItem ) );
|
||||
FreeMem(Item, sizeof(TItem));
|
||||
end;
|
||||
|
||||
function TMycNotifyList<T>.IsLocked: Boolean;
|
||||
@@ -117,65 +120,65 @@ procedure TMycNotifyList<T>.Notify(Func: TPredicate<T>);
|
||||
var
|
||||
Item, P: PItem;
|
||||
begin
|
||||
Assert( IsLocked );
|
||||
Assert(IsLocked);
|
||||
|
||||
if FFirst<>0 then
|
||||
if not Func( IInterface( FFirst ) ) then
|
||||
IInterface( FFirst ) := nil;
|
||||
if FFirst <> 0 then
|
||||
if not Func(IInterface(FFirst)) then
|
||||
IInterface(FFirst) := nil;
|
||||
|
||||
Item := FList;
|
||||
while Item<>nil do
|
||||
while Item <> nil do
|
||||
begin
|
||||
P := Item.Next;
|
||||
if not Func( Item.Receiver ) then
|
||||
Unadvise( TTag( Item ) );
|
||||
if not Func(Item.Receiver) then
|
||||
Unadvise(TTag(Item));
|
||||
Item := P;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TMycNotifyList<T>.Release;
|
||||
begin
|
||||
Assert( IsLocked );
|
||||
TInterlocked.Exchange( Pointer( FFirst ), Pointer( FFirst or 1 ) );
|
||||
Assert(IsLocked);
|
||||
TInterlocked.Exchange(Pointer(FFirst), Pointer(FFirst or 1));
|
||||
end;
|
||||
|
||||
procedure TMycNotifyList<T>.Unadvise(Tag: TTag);
|
||||
var
|
||||
Item: PItem;
|
||||
begin
|
||||
Assert( IsLocked );
|
||||
Assert(IsLocked);
|
||||
|
||||
if NativeUInt(Tag) = FFirst then
|
||||
begin
|
||||
IInterface( FFirst ) := nil;
|
||||
IInterface(FFirst) := nil;
|
||||
exit;
|
||||
end;
|
||||
|
||||
if FList = nil then
|
||||
exit;
|
||||
|
||||
Item := PItem( Tag );
|
||||
Item := PItem(Tag);
|
||||
|
||||
if Item = FList then
|
||||
FList := Item.Next;
|
||||
|
||||
if Item.Prev<>nil then
|
||||
if Item.Prev <> nil then
|
||||
Item.Prev.Next := Item.Next;
|
||||
if Item.Next<>nil then
|
||||
if Item.Next <> nil then
|
||||
Item.Next.Prev := Item.Prev;
|
||||
|
||||
Item.Receiver := nil;
|
||||
|
||||
FreeItem( Item );
|
||||
FreeItem(Item);
|
||||
end;
|
||||
|
||||
procedure TMycNotifyList<T>.UnadviseAll;
|
||||
begin
|
||||
Assert( IsLocked );
|
||||
Assert(IsLocked);
|
||||
|
||||
IInterface( FFirst ) := nil;
|
||||
while FList<>nil do
|
||||
Unadvise( TTag( FList ) );
|
||||
IInterface(FFirst) := nil;
|
||||
while FList <> nil do
|
||||
Unadvise(TTag(FList));
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
+10
-16
@@ -8,7 +8,7 @@ uses
|
||||
Myc.Signals;
|
||||
|
||||
type
|
||||
TMycState = class abstract( TInterfacedObject, IMycState )
|
||||
TMycState = class abstract(TInterfacedObject, IMycState)
|
||||
protected
|
||||
function GetIsSet: Boolean; virtual; abstract;
|
||||
public
|
||||
@@ -37,7 +37,8 @@ type
|
||||
strict private
|
||||
FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers waiting for this latch to be set.
|
||||
private
|
||||
[volatile] FCount: Integer; // The internal countdown value for the latch.
|
||||
[volatile]
|
||||
FCount: Integer; // The internal countdown value for the latch.
|
||||
function GetState: TState; // Implementation for IMycLatch.GetState and IMycFlag.GetState.
|
||||
protected
|
||||
function GetIsSet: Boolean; override; final; // Implementation for IMycState.GetIsSet.
|
||||
@@ -57,7 +58,7 @@ type
|
||||
function Notify: Boolean;
|
||||
end;
|
||||
|
||||
TMycNullLatch = class( TInterfacedObject, IMycLatch )
|
||||
TMycNullLatch = class(TInterfacedObject, IMycLatch)
|
||||
private
|
||||
function GetState: TState;
|
||||
function Notify: Boolean;
|
||||
@@ -70,7 +71,8 @@ type
|
||||
strict private
|
||||
FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers interested in state changes of this dirty flag.
|
||||
private
|
||||
[volatile] FFlag: Boolean; // Internal state: true if dirty/set, false if clean/reset.
|
||||
[volatile]
|
||||
FFlag: Boolean; // Internal state: true if dirty/set, false if clean/reset.
|
||||
function GetState: TState;
|
||||
protected
|
||||
function GetIsSet: Boolean; override; final; // Implementation for IMycState.GetIsSet (true if dirty).
|
||||
@@ -93,7 +95,7 @@ type
|
||||
function Reset: Boolean;
|
||||
end;
|
||||
|
||||
TMycNullDirty = class( TInterfacedObject, IMycDirty )
|
||||
TMycNullDirty = class(TInterfacedObject, IMycDirty)
|
||||
private
|
||||
function GetState: TState;
|
||||
function Notify: Boolean;
|
||||
@@ -150,7 +152,7 @@ end;
|
||||
constructor TMycLatch.Create(ACount: Integer);
|
||||
begin
|
||||
inherited Create;
|
||||
Assert( ACount >= 0 );
|
||||
Assert(ACount >= 0);
|
||||
FCount := ACount;
|
||||
FSubscribers.Create;
|
||||
end;
|
||||
@@ -177,11 +179,7 @@ begin
|
||||
|
||||
if shouldNotifySubscribers then
|
||||
begin
|
||||
FSubscribers.Notify(
|
||||
function(Subscriber: IMycSubscriber): Boolean
|
||||
begin
|
||||
Result := Subscriber.Notify;
|
||||
end);
|
||||
FSubscribers.Notify(function(Subscriber: IMycSubscriber): Boolean begin Result := Subscriber.Notify; end);
|
||||
end;
|
||||
|
||||
// Returns true if the latch has not yet been set by this Notify call (count > 0).
|
||||
@@ -315,11 +313,7 @@ begin
|
||||
|
||||
if wasPreviouslyClean then // Only notify subscribers if state changed from clean to dirty.
|
||||
begin
|
||||
FSubscribers.Notify(
|
||||
function(Subscriber: IMycSubscriber): Boolean
|
||||
begin
|
||||
Result := Subscriber.Notify;
|
||||
end);
|
||||
FSubscribers.Notify(function(Subscriber: IMycSubscriber): Boolean begin Result := Subscriber.Notify; end);
|
||||
end;
|
||||
// The return value of this Notify (as an IMycSubscriber) indicates if this
|
||||
// TMycDirty instance itself would want more notifications if it were subscribed to something.
|
||||
|
||||
+26
-23
@@ -3,11 +3,15 @@ unit Myc.Core.Tasks;
|
||||
interface
|
||||
|
||||
uses
|
||||
System.SysUtils, System.Classes, System.SyncObjs,
|
||||
Myc.Core.Atomic, Myc.TaskManager, Myc.Signals;
|
||||
System.SysUtils,
|
||||
System.Classes,
|
||||
System.SyncObjs,
|
||||
Myc.Core.Atomic,
|
||||
Myc.TaskManager,
|
||||
Myc.Signals;
|
||||
|
||||
type
|
||||
IMycTaskFactory = interface( IMycTaskManager )
|
||||
IMycTaskFactory = interface(IMycTaskManager)
|
||||
{$region 'property access'}
|
||||
// Retrieves the number of worker threads.
|
||||
function GetThreadCount: Integer;
|
||||
@@ -47,7 +51,8 @@ type
|
||||
|
||||
TMycTaskFactory = class(TInterfacedObject, IMycTaskManager, IMycTaskFactory)
|
||||
type
|
||||
ETaskException = class(Exception) end;
|
||||
ETaskException = class(Exception)
|
||||
end;
|
||||
private
|
||||
[volatile]
|
||||
FException: TObject; // Holds the first exception object from a worker thread
|
||||
@@ -139,7 +144,7 @@ begin
|
||||
for var i := 0 to High(FWorkThreads) do
|
||||
FWorkThreads[i].Start;
|
||||
|
||||
FWaitSemaphores.Push( TSemaphore.Create(nil, 0, 1, '') );
|
||||
FWaitSemaphores.Push(TSemaphore.Create(nil, 0, 1, ''));
|
||||
end;
|
||||
|
||||
destructor TMycTaskFactory.Destroy;
|
||||
@@ -159,14 +164,14 @@ end;
|
||||
|
||||
class function TMycTaskFactory.CreateAnonymousThread(const DbgName: String; const Proc: TProc): TThread;
|
||||
{$IFDEF MSWINDOWS}
|
||||
{$WARN SYMBOL_PLATFORM OFF}
|
||||
{$WARN SYMBOL_PLATFORM OFF}
|
||||
var
|
||||
prio: TThreadPriority;
|
||||
{$ENDIF MSWINDOWS}
|
||||
begin
|
||||
Result := TThread.CreateAnonymousThread(Proc);
|
||||
|
||||
{$IFDEF MSWINDOWS}
|
||||
{$IFDEF MSWINDOWS}
|
||||
// Set priority lower than MainThread priority if created from main thread
|
||||
if TThread.CurrentThread.ThreadID = MainThreadID then
|
||||
begin
|
||||
@@ -177,7 +182,7 @@ begin
|
||||
Result.Priority := prio;
|
||||
end;
|
||||
{$WARN SYMBOL_PLATFORM ON}
|
||||
{$ENDIF MSWINDOWS}
|
||||
{$ENDIF MSWINDOWS}
|
||||
|
||||
if DbgName <> '' then
|
||||
Result.NameThreadForDebugging(DbgName); // Set thread name for debugging
|
||||
@@ -187,12 +192,12 @@ function TMycTaskFactory.CreateTask(const Gate: TState; const Proc: TProc): TSta
|
||||
begin
|
||||
if Gate.IsSet then
|
||||
begin
|
||||
EnqueueJob( Proc );
|
||||
EnqueueJob(Proc);
|
||||
end
|
||||
else
|
||||
begin
|
||||
// The job will run when Gate notifies the subscriber returned by Run.
|
||||
Result := Gate.Subscribe( Run( Proc ) );
|
||||
Result := Gate.Subscribe(Run(Proc));
|
||||
end;
|
||||
end;
|
||||
|
||||
@@ -205,7 +210,8 @@ begin
|
||||
res := TLatch.Construct(1); // Changed to use direct static call on TMycLatch
|
||||
|
||||
capturedProc := Proc; // Capture Proc for the anonymous method
|
||||
CreateAnonymousThread('Thread',
|
||||
CreateAnonymousThread(
|
||||
'Thread',
|
||||
procedure
|
||||
begin
|
||||
try
|
||||
@@ -214,7 +220,8 @@ begin
|
||||
capturedProc := nil; // Clear the captured proc
|
||||
res.Notify; // Signal completion via the latch's Notify method
|
||||
end;
|
||||
end).Start;
|
||||
end)
|
||||
.Start;
|
||||
|
||||
// Return the state interface of the latch
|
||||
exit(res.State);
|
||||
@@ -286,12 +293,12 @@ class procedure TMycTaskFactory.AquireTaskManager;
|
||||
begin
|
||||
if not Assigned(TaskManager) then
|
||||
TaskManager := TMycTaskFactory.Create;
|
||||
inc( FTaskManagerLock );
|
||||
inc(FTaskManagerLock);
|
||||
end;
|
||||
|
||||
class procedure TMycTaskFactory.ReleaseTaskManager;
|
||||
begin
|
||||
dec( FTaskManagerLock );
|
||||
dec(FTaskManagerLock);
|
||||
if FTaskManagerLock = 0 then
|
||||
TaskManager := nil;
|
||||
end;
|
||||
@@ -322,11 +329,7 @@ begin
|
||||
for i := 0 to High(FWorkThreads) do
|
||||
FWorkGate.Release;
|
||||
|
||||
TSpinWait.SpinUntil(
|
||||
function: Boolean
|
||||
begin
|
||||
Result := FThreadsRunning = 0;
|
||||
end);
|
||||
TSpinWait.SpinUntil(function: Boolean begin Result := FThreadsRunning = 0; end);
|
||||
|
||||
Assert(FThreadsRunning = 0);
|
||||
|
||||
@@ -357,7 +360,8 @@ begin
|
||||
if lock = nil then
|
||||
lock := TSemaphore.Create(nil, 0, 1, '');
|
||||
try
|
||||
{var subscription :=} State.Subscribe(TMycTaskWait.Create(lock));
|
||||
{var subscription :=}
|
||||
State.Subscribe(TMycTaskWait.Create(lock));
|
||||
lock.Acquire;
|
||||
finally
|
||||
FWaitSemaphores.Push(lock);
|
||||
@@ -386,8 +390,7 @@ end;
|
||||
|
||||
{ TMycPendingJob }
|
||||
|
||||
constructor TMycPendingJob.Create(OwnerTaskFactory: TMycTaskFactory; const
|
||||
JobProc: TProc);
|
||||
constructor TMycPendingJob.Create(OwnerTaskFactory: TMycTaskFactory; const JobProc: TProc);
|
||||
begin
|
||||
inherited Create;
|
||||
FOwner := OwnerTaskFactory;
|
||||
@@ -398,7 +401,7 @@ function TMycPendingJob.Notify: Boolean;
|
||||
var
|
||||
P: TProc;
|
||||
begin
|
||||
PPointer(@P)^ := TInterlocked.Exchange( PPointer(@FJob)^, nil );
|
||||
PPointer(@P)^ := TInterlocked.Exchange(PPointer(@FJob)^, nil);
|
||||
Result := Assigned(P);
|
||||
if Result then
|
||||
begin
|
||||
|
||||
+23
-26
@@ -4,7 +4,8 @@ interface
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
Myc.Signals, Myc.TaskManager;
|
||||
Myc.Signals,
|
||||
Myc.TaskManager;
|
||||
|
||||
type
|
||||
// Represents the eventual result of an asynchronous operation.
|
||||
@@ -32,17 +33,17 @@ type
|
||||
class destructor DestroyClass;
|
||||
|
||||
public
|
||||
constructor Create( const AFuture: IMycFuture<T> );
|
||||
constructor Create(const AFuture: IMycFuture<T>);
|
||||
|
||||
class operator Implicit( const A: IMycFuture<T> ): TFuture<T>; overload;
|
||||
class operator Implicit( const A: TFuture<T> ): IMycFuture<T>; overload;
|
||||
class operator Implicit(const A: IMycFuture<T>): TFuture<T>; overload;
|
||||
class operator Implicit(const A: TFuture<T>): IMycFuture<T>; overload;
|
||||
|
||||
class function Construct( const Proc: TFunc<T> ): TFuture<T>; overload; static;
|
||||
class function Construct( const Gate: IMycState; const Proc: TFunc<T> ): TFuture<T>; overload; static;
|
||||
class function Construct(const Proc: TFunc<T>): TFuture<T>; overload; static;
|
||||
class function Construct(const Gate: IMycState; const Proc: TFunc<T>): TFuture<T>; overload; static;
|
||||
|
||||
class property Null: IMycFuture<T> read FNull;
|
||||
|
||||
function Chain<S>( const Proc: TFunc<T, S> ): TFuture<S>;
|
||||
function Chain<S>(const Proc: TFunc<T, S>): TFuture<S>;
|
||||
|
||||
function WaitFor: T;
|
||||
|
||||
@@ -53,12 +54,13 @@ type
|
||||
implementation
|
||||
|
||||
uses
|
||||
Myc.Core.Futures, Myc.Core.Tasks;
|
||||
Myc.Core.Futures,
|
||||
Myc.Core.Tasks;
|
||||
|
||||
constructor TFuture<T>.Create( const AFuture: IMycFuture<T> );
|
||||
constructor TFuture<T>.Create(const AFuture: IMycFuture<T>);
|
||||
begin
|
||||
FFuture := AFuture;
|
||||
if not Assigned( FFuture ) then
|
||||
if not Assigned(FFuture) then
|
||||
FFuture := FNull;
|
||||
end;
|
||||
|
||||
@@ -73,26 +75,21 @@ begin
|
||||
TMycTaskFactory.ReleaseTaskManager;
|
||||
end;
|
||||
|
||||
function TFuture<T>.Chain<S>( const Proc: TFunc<T, S> ): TFuture<S>;
|
||||
function TFuture<T>.Chain<S>(const Proc: TFunc<T, S>): TFuture<S>;
|
||||
begin
|
||||
var
|
||||
Cap := FFuture;
|
||||
var Cap := FFuture;
|
||||
|
||||
Result := TFuture<S>.Construct( FFuture.Done,
|
||||
function: S
|
||||
begin
|
||||
Result := Proc( Cap.Result );
|
||||
end );
|
||||
Result := TFuture<S>.Construct(FFuture.Done, function: S begin Result := Proc(Cap.Result); end);
|
||||
end;
|
||||
|
||||
class function TFuture<T>.Construct( const Proc: TFunc<T> ): TFuture<T>;
|
||||
class function TFuture<T>.Construct(const Proc: TFunc<T>): TFuture<T>;
|
||||
begin
|
||||
Result := TMycGateFuncFuture<T>.Create( TaskManager, nil, Proc );
|
||||
Result := TMycGateFuncFuture<T>.Create(TaskManager, nil, Proc);
|
||||
end;
|
||||
|
||||
class function TFuture<T>.Construct( const Gate: IMycState; const Proc: TFunc<T> ): TFuture<T>;
|
||||
class function TFuture<T>.Construct(const Gate: IMycState; const Proc: TFunc<T>): TFuture<T>;
|
||||
begin
|
||||
Result := TMycGateFuncFuture<T>.Create( TaskManager, Gate, Proc );
|
||||
Result := TMycGateFuncFuture<T>.Create(TaskManager, Gate, Proc);
|
||||
end;
|
||||
|
||||
function TFuture<T>.GetDone: IMycState;
|
||||
@@ -107,16 +104,16 @@ end;
|
||||
|
||||
function TFuture<T>.WaitFor: T;
|
||||
begin
|
||||
TaskManager.WaitFor( FFuture.Done );
|
||||
TaskManager.WaitFor(FFuture.Done);
|
||||
Result := FFuture.Result;
|
||||
end;
|
||||
|
||||
class operator TFuture<T>.Implicit( const A: IMycFuture<T> ): TFuture<T>;
|
||||
class operator TFuture<T>.Implicit(const A: IMycFuture<T>): TFuture<T>;
|
||||
begin
|
||||
Result.Create( A );
|
||||
Result.Create(A);
|
||||
end;
|
||||
|
||||
class operator TFuture<T>.Implicit( const A: TFuture<T> ): IMycFuture<T>;
|
||||
class operator TFuture<T>.Implicit(const A: TFuture<T>): IMycFuture<T>;
|
||||
begin
|
||||
Result := A.FFuture;
|
||||
end;
|
||||
|
||||
+15
-15
@@ -11,7 +11,7 @@ type
|
||||
{$REGION 'property access'}
|
||||
function GetChanged: IMycState;
|
||||
{$ENDREGION}
|
||||
function Pop( out Res: T ): Boolean;
|
||||
function Pop(out Res: T): Boolean;
|
||||
property Changed: IMycState read GetChanged;
|
||||
end;
|
||||
|
||||
@@ -24,14 +24,14 @@ type
|
||||
class constructor CreateClass;
|
||||
|
||||
public
|
||||
constructor Create( const ALazy: IMycLazy<T> );
|
||||
constructor Create(const ALazy: IMycLazy<T>);
|
||||
|
||||
class function Construct( const Changing: IMycState; const Proc: TFunc<T> ): IMycLazy<T>; static;
|
||||
class function Construct(const Changing: IMycState; const Proc: TFunc<T>): IMycLazy<T>; static;
|
||||
|
||||
class operator Implicit( const A: IMycLazy<T> ): TLazy<T>; overload;
|
||||
class operator Implicit( const A: TLazy<T> ): IMycLazy<T>; overload;
|
||||
class operator Implicit(const A: IMycLazy<T>): TLazy<T>; overload;
|
||||
class operator Implicit(const A: TLazy<T>): IMycLazy<T>; overload;
|
||||
|
||||
function Pop( out Res: T ): Boolean; inline;
|
||||
function Pop(out Res: T): Boolean; inline;
|
||||
|
||||
property Changed: IMycState read GetChanged;
|
||||
end;
|
||||
@@ -41,16 +41,16 @@ implementation
|
||||
uses
|
||||
Myc.Core.Lazy;
|
||||
|
||||
constructor TLazy<T>.Create( const ALazy: IMycLazy<T> );
|
||||
constructor TLazy<T>.Create(const ALazy: IMycLazy<T>);
|
||||
begin
|
||||
FLazy := ALazy;
|
||||
if not Assigned( FLazy ) then
|
||||
if not Assigned(FLazy) then
|
||||
FLazy := FNull;
|
||||
end;
|
||||
|
||||
class function TLazy<T>.Construct( const Changing: IMycState; const Proc: TFunc<T> ): IMycLazy<T>;
|
||||
class function TLazy<T>.Construct(const Changing: IMycState; const Proc: TFunc<T>): IMycLazy<T>;
|
||||
begin
|
||||
Result := TMycFuncLazy<T>.Create( Changing, Proc );
|
||||
Result := TMycFuncLazy<T>.Create(Changing, Proc);
|
||||
end;
|
||||
|
||||
class constructor TLazy<T>.CreateClass;
|
||||
@@ -63,17 +63,17 @@ begin
|
||||
Result := FLazy.Changed;
|
||||
end;
|
||||
|
||||
function TLazy<T>.Pop( out Res: T ): Boolean;
|
||||
function TLazy<T>.Pop(out Res: T): Boolean;
|
||||
begin
|
||||
Result := FLazy.Pop( Res );
|
||||
Result := FLazy.Pop(Res);
|
||||
end;
|
||||
|
||||
class operator TLazy<T>.Implicit( const A: IMycLazy<T> ): TLazy<T>;
|
||||
class operator TLazy<T>.Implicit(const A: IMycLazy<T>): TLazy<T>;
|
||||
begin
|
||||
Result.Create( A );
|
||||
Result.Create(A);
|
||||
end;
|
||||
|
||||
class operator TLazy<T>.Implicit( const A: TLazy<T> ): IMycLazy<T>;
|
||||
class operator TLazy<T>.Implicit(const A: TLazy<T>): IMycLazy<T>;
|
||||
begin
|
||||
Result := A.FLazy;
|
||||
end;
|
||||
|
||||
+46
-46
@@ -18,8 +18,8 @@ type
|
||||
function GetIsSet: Boolean;
|
||||
{$ENDREGION}
|
||||
// Subscribes a given subscriber to this TState.
|
||||
function Subscribe( Subscriber: IMycSubscriber ): Pointer;
|
||||
procedure Unsubscribe( Tag: Pointer );
|
||||
function Subscribe(Subscriber: IMycSubscriber): Pointer;
|
||||
procedure Unsubscribe(Tag: Pointer);
|
||||
// IsSet is true if the TState has been reached or the condition is met.
|
||||
property IsSet: Boolean read GetIsSet;
|
||||
end;
|
||||
@@ -30,9 +30,9 @@ type
|
||||
private
|
||||
FState: IMycState;
|
||||
FTag: Pointer; // Tag identifying this subscription within the notifier list.
|
||||
constructor Create( const AState: IMycState; ATag: Pointer );
|
||||
constructor Create(const AState: IMycState; ATag: Pointer);
|
||||
public
|
||||
class operator Initialize( out Dest: TSubscription );
|
||||
class operator Initialize(out Dest: TSubscription);
|
||||
// Unsubscribes from the associated TState.
|
||||
procedure Unsubscribe;
|
||||
end;
|
||||
@@ -45,25 +45,25 @@ type
|
||||
FState: IMycState;
|
||||
function GetIsSet: Boolean; inline;
|
||||
public
|
||||
constructor Create( const AState: IMycState );
|
||||
constructor Create(const AState: IMycState);
|
||||
|
||||
class operator Implicit( const A: IMycState ): TState; overload;
|
||||
class operator Implicit( const A: TState ): IMycState; overload;
|
||||
class operator Implicit(const A: IMycState): TState; overload;
|
||||
class operator Implicit(const A: TState): IMycState; overload;
|
||||
|
||||
class function All( const States: TArray<TState> ): TState; static;
|
||||
class function Any( const States: TArray<TState> ): TState; static;
|
||||
class function All(const States: TArray<TState>): TState; static;
|
||||
class function Any(const States: TArray<TState>): TState; static;
|
||||
|
||||
class property Null: IMycState read FNull;
|
||||
|
||||
function Subscribe( Subscriber: IMycSubscriber ): TSubscription; inline;
|
||||
procedure Unsubscribe( Tag: Pointer ); inline;
|
||||
function Subscribe(Subscriber: IMycSubscriber): TSubscription; inline;
|
||||
procedure Unsubscribe(Tag: Pointer); inline;
|
||||
|
||||
property IsSet: Boolean read GetIsSet;
|
||||
end;
|
||||
|
||||
// IMycLatch is a specific type of flag that, once set, remains set (non-resettable).
|
||||
// It typically becomes set when an internal countdown reaches zero.
|
||||
IMycLatch = interface( IMycSubscriber )
|
||||
IMycLatch = interface(IMycSubscriber)
|
||||
// Provides access to the IMycState interface of the flag.
|
||||
function GetState: TState;
|
||||
property State: TState read GetState;
|
||||
@@ -86,7 +86,7 @@ type
|
||||
|
||||
class function Construct(Count: Integer): TLatch; static;
|
||||
|
||||
class function Enqueue(var Gate: TLatch; Count: Integer=1): TState; static;
|
||||
class function Enqueue(var Gate: TLatch; Count: Integer = 1): TState; static;
|
||||
|
||||
class property Null: IMycLatch read FNull;
|
||||
|
||||
@@ -97,7 +97,7 @@ type
|
||||
|
||||
// IMycDirty represents a resettable flag, typically indicating if a State is "dirty" (requiring attention) or "clean".
|
||||
// It inherits from IMycFlag, meaning it has a State, can be notified, and subscribed to.
|
||||
IMycDirty = interface( IMycSubscriber )
|
||||
IMycDirty = interface(IMycSubscriber)
|
||||
// Provides access to the IMycState interface of the flag.
|
||||
function GetState: TState;
|
||||
// Resets the flag to its "clean" (not set / not dirty) State.
|
||||
@@ -119,26 +119,26 @@ type
|
||||
implementation
|
||||
|
||||
uses
|
||||
Myc.Core.Notifier, Myc.Core.Signals;
|
||||
Myc.Core.Notifier,
|
||||
Myc.Core.Signals;
|
||||
|
||||
{ TState.TSubscription }
|
||||
|
||||
constructor TState.TSubscription.Create( const AState: IMycState; ATag:
|
||||
TMycNotifyList<IMycSubscriber>.TTag );
|
||||
constructor TState.TSubscription.Create(const AState: IMycState; ATag: TMycNotifyList<IMycSubscriber>.TTag);
|
||||
begin
|
||||
Assert( Assigned( AState ) );
|
||||
Assert(Assigned(AState));
|
||||
FState := AState;
|
||||
FTag := ATag;
|
||||
end;
|
||||
|
||||
procedure TState.TSubscription.Unsubscribe;
|
||||
begin
|
||||
FState.Unsubscribe( FTag );
|
||||
FState.Unsubscribe(FTag);
|
||||
FState := TState.Null;
|
||||
FTag := nil;
|
||||
end;
|
||||
|
||||
class operator TState.TSubscription.Initialize( out Dest: TSubscription );
|
||||
class operator TState.TSubscription.Initialize(out Dest: TSubscription);
|
||||
begin
|
||||
Dest.FState := TState.Null;
|
||||
Dest.FTag := nil;
|
||||
@@ -146,42 +146,42 @@ end;
|
||||
|
||||
{ TState }
|
||||
|
||||
constructor TState.Create( const AState: IMycState );
|
||||
constructor TState.Create(const AState: IMycState);
|
||||
begin
|
||||
FState := AState;
|
||||
if not Assigned( FState ) then
|
||||
if not Assigned(FState) then
|
||||
FState := FNull;
|
||||
end;
|
||||
|
||||
class constructor TState.ClassCreate;
|
||||
begin
|
||||
// Create a singleton null latch instance that is initially (and always) set.
|
||||
FNull := TMycNullState.Create( ); // Calls the instance constructor
|
||||
FNull := TMycNullState.Create(); // Calls the instance constructor
|
||||
end;
|
||||
|
||||
class function TState.All( const States: TArray<TState> ): TState;
|
||||
class function TState.All(const States: TArray<TState>): TState;
|
||||
var
|
||||
Latch: IMycLatch;
|
||||
begin
|
||||
Latch := TLatch.Construct( Length( States ) );
|
||||
for var i := 0 to High( States ) do
|
||||
States[i].Subscribe( Latch );
|
||||
Latch := TLatch.Construct(Length(States));
|
||||
for var i := 0 to High(States) do
|
||||
States[i].Subscribe(Latch);
|
||||
Result := Latch.State;
|
||||
end;
|
||||
|
||||
class function TState.Any( const States: TArray<TState> ): TState;
|
||||
class function TState.Any(const States: TArray<TState>): TState;
|
||||
var
|
||||
Latch: IMycLatch;
|
||||
begin
|
||||
if Length( States ) = 0 then
|
||||
if Length(States) = 0 then
|
||||
begin
|
||||
Result := TState.Null;
|
||||
end
|
||||
else
|
||||
begin
|
||||
Latch := TLatch.Construct( 1 );
|
||||
for var i := 0 to High( States ) do
|
||||
States[i].Subscribe( Latch );
|
||||
Latch := TLatch.Construct(1);
|
||||
for var i := 0 to High(States) do
|
||||
States[i].Subscribe(Latch);
|
||||
Result := Latch.State;
|
||||
end;
|
||||
end;
|
||||
@@ -191,24 +191,24 @@ begin
|
||||
Result := FState.IsSet;
|
||||
end;
|
||||
|
||||
function TState.Subscribe( Subscriber: IMycSubscriber ): TSubscription;
|
||||
function TState.Subscribe(Subscriber: IMycSubscriber): TSubscription;
|
||||
begin
|
||||
Result := TSubscription.Create( FState, FState.Subscribe( Subscriber ) );
|
||||
Result := TSubscription.Create(FState, FState.Subscribe(Subscriber));
|
||||
end;
|
||||
|
||||
procedure TState.Unsubscribe( Tag: Pointer );
|
||||
procedure TState.Unsubscribe(Tag: Pointer);
|
||||
begin
|
||||
FState.Unsubscribe( Tag );
|
||||
FState.Unsubscribe(Tag);
|
||||
end;
|
||||
|
||||
class operator TState.Implicit( const A: TState ): IMycState;
|
||||
class operator TState.Implicit(const A: TState): IMycState;
|
||||
begin
|
||||
Result := A.FState;
|
||||
end;
|
||||
|
||||
class operator TState.Implicit( const A: IMycState ): TState;
|
||||
class operator TState.Implicit(const A: IMycState): TState;
|
||||
begin
|
||||
Result.Create( A );
|
||||
Result.Create(A);
|
||||
end;
|
||||
|
||||
{ TLatch }
|
||||
@@ -224,24 +224,24 @@ end;
|
||||
constructor TLatch.Create(const ALatch: IMycLatch);
|
||||
begin
|
||||
FLatch := ALatch;
|
||||
if not Assigned( FLatch ) then
|
||||
if not Assigned(FLatch) then
|
||||
FLatch := FNull;
|
||||
end;
|
||||
|
||||
class function TLatch.Construct(Count: Integer): TLatch;
|
||||
begin
|
||||
if Count > 0 then
|
||||
Result := TMycLatch.Create( Count )
|
||||
Result := TMycLatch.Create(Count)
|
||||
else
|
||||
Result := FNull;
|
||||
end;
|
||||
|
||||
class function TLatch.Enqueue(var Gate: TLatch; Count: Integer=1): TState;
|
||||
class function TLatch.Enqueue(var Gate: TLatch; Count: Integer = 1): TState;
|
||||
begin
|
||||
var gateState := Gate.State;
|
||||
Gate := TMycLatch.Create( Count );
|
||||
gateState.Subscribe( Gate );
|
||||
exit( Gate.State );
|
||||
Gate := TMycLatch.Create(Count);
|
||||
gateState.Subscribe(Gate);
|
||||
exit(Gate.State);
|
||||
end;
|
||||
|
||||
function TLatch.GetState: TState;
|
||||
@@ -256,7 +256,7 @@ end;
|
||||
|
||||
class operator TLatch.Implicit(const A: IMycLatch): TLatch;
|
||||
begin
|
||||
Result.Create( A );
|
||||
Result.Create(A);
|
||||
end;
|
||||
|
||||
class operator TLatch.Implicit(const A: TLatch): IMycLatch;
|
||||
|
||||
@@ -9,7 +9,7 @@ uses
|
||||
type
|
||||
IMycTaskManager = interface
|
||||
// TOD Dokumentation
|
||||
function CreateTask( const Gate: TState; const Proc: TProc ): TState.TSubscription;
|
||||
function CreateTask(const Gate: TState; const Proc: TProc): TState.TSubscription;
|
||||
|
||||
// Waits for the operation associated with State to complete.
|
||||
// Must not be called from a worker thread of this factory.
|
||||
@@ -74,12 +74,12 @@ end;
|
||||
|
||||
function TMycTaskManagerMock.CreateTask(const Gate: TState; const Proc: TProc): TState.TSubscription;
|
||||
begin
|
||||
Result := Gate.Subscribe( TMycExecMock.Create( Proc ) );
|
||||
Result := Gate.Subscribe(TMycExecMock.Create(Proc));
|
||||
end;
|
||||
|
||||
procedure TMycTaskManagerMock.WaitFor(State: IMycState);
|
||||
begin
|
||||
Assert( State.IsSet );
|
||||
Assert(State.IsSet);
|
||||
end;
|
||||
|
||||
procedure SetupTaskManagerMock;
|
||||
|
||||
@@ -109,7 +109,8 @@ type
|
||||
|
||||
implementation
|
||||
|
||||
uses System.TypInfo;
|
||||
uses
|
||||
System.TypInfo;
|
||||
|
||||
{ TTestSList }
|
||||
|
||||
@@ -154,7 +155,7 @@ begin
|
||||
// Free all allocated PTestSListEntryData
|
||||
for entryNode in FEntries do
|
||||
begin
|
||||
var P:= entryNode;
|
||||
var P := entryNode;
|
||||
FreeMemAligned(P);
|
||||
end;
|
||||
SetLength(FEntries, 0);
|
||||
@@ -277,10 +278,11 @@ begin
|
||||
// This test confirms that using an aligned entry from AllocEntryData works.
|
||||
entryData := AllocEntryData(123);
|
||||
// This should not raise an assertion error from TSList.Push.
|
||||
Assert.WillNotRaise(procedure
|
||||
begin
|
||||
FList.Push(@entryData.Entry);
|
||||
end, nil, 'Pushing an aligned entry should not raise an exception/assertion.');
|
||||
Assert.WillNotRaise(
|
||||
procedure begin FList.Push(@entryData.Entry); end,
|
||||
nil,
|
||||
'Pushing an aligned entry should not raise an exception/assertion.'
|
||||
);
|
||||
Assert.AreEqual(1, FList.QueryDepth, 'QueryDepth should be 1 after pushing an aligned entry.');
|
||||
end;
|
||||
|
||||
@@ -391,10 +393,7 @@ end;
|
||||
procedure TTestMycAtomicStack_Integer.TestClear_OnEmptyStack;
|
||||
begin
|
||||
// Clearing an already empty stack should not cause issues
|
||||
Assert.WillNotRaise(procedure
|
||||
begin
|
||||
FStack.Clear;
|
||||
end, nil, 'Clear on an empty stack should not raise an exception.');
|
||||
Assert.WillNotRaise(procedure begin FStack.Clear; end, nil, 'Clear on an empty stack should not raise an exception.');
|
||||
Assert.AreEqual(Default(Integer), FStack.Pop, 'Stack should remain empty after Clear on empty stack.');
|
||||
end;
|
||||
|
||||
|
||||
+48
-23
@@ -186,12 +186,15 @@ begin
|
||||
sourceDirty.Reset;
|
||||
procExecuted := False;
|
||||
expectedValue := 50;
|
||||
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State,
|
||||
funcLazy :=
|
||||
TMycFuncLazy<Integer>.Create(
|
||||
sourceDirty.State,
|
||||
function: Integer
|
||||
begin
|
||||
procExecuted := True;
|
||||
Result := expectedValue;
|
||||
end);
|
||||
end
|
||||
);
|
||||
Assert.IsNotNull(funcLazy, 'TMycFuncLazy<Integer> instance should not be nil');
|
||||
Assert.IsTrue(funcLazy.GetChanged.IsSet, 'Changed.IsSet should be true before the first Pop by design');
|
||||
value := 0;
|
||||
@@ -219,7 +222,11 @@ begin
|
||||
value := preCallValue;
|
||||
result := funcLazy.Pop(value);
|
||||
Assert.IsTrue(result, 'First Pop should return true by design, even if FProc is nil');
|
||||
Assert.AreEqual(preCallValue, value, 'Value should be unchanged as FProc was nil and current Pop implementation does not assign Default(T)');
|
||||
Assert.AreEqual(
|
||||
preCallValue,
|
||||
value,
|
||||
'Value should be unchanged as FProc was nil and current Pop implementation does not assign Default(T)'
|
||||
);
|
||||
Assert.IsFalse(funcLazy.GetChanged.IsSet, 'Changed state should be false after Pop');
|
||||
end;
|
||||
|
||||
@@ -252,12 +259,15 @@ begin
|
||||
sourceDirty.Reset;
|
||||
initialProcValue := 44;
|
||||
procExecutedCount := 0;
|
||||
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State,
|
||||
funcLazy :=
|
||||
TMycFuncLazy<Integer>.Create(
|
||||
sourceDirty.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(procExecutedCount);
|
||||
Result := initialProcValue;
|
||||
end);
|
||||
end
|
||||
);
|
||||
Helper_ConsumeInitialPop(funcLazy, initialProcValue);
|
||||
Assert.AreEqual(1, procExecutedCount, 'Proc should have executed once for initial pop');
|
||||
result := funcLazy.Pop(value);
|
||||
@@ -294,13 +304,18 @@ begin
|
||||
sourceDirty := TDirty.Construct;
|
||||
sourceDirty.Reset;
|
||||
procCallCount := 0;
|
||||
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State,
|
||||
funcLazy :=
|
||||
TMycFuncLazy<Integer>.Create(
|
||||
sourceDirty.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(procCallCount);
|
||||
if procCallCount = 1 then Result := 30
|
||||
else Result := 300 + procCallCount;
|
||||
end);
|
||||
if procCallCount = 1 then
|
||||
Result := 30
|
||||
else
|
||||
Result := 300 + procCallCount;
|
||||
end
|
||||
);
|
||||
currentExpectedValue := 30;
|
||||
Helper_ConsumeInitialPop(funcLazy, currentExpectedValue);
|
||||
Assert.AreEqual(1, procCallCount, 'Proc executed for initial Pop');
|
||||
@@ -326,12 +341,15 @@ begin
|
||||
sourceDirty := TDirty.Construct;
|
||||
sourceDirty.Reset;
|
||||
procCallCount := 0;
|
||||
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State,
|
||||
funcLazy :=
|
||||
TMycFuncLazy<Integer>.Create(
|
||||
sourceDirty.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(procCallCount);
|
||||
Result := 40;
|
||||
end);
|
||||
end
|
||||
);
|
||||
resultPop1 := funcLazy.Pop(value);
|
||||
Assert.IsTrue(resultPop1, 'First Pop should return true by design');
|
||||
Assert.AreEqual(40, value, 'Value from first Pop');
|
||||
@@ -359,14 +377,20 @@ begin
|
||||
initialValue := 51;
|
||||
firstSourceChangeValue := 52;
|
||||
|
||||
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State,
|
||||
funcLazy :=
|
||||
TMycFuncLazy<Integer>.Create(
|
||||
sourceDirty.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(procCallCount);
|
||||
if procCallCount = 1 then Result := initialValue // For initial pop
|
||||
else if procCallCount = 2 then Result := firstSourceChangeValue // For pop after first effective source change
|
||||
else Result := 999; // Should not be reached in this specific test logic
|
||||
end);
|
||||
if procCallCount = 1 then
|
||||
Result := initialValue // For initial pop
|
||||
else if procCallCount = 2 then
|
||||
Result := firstSourceChangeValue // For pop after first effective source change
|
||||
else
|
||||
Result := 999; // Should not be reached in this specific test logic
|
||||
end
|
||||
);
|
||||
|
||||
// 1. Initial Pop (consumes "by design" changed state)
|
||||
Assert.IsTrue(funcLazy.GetChanged.IsSet, 'Changed state should be true before first Pop (by design)');
|
||||
@@ -412,12 +436,10 @@ begin
|
||||
Assert.IsTrue(funcLazyObj.Pop(tempVal), 'Initial Pop should succeed');
|
||||
funcLazyObj.Destroy;
|
||||
Assert.WillNotRaise(
|
||||
procedure
|
||||
begin
|
||||
sourceDirty.Notify;
|
||||
end,
|
||||
procedure begin sourceDirty.Notify; end,
|
||||
nil,
|
||||
'Destroy test assumes TMycSubscription.Unsubscribe works. Verified by no crash on source notify post-destroy.');
|
||||
'Destroy test assumes TMycSubscription.Unsubscribe works. Verified by no crash on source notify post-destroy.'
|
||||
);
|
||||
sourceDirty := nil;
|
||||
end;
|
||||
|
||||
@@ -434,12 +456,15 @@ begin
|
||||
Assert.IsTrue(sourceDirty.State.IsSet, 'SourceDirty should be initially set for this test scenario');
|
||||
expectedValue := 70;
|
||||
procExecuted := False;
|
||||
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State,
|
||||
funcLazy :=
|
||||
TMycFuncLazy<Integer>.Create(
|
||||
sourceDirty.State,
|
||||
function: Integer
|
||||
begin
|
||||
procExecuted := True;
|
||||
Result := expectedValue;
|
||||
end);
|
||||
end
|
||||
);
|
||||
Assert.IsNotNull(funcLazy, 'TMycFuncLazy<Integer> instance should not be nil');
|
||||
Assert.IsTrue(funcLazy.GetChanged.IsSet, 'funcLazy.GetChanged.IsSet should be true after creation (by design)');
|
||||
value := 0;
|
||||
|
||||
+32
-14
@@ -145,12 +145,15 @@ var
|
||||
begin
|
||||
procExecuted := False;
|
||||
// FChangingSignal is reset in Setup
|
||||
lazyIntf := TLazy<Integer>.Construct(FChangingSignal.State,
|
||||
lazyIntf :=
|
||||
TLazy<Integer>.Construct(
|
||||
FChangingSignal.State,
|
||||
function: Integer
|
||||
begin
|
||||
procExecuted := True;
|
||||
Result := 10;
|
||||
end);
|
||||
end
|
||||
);
|
||||
Assert.IsNotNull(lazyIntf, 'TLazy.Construct should return a valid interface');
|
||||
|
||||
lazyRec := lazyIntf; // Implicit conversion
|
||||
@@ -167,12 +170,15 @@ var
|
||||
begin
|
||||
procExecuted := False;
|
||||
expectedValue := 20;
|
||||
lazyIntf := TLazy<Integer>.Construct(FChangingSignal.State,
|
||||
lazyIntf :=
|
||||
TLazy<Integer>.Construct(
|
||||
FChangingSignal.State,
|
||||
function: Integer
|
||||
begin
|
||||
procExecuted := True;
|
||||
Result := expectedValue;
|
||||
end);
|
||||
end
|
||||
);
|
||||
lazyRec := lazyIntf;
|
||||
|
||||
ConsumeInitialPop(lazyRec, expectedValue, 'TestConstruct_FirstPop');
|
||||
@@ -229,12 +235,15 @@ var
|
||||
procCallCount: Integer;
|
||||
begin
|
||||
procCallCount := 0;
|
||||
lazyIntf := TLazy<Integer>.Construct(FChangingSignal.State,
|
||||
lazyIntf :=
|
||||
TLazy<Integer>.Construct(
|
||||
FChangingSignal.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(procCallCount);
|
||||
Result := 100 + procCallCount; // Value changes per call
|
||||
end);
|
||||
end
|
||||
);
|
||||
lazyRec := lazyIntf;
|
||||
|
||||
ConsumeInitialPop(lazyRec, 101, 'TestConstruct_StateInteraction_PopResetsChangedAfterSignal (Initial)'); // procCallCount = 1
|
||||
@@ -257,12 +266,15 @@ var
|
||||
procCallCount: Integer;
|
||||
begin
|
||||
procCallCount := 0;
|
||||
lazyIntf := TLazy<Integer>.Construct(FChangingSignal.State,
|
||||
lazyIntf :=
|
||||
TLazy<Integer>.Construct(
|
||||
FChangingSignal.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(procCallCount);
|
||||
Result := 200 + procCallCount;
|
||||
end);
|
||||
end
|
||||
);
|
||||
lazyRec := lazyIntf;
|
||||
|
||||
// 1. Initial Pop
|
||||
@@ -315,12 +327,12 @@ begin
|
||||
localChangingSignal.Notify; // Notify the source AFTER the lazy object is supposed to be gone.
|
||||
end,
|
||||
nil, // Default: any exception is a failure
|
||||
'Notifying source after lazy object is freed should not crash, indicating unsubscription.');
|
||||
'Notifying source after lazy object is freed should not crash, indicating unsubscription.'
|
||||
);
|
||||
|
||||
localChangingSignal := nil; // Clean up the local signal itself.
|
||||
end;
|
||||
|
||||
|
||||
// == Tests for TLazy<T>.Create with a pre-existing (non-nil) IMycLazy ==
|
||||
|
||||
procedure TTestMyLazy.TestCreateWithExistingLazy_DelegatesChangedCorrectly;
|
||||
@@ -355,12 +367,15 @@ var
|
||||
procCallCount: Integer;
|
||||
begin
|
||||
procCallCount := 0;
|
||||
originalLazyIntf := TLazy<Integer>.Construct(FChangingSignal.State,
|
||||
originalLazyIntf :=
|
||||
TLazy<Integer>.Construct(
|
||||
FChangingSignal.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(procCallCount);
|
||||
Result := 70 + procCallCount;
|
||||
end);
|
||||
end
|
||||
);
|
||||
wrappedLazyRec := TLazy<Integer>.Create(originalLazyIntf);
|
||||
|
||||
// First Pop via wrapper (initial pop)
|
||||
@@ -434,12 +449,15 @@ var
|
||||
procExecuted: Boolean;
|
||||
begin
|
||||
procExecuted := False;
|
||||
lazyIntf := TLazy<Integer>.Construct(FChangingSignal.State,
|
||||
lazyIntf :=
|
||||
TLazy<Integer>.Construct(
|
||||
FChangingSignal.State,
|
||||
function: Integer
|
||||
begin
|
||||
procExecuted := True;
|
||||
Result := 100;
|
||||
end);
|
||||
end
|
||||
);
|
||||
lazyRec := lazyIntf;
|
||||
|
||||
ConsumeInitialPop(lazyRec, 100, 'TestPop_AfterInitialAndNoSignal (Initial)');
|
||||
|
||||
+5
-5
@@ -1,18 +1,18 @@
|
||||
program MycTests;
|
||||
|
||||
{$IFNDEF TESTINSIGHT}
|
||||
{$APPTYPE CONSOLE}
|
||||
{$APPTYPE CONSOLE}
|
||||
{$ENDIF}
|
||||
{$STRONGLINKTYPES ON}
|
||||
uses
|
||||
FastMM5,
|
||||
DUnitX.MemoryLeakMonitor.FastMM5,
|
||||
System.SysUtils,
|
||||
{$IFDEF TESTINSIGHT}
|
||||
{$IFDEF TESTINSIGHT}
|
||||
TestInsight.DUnitX,
|
||||
{$ELSE}
|
||||
{$ELSE}
|
||||
DUnitX.Loggers.Console,
|
||||
{$ENDIF }
|
||||
{$ENDIF }
|
||||
DUnitX.TestFramework,
|
||||
TestNotifier in 'TestNotifier.pas',
|
||||
TestNotifier_Threading in 'TestNotifier_Threading.pas' {/TestNotifier_ChaosStress in 'TestNotifier_ChaosStress.pas',},
|
||||
@@ -39,7 +39,7 @@ var
|
||||
runner: ITestRunner;
|
||||
results: IRunResults;
|
||||
logger: ITestLogger;
|
||||
nunitLogger : ITestLogger;
|
||||
nunitLogger: ITestLogger;
|
||||
{$ENDIF}
|
||||
begin
|
||||
{$IFDEF TESTINSIGHT}
|
||||
|
||||
+60
-31
@@ -4,11 +4,13 @@ interface
|
||||
|
||||
uses
|
||||
DUnitX.TestFramework,
|
||||
System.SysUtils, System.Generics.Collections, // Added for TObjectList if needed, not strictly for this
|
||||
System.SysUtils,
|
||||
System.Generics.Collections, // Added for TObjectList if needed, not strictly for this
|
||||
System.SyncObjs,
|
||||
Myc.Signals, // For IMycLatch, TMycLatch, IMycState, IMycSubscriber [cite: 62, 68, 53, 45]
|
||||
Myc.Core.Tasks, // For IMycTaskFactory, TMycTaskFactory [cite: 97, 113]
|
||||
Myc.Futures, Myc.Core.Futures; // For IMycFuture, TMycInitStateFuncFuture
|
||||
Myc.Futures,
|
||||
Myc.Core.Futures; // For IMycFuture, TMycInitStateFuncFuture
|
||||
|
||||
type
|
||||
// Helper class to notify a latch when its Notify method is called.
|
||||
@@ -96,12 +98,16 @@ begin
|
||||
// Use TMycLatch.Null for an already set init state [cite: 71, 81, 206, 216, 324, 334]
|
||||
LInitStateAsState := TState.Null;
|
||||
|
||||
LFuture := TMycGateFuncFuture<Integer>.Create(FTaskFactory, LInitStateAsState,
|
||||
LFuture :=
|
||||
TMycGateFuncFuture<Integer>.Create(
|
||||
FTaskFactory,
|
||||
LInitStateAsState,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(Self.FProcExecutionCount);
|
||||
Result := CExpectedResult;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
FTaskFactory.WaitFor(LFuture.Done); // Accesses IMycFuture.GetDone [cite: 110, 234, 352]
|
||||
|
||||
@@ -122,12 +128,16 @@ const
|
||||
begin
|
||||
LInitLatch := TLatch.Construct(1); // Create an init state that is not yet set [cite: 77, 212, 330]
|
||||
|
||||
LFuture := TMycGateFuncFuture<string>.Create(FTaskFactory, LInitLatch.State,
|
||||
LFuture :=
|
||||
TMycGateFuncFuture<string>.Create(
|
||||
FTaskFactory,
|
||||
LInitLatch.State,
|
||||
function: string
|
||||
begin
|
||||
Inc(Self.FProcExecutionCount);
|
||||
Result := CExpectedResult;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
Assert.AreEqual(0, FProcExecutionCount, 'AProc should not have executed yet.');
|
||||
Assert.IsFalse(LFuture.Done.IsSet, 'Future should not be done yet.');
|
||||
@@ -157,12 +167,16 @@ begin
|
||||
LLocalTaskFactory := TMycTaskFactory.Create;
|
||||
LInitStateAsState := TState.Null; // Immediate execution
|
||||
|
||||
LFuture := TMycGateFuncFuture<Integer>.Create(LLocalTaskFactory, LInitStateAsState,
|
||||
LFuture :=
|
||||
TMycGateFuncFuture<Integer>.Create(
|
||||
LLocalTaskFactory,
|
||||
LInitStateAsState,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(Self.FProcExecutionCount);
|
||||
raise Exception.Create(CExceptionMessage);
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
try
|
||||
LLocalTaskFactory.WaitFor(LFuture.Done);
|
||||
@@ -210,30 +224,26 @@ var
|
||||
begin
|
||||
LInitLatch := TLatch.Construct(1);
|
||||
|
||||
LFuture := TMycGateFuncFuture<Integer>.Create(FTaskFactory, LInitLatch.State,
|
||||
LFuture :=
|
||||
TMycGateFuncFuture<Integer>.Create(
|
||||
FTaskFactory,
|
||||
LInitLatch.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(Self.FProcExecutionCount);
|
||||
Result := 123;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsFalse(LFuture.Done.IsSet, 'Future should not be marked as done initially.');
|
||||
|
||||
Assert.WillRaise(
|
||||
procedure
|
||||
begin
|
||||
LFuture.GetResult;
|
||||
end );
|
||||
Assert.WillRaise(procedure begin LFuture.GetResult; end);
|
||||
|
||||
LInitLatch.Notify;
|
||||
|
||||
FTaskFactory.WaitFor(LFuture.Done);
|
||||
|
||||
Assert.WillNotRaise(
|
||||
procedure
|
||||
begin
|
||||
LFuture.GetResult;
|
||||
end );
|
||||
Assert.WillNotRaise(procedure begin LFuture.GetResult; end);
|
||||
end;
|
||||
|
||||
procedure TTestMycGateFuncFuture.Test_FanIn_OneFutureWaitsForMultipleOthers;
|
||||
@@ -253,33 +263,45 @@ begin
|
||||
LGateLatch := TLatch.Construct(2); // [cite: 77, 212, 330]
|
||||
|
||||
// Create MainFuture, AInitState is the GateLatch's state.
|
||||
LMainFuture := TMycGateFuncFuture<string>.Create(FTaskFactory, LGateLatch.State,
|
||||
LMainFuture :=
|
||||
TMycGateFuncFuture<string>.Create(
|
||||
FTaskFactory,
|
||||
LGateLatch.State,
|
||||
function: string
|
||||
begin
|
||||
Inc(Self.FProcExecutionCount, 10); // Indicate MainFuture's proc ran
|
||||
Result := CMainFutureResult;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
// Setup Prerequisite Futures
|
||||
// PrerequisiteFuture1
|
||||
LInitStateP1 := TLatch.Construct(1); // Controllable init state for PF1
|
||||
LPrerequisiteFuture1 := TMycGateFuncFuture<Integer>.Create(FTaskFactory, LInitStateP1.State,
|
||||
LPrerequisiteFuture1 :=
|
||||
TMycGateFuncFuture<Integer>.Create(
|
||||
FTaskFactory,
|
||||
LInitStateP1.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(Self.FProcExecutionCount, 1); // PF1 ran
|
||||
Result := 1;
|
||||
end);
|
||||
end
|
||||
);
|
||||
LSub1 := TLatchNotifierSubscriber.Create(LGateLatch);
|
||||
Subscriptions[1] := LPrerequisiteFuture1.Done.Subscribe(LSub1); // Subscribe to PF1's completion [cite: 56, 188, 306]
|
||||
|
||||
// PrerequisiteFuture2
|
||||
LInitStateP2 := TLatch.Construct(1); // Controllable init state for PF2
|
||||
LPrerequisiteFuture2 := TMycGateFuncFuture<Integer>.Create(FTaskFactory, LInitStateP2.State,
|
||||
LPrerequisiteFuture2 :=
|
||||
TMycGateFuncFuture<Integer>.Create(
|
||||
FTaskFactory,
|
||||
LInitStateP2.State,
|
||||
function: Integer
|
||||
begin
|
||||
Inc(Self.FProcExecutionCount, 1); // PF2 ran
|
||||
Result := 2;
|
||||
end);
|
||||
end
|
||||
);
|
||||
LSub2 := TLatchNotifierSubscriber.Create(LGateLatch);
|
||||
Subscriptions[2] := LPrerequisiteFuture2.Done.Subscribe(LSub2); // Subscribe to PF2's completion
|
||||
|
||||
@@ -294,7 +316,6 @@ begin
|
||||
Assert.IsFalse(LMainFuture.Done.IsSet, 'MainFuture should still not be done.');
|
||||
Assert.AreEqual(1, FProcExecutionCount mod 10, 'Only PF1 AProc should have run.');
|
||||
|
||||
|
||||
// Trigger PrerequisiteFuture2
|
||||
LInitStateP2.Notify; //
|
||||
FTaskFactory.WaitFor(LPrerequisiteFuture2.Done); // Ensure PF2 completes and notifies GateLatch
|
||||
@@ -325,22 +346,30 @@ begin
|
||||
LTriggerLatch := TLatch.Construct(1); // Single trigger [cite: 77, 212, 330]
|
||||
|
||||
// Future A
|
||||
LFutureA := TMycGateFuncFuture<Integer>.Create(FTaskFactory, LTriggerLatch.State,
|
||||
LFutureA :=
|
||||
TMycGateFuncFuture<Integer>.Create(
|
||||
FTaskFactory,
|
||||
LTriggerLatch.State,
|
||||
function: Integer
|
||||
begin
|
||||
LFlagFutureARan := True;
|
||||
System.SyncObjs.TInterlocked.Increment(Self.FSharedCounter); // Thread-safe increment
|
||||
Result := 100;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
// Future B
|
||||
LFutureB := TMycGateFuncFuture<Integer>.Create(FTaskFactory, LTriggerLatch.State,
|
||||
LFutureB :=
|
||||
TMycGateFuncFuture<Integer>.Create(
|
||||
FTaskFactory,
|
||||
LTriggerLatch.State,
|
||||
function: Integer
|
||||
begin
|
||||
LFlagFutureBRan := True;
|
||||
System.SyncObjs.TInterlocked.Increment(Self.FSharedCounter); // Thread-safe increment
|
||||
Result := 200;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsFalse(LFutureA.Done.IsSet, 'FutureA should not be done yet.');
|
||||
Assert.IsFalse(LFutureB.Done.IsSet, 'FutureB should not be done yet.');
|
||||
|
||||
+173
-151
@@ -16,7 +16,7 @@ uses
|
||||
type
|
||||
|
||||
[TestFixture]
|
||||
TTestFuture = class( TObject )
|
||||
TTestFuture = class(TObject)
|
||||
public
|
||||
[Setup]
|
||||
procedure Setup;
|
||||
@@ -38,9 +38,9 @@ type
|
||||
[Test]
|
||||
procedure TestMultipleChains;
|
||||
[Test]
|
||||
[TestCase( 'StringFutureTest', 'Test String' )]
|
||||
[TestCase( 'IntegerFutureTestForParam', '12345' )]
|
||||
procedure TestConstructSimple_Parametric( const ParamValue: string );
|
||||
[TestCase('StringFutureTest', 'Test String')]
|
||||
[TestCase('IntegerFutureTestForParam', '12345')]
|
||||
procedure TestConstructSimple_Parametric(const ParamValue: string);
|
||||
|
||||
[Test]
|
||||
procedure TestStress_StateAll;
|
||||
@@ -59,7 +59,7 @@ implementation
|
||||
|
||||
procedure TTestFuture.Setup;
|
||||
begin
|
||||
// SetupTaskManagerMock;
|
||||
// SetupTaskManagerMock;
|
||||
end;
|
||||
|
||||
procedure TTestFuture.TearDown;
|
||||
@@ -74,20 +74,21 @@ var
|
||||
resultValue: Integer;
|
||||
begin
|
||||
// Test construction with a simple function that returns an Integer
|
||||
fut := TFuture<Integer>.Construct( // [cite: 146]
|
||||
fut :=
|
||||
TFuture<Integer>.Construct( // [cite: 146]
|
||||
function: Integer
|
||||
begin
|
||||
TThread.Sleep( 20 ); // Simulate some background work
|
||||
TThread.Sleep(20); // Simulate some background work
|
||||
Result := 42;
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( fut.Done, 'Future.Done property should not be nil after construction.' ); // Static string [cite: 148]
|
||||
fut.WaitFor( ); // Wait for the future to complete its execution [cite: 148]
|
||||
Assert.IsNotNull(fut.Done, 'Future.Done property should not be nil after construction.'); // Static string [cite: 148]
|
||||
fut.WaitFor(); // Wait for the future to complete its execution [cite: 148]
|
||||
|
||||
Assert.IsTrue( fut.Done.IsSet, 'Future.Done.IsSet should be true after completion.' ); // Static string [cite: 148]
|
||||
Assert.IsTrue(fut.Done.IsSet, 'Future.Done.IsSet should be true after completion.'); // Static string [cite: 148]
|
||||
resultValue := fut.Result; // Retrieve the result of the future [cite: 148]
|
||||
Assert.AreEqual( 42, resultValue, 'The result of the future is not the expected value.' ); // Static string
|
||||
Assert.AreEqual(42, resultValue, 'The result of the future is not the expected value.'); // Static string
|
||||
end;
|
||||
|
||||
[Test]
|
||||
@@ -97,20 +98,22 @@ var
|
||||
resultValue: Integer;
|
||||
begin
|
||||
// Test construction with a nil gate, which should execute the task immediately
|
||||
fut := TFuture<Integer>.Construct( nil, // Explicitly providing a nil gate [cite: 147]
|
||||
fut :=
|
||||
TFuture<Integer>.Construct(
|
||||
nil, // Explicitly providing a nil gate [cite: 147]
|
||||
function: Integer
|
||||
begin
|
||||
TThread.Sleep( 20 ); // Simulate work
|
||||
TThread.Sleep(20); // Simulate work
|
||||
Result := 43;
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( fut.Done, 'Future.Done should not be nil when constructed with a nil gate.' ); // Static string [cite: 148]
|
||||
fut.WaitFor( ); // [cite: 148]
|
||||
Assert.IsNotNull(fut.Done, 'Future.Done should not be nil when constructed with a nil gate.'); // Static string [cite: 148]
|
||||
fut.WaitFor(); // [cite: 148]
|
||||
|
||||
Assert.IsTrue( fut.Done.IsSet, 'Future.Done.IsSet should be true for nil gate construct.' ); // Static string [cite: 148]
|
||||
Assert.IsTrue(fut.Done.IsSet, 'Future.Done.IsSet should be true for nil gate construct.'); // Static string [cite: 148]
|
||||
resultValue := fut.Result; // [cite: 148]
|
||||
Assert.AreEqual( 43, resultValue, 'Future result is incorrect for nil gate construct.' ); // Static string
|
||||
Assert.AreEqual(43, resultValue, 'Future result is incorrect for nil gate construct.'); // Static string
|
||||
end;
|
||||
|
||||
[Test]
|
||||
@@ -121,22 +124,24 @@ var
|
||||
resultValue: Integer;
|
||||
begin
|
||||
presetGate := TState.Null; // State.Null is an IMycState that is always set [cite: 68]
|
||||
Assert.IsTrue( presetGate.IsSet, 'The preset gate (State.Null) should be initially set.' ); // Static string [cite: 55]
|
||||
Assert.IsTrue(presetGate.IsSet, 'The preset gate (State.Null) should be initially set.'); // Static string [cite: 55]
|
||||
|
||||
// Construct a future with a gate that is already set
|
||||
fut := TFuture<Integer>.Construct( presetGate, // [cite: 147]
|
||||
fut :=
|
||||
TFuture<Integer>.Construct(
|
||||
presetGate, // [cite: 147]
|
||||
function: Integer
|
||||
begin
|
||||
Result := 44; // This should execute quickly
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( fut.Done, 'Future.Done should not be nil for preset gate construct.' ); // Static string [cite: 148]
|
||||
fut.WaitFor( ); // [cite: 148]
|
||||
Assert.IsNotNull(fut.Done, 'Future.Done should not be nil for preset gate construct.'); // Static string [cite: 148]
|
||||
fut.WaitFor(); // [cite: 148]
|
||||
|
||||
Assert.IsTrue( fut.Done.IsSet, 'Future.Done.IsSet should be true for preset gate construct.' ); // Static string [cite: 148]
|
||||
Assert.IsTrue(fut.Done.IsSet, 'Future.Done.IsSet should be true for preset gate construct.'); // Static string [cite: 148]
|
||||
resultValue := fut.Result; // [cite: 148]
|
||||
Assert.AreEqual( 44, resultValue, 'Future result is incorrect for preset gate construct.' ); // Static string
|
||||
Assert.AreEqual(44, resultValue, 'Future result is incorrect for preset gate construct.'); // Static string
|
||||
end;
|
||||
|
||||
[Test]
|
||||
@@ -146,34 +151,33 @@ var
|
||||
delayedGate: IMycLatch; // IMycLatch implements IMycState [cite: 58]
|
||||
resultValue: Integer;
|
||||
begin
|
||||
delayedGate := TLatch.Construct( 1 ); // Create a latch that requires one notification to be set [cite: 66]
|
||||
Assert.IsNotNull( delayedGate, 'The delayed gate (IMycLatch) should not be nil.' ); // Static string
|
||||
Assert.IsFalse( delayedGate.State.IsSet, 'The delayed gate should not be initially set.' ); // Static string [cite: 59, 55]
|
||||
delayedGate := TLatch.Construct(1); // Create a latch that requires one notification to be set [cite: 66]
|
||||
Assert.IsNotNull(delayedGate, 'The delayed gate (IMycLatch) should not be nil.'); // Static string
|
||||
Assert.IsFalse(delayedGate.State.IsSet, 'The delayed gate should not be initially set.'); // Static string [cite: 59, 55]
|
||||
|
||||
// Construct a future with a gate that is not yet set
|
||||
fut := TFuture<Integer>.Construct( delayedGate.State, // Get the IMycState interface from the latch [cite: 147, 59]
|
||||
function: Integer
|
||||
begin
|
||||
Result := 45;
|
||||
end
|
||||
fut :=
|
||||
TFuture<Integer>.Construct(
|
||||
delayedGate.State, // Get the IMycState interface from the latch [cite: 147, 59]
|
||||
function: Integer begin Result := 45; end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( fut.Done, 'Future.Done should not be nil for delayed gate construct.' ); // Static string [cite: 148]
|
||||
Assert.IsNotNull(fut.Done, 'Future.Done should not be nil for delayed gate construct.'); // Static string [cite: 148]
|
||||
|
||||
// Verify the future is not yet done as the gate is not set
|
||||
TThread.Sleep( 50 ); // Allow some time for task scheduling
|
||||
Assert.IsFalse( fut.Done.IsSet, 'Future.Done.IsSet should be false before the delayed gate is triggered.' );
|
||||
TThread.Sleep(50); // Allow some time for task scheduling
|
||||
Assert.IsFalse(fut.Done.IsSet, 'Future.Done.IsSet should be false before the delayed gate is triggered.');
|
||||
// Static string [cite: 148, 55]
|
||||
|
||||
delayedGate.Notify; // Trigger the latch [cite: 46, 94] (IMycSubscriber.Notify)
|
||||
|
||||
fut.WaitFor( ); // Wait for the future to complete now that the gate is set [cite: 148]
|
||||
fut.WaitFor(); // Wait for the future to complete now that the gate is set [cite: 148]
|
||||
|
||||
Assert.IsTrue( delayedGate.State.IsSet, 'The delayed gate should be set after Notify.' ); // Static string [cite: 59, 55]
|
||||
Assert.IsTrue( fut.Done.IsSet, 'Future.Done.IsSet should be true after the delayed gate is triggered.' );
|
||||
Assert.IsTrue(delayedGate.State.IsSet, 'The delayed gate should be set after Notify.'); // Static string [cite: 59, 55]
|
||||
Assert.IsTrue(fut.Done.IsSet, 'Future.Done.IsSet should be true after the delayed gate is triggered.');
|
||||
// Static string [cite: 148, 55]
|
||||
resultValue := fut.Result; // [cite: 148]
|
||||
Assert.AreEqual( 45, resultValue, 'Future result is incorrect for delayed gate construct.' ); // Static string
|
||||
Assert.AreEqual(45, resultValue, 'Future result is incorrect for delayed gate construct.'); // Static string
|
||||
end;
|
||||
|
||||
[Test]
|
||||
@@ -184,29 +188,31 @@ var
|
||||
resultValue: string;
|
||||
begin
|
||||
// Create an initial future
|
||||
fut1 := TFuture<Integer>.Construct( // [cite: 146]
|
||||
fut1 :=
|
||||
TFuture<Integer>.Construct( // [cite: 146]
|
||||
function: Integer
|
||||
begin
|
||||
TThread.Sleep( 20 ); // Simulate work
|
||||
TThread.Sleep(20); // Simulate work
|
||||
Result := 100;
|
||||
end
|
||||
);
|
||||
|
||||
// Chain a second future that depends on the result of the first
|
||||
fut2 := fut1.Chain<string>( // [cite: 147]
|
||||
function( Input: Integer ): string // This function receives the result of fut1
|
||||
fut2 :=
|
||||
fut1.Chain<string>( // [cite: 147]
|
||||
function(Input: Integer): string // This function receives the result of fut1
|
||||
begin
|
||||
TThread.Sleep( 20 ); // Simulate further work
|
||||
TThread.Sleep(20); // Simulate further work
|
||||
Result := 'Value: ' + Input.ToString; // Use ToString for converting Integer to String
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( fut2.Done, 'Chained Future.Done should not be nil.' ); // Static string [cite: 148]
|
||||
fut2.WaitFor( ); // Wait for the chained future to complete [cite: 148]
|
||||
Assert.IsNotNull(fut2.Done, 'Chained Future.Done should not be nil.'); // Static string [cite: 148]
|
||||
fut2.WaitFor(); // Wait for the chained future to complete [cite: 148]
|
||||
|
||||
Assert.IsTrue( fut2.Done.IsSet, 'Chained Future.Done.IsSet should be true after completion.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue(fut2.Done.IsSet, 'Chained Future.Done.IsSet should be true after completion.'); // Static string [cite: 148, 55]
|
||||
resultValue := fut2.Result; // [cite: 148]
|
||||
Assert.AreEqual( 'Value: 100', resultValue, 'Chained Future result is incorrect.' ); // Static string
|
||||
Assert.AreEqual('Value: 100', resultValue, 'Chained Future result is incorrect.'); // Static string
|
||||
end;
|
||||
|
||||
[Test]
|
||||
@@ -217,42 +223,39 @@ var
|
||||
delayedGate: IMycLatch;
|
||||
resultValue: string;
|
||||
begin
|
||||
delayedGate := TLatch.Construct( 1 ); // [cite: 66]
|
||||
Assert.IsFalse( delayedGate.State.IsSet, 'The delayed gate for chain test should not be initially set.' );
|
||||
delayedGate := TLatch.Construct(1); // [cite: 66]
|
||||
Assert.IsFalse(delayedGate.State.IsSet, 'The delayed gate for chain test should not be initially set.');
|
||||
// Static string [cite: 59, 55]
|
||||
|
||||
// First future depends on the delayedGate
|
||||
fut1 := TFuture<Integer>.Construct( delayedGate.State, // [cite: 147, 59]
|
||||
function: Integer
|
||||
begin
|
||||
Result := 200;
|
||||
end
|
||||
fut1 :=
|
||||
TFuture<Integer>.Construct(
|
||||
delayedGate.State, // [cite: 147, 59]
|
||||
function: Integer begin Result := 200; end
|
||||
);
|
||||
|
||||
// Second future is chained to the first
|
||||
fut2 := fut1.Chain<string>( // [cite: 147]
|
||||
function( Input: Integer ): string
|
||||
begin
|
||||
Result := 'ChainVal: ' + Input.ToString;
|
||||
end
|
||||
fut2 :=
|
||||
fut1.Chain<string>( // [cite: 147]
|
||||
function(Input: Integer): string begin Result := 'ChainVal: ' + Input.ToString; end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( fut2.Done, 'Chained (with gate) Future.Done should not be nil.' ); // Static string [cite: 148]
|
||||
Assert.IsNotNull(fut2.Done, 'Chained (with gate) Future.Done should not be nil.'); // Static string [cite: 148]
|
||||
|
||||
// Verify futures are not done yet
|
||||
TThread.Sleep( 50 );
|
||||
Assert.IsFalse( fut1.Done.IsSet, 'Initial future (gated) should not be done before gate is set.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsFalse( fut2.Done.IsSet, 'Chained future (gated) should not be done before gate is set.' ); // Static string [cite: 148, 55]
|
||||
TThread.Sleep(50);
|
||||
Assert.IsFalse(fut1.Done.IsSet, 'Initial future (gated) should not be done before gate is set.'); // Static string [cite: 148, 55]
|
||||
Assert.IsFalse(fut2.Done.IsSet, 'Chained future (gated) should not be done before gate is set.'); // Static string [cite: 148, 55]
|
||||
|
||||
delayedGate.Notify; // Trigger the gate [cite: 46, 94]
|
||||
|
||||
fut2.WaitFor( ); // Wait for the final chained future to complete [cite: 148]
|
||||
fut2.WaitFor(); // Wait for the final chained future to complete [cite: 148]
|
||||
|
||||
Assert.IsTrue( fut1.Done.IsSet, 'Initial future (gated) should be done after gate is set.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue( fut2.Done.IsSet, 'Chained future (gated) should be done after gate is set.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue(fut1.Done.IsSet, 'Initial future (gated) should be done after gate is set.'); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue(fut2.Done.IsSet, 'Chained future (gated) should be done after gate is set.'); // Static string [cite: 148, 55]
|
||||
|
||||
resultValue := fut2.Result; // [cite: 148]
|
||||
Assert.AreEqual( 'ChainVal: 200', resultValue, 'Chained future (gated) result is incorrect.' ); // Static string
|
||||
Assert.AreEqual('ChainVal: 200', resultValue, 'Chained future (gated) result is incorrect.'); // Static string
|
||||
end;
|
||||
|
||||
[Test]
|
||||
@@ -264,67 +267,71 @@ var
|
||||
finalResult: string;
|
||||
begin
|
||||
// Initial future A
|
||||
futA := TFuture<Integer>.Construct( // [cite: 146]
|
||||
futA :=
|
||||
TFuture<Integer>.Construct( // [cite: 146]
|
||||
function: Integer
|
||||
begin
|
||||
TThread.Sleep( 10 );
|
||||
TThread.Sleep(10);
|
||||
Result := 10;
|
||||
end
|
||||
);
|
||||
|
||||
// Future B, chained from A
|
||||
futB := futA.Chain<Real>( // [cite: 147]
|
||||
function( InputA: Integer ): Real
|
||||
futB :=
|
||||
futA.Chain<Real>( // [cite: 147]
|
||||
function(InputA: Integer): Real
|
||||
begin
|
||||
TThread.Sleep( 10 );
|
||||
TThread.Sleep(10);
|
||||
Result := InputA * 2.5; // Calculation: 10 * 2.5 = 25.0
|
||||
end
|
||||
);
|
||||
|
||||
// Future C, chained from B
|
||||
futC := futB.Chain<string>( // [cite: 147]
|
||||
function( InputB: Real ): string
|
||||
futC :=
|
||||
futB.Chain<string>( // [cite: 147]
|
||||
function(InputB: Real): string
|
||||
begin
|
||||
TThread.Sleep( 10 );
|
||||
Result := 'Final: ' + FloatToStr( InputB ); // Convert Real to String
|
||||
TThread.Sleep(10);
|
||||
Result := 'Final: ' + FloatToStr(InputB); // Convert Real to String
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( futC.Done, 'Multi-chained Future.Done should not be nil.' ); // Static string [cite: 148]
|
||||
futC.WaitFor( ); // Wait for the last future in the chain [cite: 148]
|
||||
Assert.IsNotNull(futC.Done, 'Multi-chained Future.Done should not be nil.'); // Static string [cite: 148]
|
||||
futC.WaitFor(); // Wait for the last future in the chain [cite: 148]
|
||||
|
||||
Assert.IsTrue( futA.Done.IsSet, 'Future A in chain should be done.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue( futB.Done.IsSet, 'Future B in chain should be done.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue( futC.Done.IsSet, 'Future C (multi-chained) should be done.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue(futA.Done.IsSet, 'Future A in chain should be done.'); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue(futB.Done.IsSet, 'Future B in chain should be done.'); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue(futC.Done.IsSet, 'Future C (multi-chained) should be done.'); // Static string [cite: 148, 55]
|
||||
|
||||
finalResult := futC.Result; // [cite: 148]
|
||||
// Ensure FloatToStr conversion is consistent for comparison
|
||||
Assert.AreEqual( 'Final: ' + FloatToStr( 25.0 ), finalResult, 'Multi-chained Future result is incorrect.' ); // Static string
|
||||
Assert.AreEqual('Final: ' + FloatToStr(25.0), finalResult, 'Multi-chained Future result is incorrect.'); // Static string
|
||||
end;
|
||||
|
||||
[Test]
|
||||
[TestCase( 'StringFutureTest', 'Test String' )]
|
||||
[TestCase( 'IntegerFutureTestForParam', '12345' )]
|
||||
procedure TTestFuture.TestConstructSimple_Parametric( const ParamValue: string );
|
||||
[TestCase('StringFutureTest', 'Test String')]
|
||||
[TestCase('IntegerFutureTestForParam', '12345')]
|
||||
procedure TTestFuture.TestConstructSimple_Parametric(const ParamValue: string);
|
||||
var
|
||||
fut: TFuture<string>;
|
||||
resultValue: string;
|
||||
begin
|
||||
// Parametric test for Future<string>
|
||||
fut := TFuture<string>.Construct( // [cite: 146]
|
||||
fut :=
|
||||
TFuture<string>.Construct( // [cite: 146]
|
||||
function: string
|
||||
begin
|
||||
TThread.Sleep( 10 );
|
||||
TThread.Sleep(10);
|
||||
Result := ParamValue; // Use the parameter in the future's function
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( fut.Done, 'Parametric Future.Done should not be nil.' ); // Static string [cite: 148]
|
||||
fut.WaitFor( ); // [cite: 148]
|
||||
Assert.IsNotNull(fut.Done, 'Parametric Future.Done should not be nil.'); // Static string [cite: 148]
|
||||
fut.WaitFor(); // [cite: 148]
|
||||
|
||||
Assert.IsTrue( fut.Done.IsSet, 'Parametric Future.Done.IsSet should be true.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsTrue(fut.Done.IsSet, 'Parametric Future.Done.IsSet should be true.'); // Static string [cite: 148, 55]
|
||||
resultValue := fut.Result; // [cite: 148]
|
||||
Assert.AreEqual( ParamValue, resultValue, 'Parametric Future result does not match input parameter.' ); // Static string
|
||||
Assert.AreEqual(ParamValue, resultValue, 'Parametric Future result does not match input parameter.'); // Static string
|
||||
end;
|
||||
|
||||
[Test]
|
||||
@@ -338,53 +345,58 @@ var
|
||||
masterFuture: TFuture<Boolean>;
|
||||
i: Integer;
|
||||
begin
|
||||
SetLength( Futures, StressTestFutureCount );
|
||||
SetLength( doneStates, StressTestFutureCount );
|
||||
SetLength(Futures, StressTestFutureCount);
|
||||
SetLength(doneStates, StressTestFutureCount);
|
||||
Randomize; // Initialize random number generator for varied delays
|
||||
|
||||
// Create multiple futures, each with a small random delay
|
||||
for i := 0 to High( Futures ) do
|
||||
for i := 0 to High(Futures) do
|
||||
begin
|
||||
// Capture loop variable for use in anonymous method
|
||||
Futures[i] := TFuture<Integer>.Construct( // [cite: 146]
|
||||
Futures[i] :=
|
||||
TFuture<Integer>.Construct( // [cite: 146]
|
||||
(
|
||||
function( captureIndex: Integer ): TFunc<Integer>
|
||||
function(captureIndex: Integer): TFunc<Integer>
|
||||
begin
|
||||
Result := function: Integer
|
||||
Result :=
|
||||
function: Integer
|
||||
var
|
||||
delay: Integer;
|
||||
begin
|
||||
delay := 10 + Random( 40 ); // Random delay between 10ms and 49ms
|
||||
TThread.Sleep( delay );
|
||||
delay := 10 + Random(40); // Random delay between 10ms and 49ms
|
||||
TThread.Sleep(delay);
|
||||
Result := captureIndex; // Return the captured index
|
||||
end;
|
||||
end )( i )
|
||||
end
|
||||
)(i)
|
||||
);
|
||||
doneStates[i] := Futures[i].Done; // [cite: 148]
|
||||
end;
|
||||
|
||||
combinedStateAll := TState.All( doneStates ); // [cite: 67]
|
||||
Assert.IsNotNull( combinedStateAll, 'State.All should return a valid IMycState.' ); // Static string
|
||||
combinedStateAll := TState.All(doneStates); // [cite: 67]
|
||||
Assert.IsNotNull(combinedStateAll, 'State.All should return a valid IMycState.'); // Static string
|
||||
|
||||
// Create a master future that waits on the combined State.All state
|
||||
masterFuture := TFuture<Boolean>.Construct( combinedStateAll, // [cite: 147]
|
||||
masterFuture :=
|
||||
TFuture<Boolean>.Construct(
|
||||
combinedStateAll, // [cite: 147]
|
||||
function: Boolean
|
||||
begin
|
||||
Result := True; // This function executes when combinedStateAll is set
|
||||
end
|
||||
);
|
||||
masterFuture.WaitFor( ); // Wait for all futures to complete via the master future [cite: 148]
|
||||
masterFuture.WaitFor(); // Wait for all futures to complete via the master future [cite: 148]
|
||||
|
||||
Assert.IsTrue( combinedStateAll.IsSet, 'Combined State.All should be set after masterFuture.WaitFor().' ); // Static string [cite: 55]
|
||||
Assert.IsTrue(combinedStateAll.IsSet, 'Combined State.All should be set after masterFuture.WaitFor().'); // Static string [cite: 55]
|
||||
|
||||
// Verify all individual futures are done and their results are correct
|
||||
for i := 0 to High( Futures ) do
|
||||
for i := 0 to High(Futures) do
|
||||
begin
|
||||
Assert.IsTrue( Futures[i].Done.IsSet, 'An individual future was not set after State.All completed.' );
|
||||
Assert.IsTrue(Futures[i].Done.IsSet, 'An individual future was not set after State.All completed.');
|
||||
// Static string [cite: 148, 55]
|
||||
if Futures[i].Done.IsSet then // Additional check to safely access Result
|
||||
begin
|
||||
Assert.AreEqual( i, Futures[i].Result, 'A future''s result was incorrect in State.All stress test.' );
|
||||
Assert.AreEqual(i, Futures[i].Result, 'A future''s result was incorrect in State.All stress test.');
|
||||
// Static string [cite: 148]
|
||||
end;
|
||||
end;
|
||||
@@ -403,65 +415,70 @@ var
|
||||
i: Integer;
|
||||
isAtLeastOneSet: Boolean;
|
||||
begin
|
||||
SetLength( Futures, StressTestFutureCount );
|
||||
SetLength( doneStates, StressTestFutureCount );
|
||||
SetLength(Futures, StressTestFutureCount);
|
||||
SetLength(doneStates, StressTestFutureCount);
|
||||
Randomize; // Initialize random number generator
|
||||
|
||||
// Create multiple futures, one of which is designed to finish quickly
|
||||
for i := 0 to High( Futures ) do
|
||||
for i := 0 to High(Futures) do
|
||||
begin
|
||||
// Capture loop variable
|
||||
Futures[i] := TFuture<Integer>.Construct( // [cite: 146]
|
||||
Futures[i] :=
|
||||
TFuture<Integer>.Construct( // [cite: 146]
|
||||
(
|
||||
function( captureIndex: Integer ): TFunc<Integer>
|
||||
function(captureIndex: Integer): TFunc<Integer>
|
||||
begin
|
||||
Result := function: Integer
|
||||
Result :=
|
||||
function: Integer
|
||||
var
|
||||
delay: Integer;
|
||||
begin
|
||||
if captureIndex = QuickFutureIndex then
|
||||
delay := 5 // Short delay for the 'quick' future
|
||||
else
|
||||
delay := 50 + Random( 100 ); // Longer random delay (50-149ms) for others
|
||||
TThread.Sleep( delay );
|
||||
delay := 50 + Random(100); // Longer random delay (50-149ms) for others
|
||||
TThread.Sleep(delay);
|
||||
Result := captureIndex;
|
||||
end;
|
||||
end )( i )
|
||||
end
|
||||
)(i)
|
||||
);
|
||||
doneStates[i] := Futures[i].Done; // [cite: 148]
|
||||
end;
|
||||
|
||||
combinedStateAny := TState.Any( doneStates ); // [cite: 68]
|
||||
Assert.IsNotNull( combinedStateAny, 'State.Any should return a valid IMycState.' ); // Static string
|
||||
combinedStateAny := TState.Any(doneStates); // [cite: 68]
|
||||
Assert.IsNotNull(combinedStateAny, 'State.Any should return a valid IMycState.'); // Static string
|
||||
|
||||
// Create a master future that waits on the combined State.Any state
|
||||
masterFuture := TFuture<Boolean>.Construct( combinedStateAny, // [cite: 147]
|
||||
masterFuture :=
|
||||
TFuture<Boolean>.Construct(
|
||||
combinedStateAny, // [cite: 147]
|
||||
function: Boolean
|
||||
begin
|
||||
Result := True; // This function executes when combinedStateAny is set
|
||||
end
|
||||
);
|
||||
masterFuture.WaitFor( ); // Wait for at least one future to complete [cite: 148]
|
||||
masterFuture.WaitFor(); // Wait for at least one future to complete [cite: 148]
|
||||
|
||||
Assert.IsTrue( combinedStateAny.IsSet, 'Combined State.Any should be set after masterFuture.WaitFor().' ); // Static string [cite: 55]
|
||||
Assert.IsTrue(combinedStateAny.IsSet, 'Combined State.Any should be set after masterFuture.WaitFor().'); // Static string [cite: 55]
|
||||
|
||||
// Verify that at least one of the original futures is now set
|
||||
isAtLeastOneSet := False;
|
||||
for i := 0 to High( Futures ) do
|
||||
for i := 0 to High(Futures) do
|
||||
begin
|
||||
if Futures[i].Done.IsSet then // [cite: 148, 55]
|
||||
begin
|
||||
isAtLeastOneSet := True;
|
||||
end;
|
||||
end;
|
||||
Assert.IsTrue( isAtLeastOneSet, 'At least one underlying future should be set after State.Any completed.' ); // Static string
|
||||
Assert.IsTrue(isAtLeastOneSet, 'At least one underlying future should be set after State.Any completed.'); // Static string
|
||||
|
||||
// It's good practice to ensure all futures complete
|
||||
for i := 0 to High( Futures ) do
|
||||
for i := 0 to High(Futures) do
|
||||
begin
|
||||
if not Futures[i].Done.IsSet then // [cite: 148, 55]
|
||||
begin
|
||||
Futures[i].WaitFor( ); // Wait for any remaining futures [cite: 148]
|
||||
Futures[i].WaitFor(); // Wait for any remaining futures [cite: 148]
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
@@ -474,32 +491,34 @@ var
|
||||
finalResult: Integer;
|
||||
begin
|
||||
// Create an outer future that, when resolved, produces another (inner) future.
|
||||
outerFuture := TFuture < TFuture < Integer >>.Construct( // [cite: 146]
|
||||
outerFuture :=
|
||||
TFuture<TFuture<Integer>>.Construct( // [cite: 146]
|
||||
function: TFuture<Integer> // This lambda returns a Future<Integer>
|
||||
begin
|
||||
TThread.Sleep( 10 ); // Simulate work for the outer future to produce the inner one
|
||||
Result := TFuture<Integer>.Construct( // [cite: 146]
|
||||
TThread.Sleep(10); // Simulate work for the outer future to produce the inner one
|
||||
Result :=
|
||||
TFuture<Integer>.Construct( // [cite: 146]
|
||||
function: Integer
|
||||
begin
|
||||
TThread.Sleep( 10 ); // Simulate work for the inner future
|
||||
TThread.Sleep(10); // Simulate work for the inner future
|
||||
Result := 123; // The final value
|
||||
end
|
||||
);
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( outerFuture.Done, 'Outer future''s Done state should not be nil.' ); // Static string [cite: 148]
|
||||
outerFuture.WaitFor( ); // Wait for the outer future to complete and yield the inner future [cite: 148]
|
||||
Assert.IsTrue( outerFuture.Done.IsSet, 'Outer future should be done after WaitFor.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsNotNull(outerFuture.Done, 'Outer future''s Done state should not be nil.'); // Static string [cite: 148]
|
||||
outerFuture.WaitFor(); // Wait for the outer future to complete and yield the inner future [cite: 148]
|
||||
Assert.IsTrue(outerFuture.Done.IsSet, 'Outer future should be done after WaitFor.'); // Static string [cite: 148, 55]
|
||||
|
||||
innerFuture := outerFuture.Result; // Retrieve the inner future [cite: 148]
|
||||
Assert.IsNotNull( innerFuture.Done, 'Inner future (from outer.Result) should have a non-nil Done state.' ); // Static string [cite: 148]
|
||||
Assert.IsNotNull(innerFuture.Done, 'Inner future (from outer.Result) should have a non-nil Done state.'); // Static string [cite: 148]
|
||||
|
||||
innerFuture.WaitFor( ); // Wait for the inner future to complete and yield the final result [cite: 148]
|
||||
Assert.IsTrue( innerFuture.Done.IsSet, 'Inner future should be done after its WaitFor.' ); // Static string [cite: 148, 55]
|
||||
innerFuture.WaitFor(); // Wait for the inner future to complete and yield the final result [cite: 148]
|
||||
Assert.IsTrue(innerFuture.Done.IsSet, 'Inner future should be done after its WaitFor.'); // Static string [cite: 148, 55]
|
||||
|
||||
finalResult := innerFuture.Result; // Retrieve the final integer result [cite: 148]
|
||||
Assert.AreEqual( 123, finalResult, 'Nested future final result from Construct is incorrect.' ); // Static string
|
||||
Assert.AreEqual(123, finalResult, 'Nested future final result from Construct is incorrect.'); // Static string
|
||||
end;
|
||||
|
||||
[Test]
|
||||
@@ -511,43 +530,46 @@ var
|
||||
finalResult: string;
|
||||
begin
|
||||
// Create an initial future
|
||||
initialFuture := TFuture<Integer>.Construct( // [cite: 146]
|
||||
initialFuture :=
|
||||
TFuture<Integer>.Construct( // [cite: 146]
|
||||
function: Integer
|
||||
begin
|
||||
TThread.Sleep( 10 );
|
||||
TThread.Sleep(10);
|
||||
Result := 77;
|
||||
end
|
||||
);
|
||||
|
||||
// Chain it with a function that itself returns a new Future<string>
|
||||
outerChainedFuture := initialFuture.Chain < TFuture < string >> ( // [cite: 147]
|
||||
function( Input: Integer ): TFuture<string> // This lambda returns a Future<string>
|
||||
outerChainedFuture :=
|
||||
initialFuture.Chain<TFuture<string>>( // [cite: 147]
|
||||
function(Input: Integer): TFuture<string> // This lambda returns a Future<string>
|
||||
begin
|
||||
TThread.Sleep( 10 ); // Simulate work in the chain function
|
||||
TThread.Sleep(10); // Simulate work in the chain function
|
||||
// Input is the result of initialFuture (77)
|
||||
Result := TFuture<string>.Construct( // [cite: 146]
|
||||
Result :=
|
||||
TFuture<string>.Construct( // [cite: 146]
|
||||
function: string
|
||||
begin
|
||||
TThread.Sleep( 10 ); // Simulate work for the inner-most future
|
||||
TThread.Sleep(10); // Simulate work for the inner-most future
|
||||
Result := 'Value: ' + Input.ToString; // Input is captured (77)
|
||||
end
|
||||
);
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsNotNull( outerChainedFuture.Done, 'Outer chained future''s Done state should not be nil.' ); // Static string [cite: 148]
|
||||
outerChainedFuture.WaitFor( ); // Wait for initialFuture to complete AND the chain function to execute [cite: 148]
|
||||
Assert.IsTrue( outerChainedFuture.Done.IsSet, 'Outer chained future should be done after WaitFor.' ); // Static string [cite: 148, 55]
|
||||
Assert.IsNotNull(outerChainedFuture.Done, 'Outer chained future''s Done state should not be nil.'); // Static string [cite: 148]
|
||||
outerChainedFuture.WaitFor(); // Wait for initialFuture to complete AND the chain function to execute [cite: 148]
|
||||
Assert.IsTrue(outerChainedFuture.Done.IsSet, 'Outer chained future should be done after WaitFor.'); // Static string [cite: 148, 55]
|
||||
|
||||
innerStringFuture := outerChainedFuture.Result; // Get the Future<string> produced by the chain function [cite: 148]
|
||||
Assert.IsNotNull( innerStringFuture.Done, 'Inner string future (from chain) should have a non-nil Done state.' );
|
||||
Assert.IsNotNull(innerStringFuture.Done, 'Inner string future (from chain) should have a non-nil Done state.');
|
||||
// Static string [cite: 148]
|
||||
|
||||
innerStringFuture.WaitFor( ); // Wait for the inner Future<string> to complete [cite: 148]
|
||||
Assert.IsTrue( innerStringFuture.Done.IsSet, 'Inner string future should be done after its WaitFor.' ); // Static string [cite: 148, 55]
|
||||
innerStringFuture.WaitFor(); // Wait for the inner Future<string> to complete [cite: 148]
|
||||
Assert.IsTrue(innerStringFuture.Done.IsSet, 'Inner string future should be done after its WaitFor.'); // Static string [cite: 148, 55]
|
||||
|
||||
finalResult := innerStringFuture.Result; // Get the final string result [cite: 148]
|
||||
Assert.AreEqual( 'Value: 77', finalResult, 'Nested future (via Chain) final result is incorrect.' ); // Static string
|
||||
Assert.AreEqual('Value: 77', finalResult, 'Nested future (via Chain) final result is incorrect.'); // Static string
|
||||
end;
|
||||
|
||||
end.
|
||||
|
||||
+59
-58
@@ -9,19 +9,19 @@ uses
|
||||
|
||||
type
|
||||
// Dummy interface and class for testing
|
||||
IMyTestInterface = interface( IInterface )
|
||||
IMyTestInterface = interface(IInterface)
|
||||
['{7A8C1A01-1C6B-4A7A-B9D8-28E6A8D02A0F}']
|
||||
// Unique GUID
|
||||
procedure Foo;
|
||||
function GetValue: Integer;
|
||||
end;
|
||||
|
||||
TMyTestReceiver = class( TInterfacedObject, IMyTestInterface )
|
||||
TMyTestReceiver = class(TInterfacedObject, IMyTestInterface)
|
||||
private
|
||||
FValue: Integer;
|
||||
FCallCount: Integer;
|
||||
public
|
||||
constructor Create( AValue: Integer );
|
||||
constructor Create(AValue: Integer);
|
||||
procedure Foo;
|
||||
function GetValue: Integer;
|
||||
property CallCount: Integer read FCallCount write FCallCount;
|
||||
@@ -53,7 +53,7 @@ implementation
|
||||
|
||||
{ TMyTestReceiver }
|
||||
|
||||
constructor TMyTestReceiver.Create( AValue: Integer );
|
||||
constructor TMyTestReceiver.Create(AValue: Integer);
|
||||
begin
|
||||
inherited Create;
|
||||
FValue := AValue;
|
||||
@@ -62,7 +62,7 @@ end;
|
||||
|
||||
procedure TMyTestReceiver.Foo;
|
||||
begin
|
||||
Inc( FCallCount );
|
||||
Inc(FCallCount);
|
||||
end;
|
||||
|
||||
function TMyTestReceiver.GetValue: Integer;
|
||||
@@ -93,7 +93,7 @@ begin
|
||||
// This call is expected to succeed without raising EAssertionFailed or other exceptions
|
||||
// if TMycNotifyList.Destroy is implemented correctly.
|
||||
FNotifier.Destroy;
|
||||
Assert.IsTrue( True, 'FNotifier.Destroy completed without raising an exception.' );
|
||||
Assert.IsTrue(True, 'FNotifier.Destroy completed without raising an exception.');
|
||||
end;
|
||||
|
||||
// Test for: Correct execution of Notify on an empty, locked, and finalized list.
|
||||
@@ -105,22 +105,23 @@ var
|
||||
dummyPredicate: TPredicate<IMyTestInterface>;
|
||||
begin
|
||||
predicateCallCount := 0;
|
||||
dummyPredicate := function( Item: IMyTestInterface ): Boolean
|
||||
dummyPredicate :=
|
||||
function(Item: IMyTestInterface): Boolean
|
||||
begin
|
||||
Inc( predicateCallCount );
|
||||
Inc(predicateCallCount);
|
||||
Result := True;
|
||||
end;
|
||||
|
||||
FNotifier.Lock;
|
||||
Assert.IsTrue( FNotifier.IsLocked, 'Notifier should be locked.' );
|
||||
Assert.IsTrue(FNotifier.IsLocked, 'Notifier should be locked.');
|
||||
|
||||
// FNotifier.Finalize; // Internally, FFirst becomes 2 (locked & finalized state bits) if it was 0 after Lock
|
||||
// Assert.IsTrue( FNotifier.IsFinalized, 'Notifier should be finalized.' );
|
||||
// FNotifier.Finalize; // Internally, FFirst becomes 2 (locked & finalized state bits) if it was 0 after Lock
|
||||
// Assert.IsTrue( FNotifier.IsFinalized, 'Notifier should be finalized.' );
|
||||
|
||||
// In a corrected Notifier, this call should not cause an AV and not call the predicate.
|
||||
FNotifier.Notify( dummyPredicate );
|
||||
Assert.AreEqual( 0, predicateCallCount, 'Notify predicate should not have been called for an empty, finalized list.' );
|
||||
Assert.IsTrue( True, 'FNotifier.Notify on finalized locked empty list completed without AV.' );
|
||||
FNotifier.Notify(dummyPredicate);
|
||||
Assert.AreEqual(0, predicateCallCount, 'Notify predicate should not have been called for an empty, finalized list.');
|
||||
Assert.IsTrue(True, 'FNotifier.Notify on finalized locked empty list completed without AV.');
|
||||
|
||||
if FNotifier.IsLocked then // Release lock for subsequent operations or cleanup
|
||||
begin
|
||||
@@ -134,14 +135,14 @@ end;
|
||||
procedure TMycTestNotifierTests.Test03_UnadviseAllOnFinalizedLockedEmptyListExecutesWithoutErrors;
|
||||
begin
|
||||
FNotifier.Lock;
|
||||
Assert.IsTrue( FNotifier.IsLocked, 'Notifier should be locked.' );
|
||||
Assert.IsTrue(FNotifier.IsLocked, 'Notifier should be locked.');
|
||||
|
||||
// FNotifier.Finalize; // FFirst becomes 2 internally
|
||||
// Assert.IsTrue( FNotifier.IsFinalized, 'Notifier should be finalized.' );
|
||||
// FNotifier.Finalize; // FFirst becomes 2 internally
|
||||
// Assert.IsTrue( FNotifier.IsFinalized, 'Notifier should be finalized.' );
|
||||
|
||||
// In a corrected Notifier, this call should not cause an AV.
|
||||
FNotifier.UnadviseAll;
|
||||
Assert.IsTrue( True, 'FNotifier.UnadviseAll on finalized locked empty list completed without AV.' );
|
||||
Assert.IsTrue(True, 'FNotifier.UnadviseAll on finalized locked empty list completed without AV.');
|
||||
|
||||
if FNotifier.IsLocked then // Release lock
|
||||
begin
|
||||
@@ -156,37 +157,37 @@ var
|
||||
tag1, tag2: TMycNotifyList<IMyTestInterface>.TTag;
|
||||
receiver1, receiver2: IMyTestInterface;
|
||||
begin
|
||||
receiver1 := TMyTestReceiver.Create( 1 );
|
||||
receiver2 := TMyTestReceiver.Create( 2 );
|
||||
receiver1 := TMyTestReceiver.Create(1);
|
||||
receiver2 := TMyTestReceiver.Create(2);
|
||||
|
||||
FNotifier.Lock;
|
||||
try
|
||||
tag1 := FNotifier.Advise( receiver1 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag1, 'Tag1 should not be nil.' );
|
||||
FNotifier.Unadvise( tag1 );
|
||||
tag1 := FNotifier.Advise(receiver1);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag1, 'Tag1 should not be nil.');
|
||||
FNotifier.Unadvise(tag1);
|
||||
|
||||
tag1 := FNotifier.Advise( receiver1 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag1, 'Tag1 should not be nil.' );
|
||||
tag1 := FNotifier.Advise(receiver1);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag1, 'Tag1 should not be nil.');
|
||||
|
||||
tag2 := FNotifier.Advise( receiver2 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag2, 'Tag2 should not be nil.' );
|
||||
Assert.AreNotEqual( tag1, tag2, 'Tag1 and Tag2 should be different.' );
|
||||
tag2 := FNotifier.Advise(receiver2);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag2, 'Tag2 should not be nil.');
|
||||
Assert.AreNotEqual(tag1, tag2, 'Tag1 and Tag2 should be different.');
|
||||
|
||||
FNotifier.Unadvise( tag1 );
|
||||
FNotifier.Unadvise( tag2 );
|
||||
FNotifier.Unadvise(tag1);
|
||||
FNotifier.Unadvise(tag2);
|
||||
|
||||
// Re-add and unadvise in different order
|
||||
tag1 := FNotifier.Advise( receiver1 );
|
||||
tag2 := FNotifier.Advise( receiver2 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag1 );
|
||||
Assert.AreNotEqual( TMycNotifyList<IMyTestInterface>.TTag( nil ), tag2 );
|
||||
FNotifier.Unadvise( tag2 );
|
||||
FNotifier.Unadvise( tag1 );
|
||||
tag1 := FNotifier.Advise(receiver1);
|
||||
tag2 := FNotifier.Advise(receiver2);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag1);
|
||||
Assert.AreNotEqual(TMycNotifyList<IMyTestInterface>.TTag(nil), tag2);
|
||||
FNotifier.Unadvise(tag2);
|
||||
FNotifier.Unadvise(tag1);
|
||||
|
||||
finally
|
||||
FNotifier.Release;
|
||||
end;
|
||||
Assert.IsFalse( FNotifier.IsLocked, 'Notifier should be unlocked.' );
|
||||
Assert.IsFalse(FNotifier.IsLocked, 'Notifier should be unlocked.');
|
||||
end;
|
||||
|
||||
// Test for: Notify calls advised items and can remove items based on the predicate.
|
||||
@@ -196,30 +197,30 @@ var
|
||||
rcv1, rcv2, rcv3: TMyTestReceiver;
|
||||
itemsProcessedCount: Integer;
|
||||
begin
|
||||
rcv1 := TMyTestReceiver.Create( 10 ); // Keep
|
||||
rcv2 := TMyTestReceiver.Create( 20 ); // Remove
|
||||
rcv3 := TMyTestReceiver.Create( 30 ); // Keep
|
||||
rcv1 := TMyTestReceiver.Create(10); // Keep
|
||||
rcv2 := TMyTestReceiver.Create(20); // Remove
|
||||
rcv3 := TMyTestReceiver.Create(30); // Keep
|
||||
|
||||
FNotifier.Lock;
|
||||
try
|
||||
FNotifier.Advise( rcv1 );
|
||||
FNotifier.Advise( rcv2 );
|
||||
FNotifier.Advise( rcv3 );
|
||||
FNotifier.Advise(rcv1);
|
||||
FNotifier.Advise(rcv2);
|
||||
FNotifier.Advise(rcv3);
|
||||
|
||||
itemsProcessedCount := 0;
|
||||
FNotifier.Notify(
|
||||
function( Item: IMyTestInterface ): Boolean
|
||||
function(Item: IMyTestInterface): Boolean
|
||||
begin
|
||||
Inc( itemsProcessedCount );
|
||||
TMyTestReceiver( Item ).Foo;
|
||||
Inc(itemsProcessedCount);
|
||||
TMyTestReceiver(Item).Foo;
|
||||
Result := Item.GetValue <> 20; // Remove item with value 20
|
||||
end
|
||||
);
|
||||
|
||||
Assert.AreEqual( 3, itemsProcessedCount, 'Notify predicate called for all 3 items.' );
|
||||
Assert.AreEqual( 1, rcv1.CallCount, 'Receiver1 called once.' );
|
||||
Assert.AreEqual( 1, rcv2.CallCount, 'Receiver2 called once (before removal).' );
|
||||
Assert.AreEqual( 1, rcv3.CallCount, 'Receiver3 called once.' );
|
||||
Assert.AreEqual(3, itemsProcessedCount, 'Notify predicate called for all 3 items.');
|
||||
Assert.AreEqual(1, rcv1.CallCount, 'Receiver1 called once.');
|
||||
Assert.AreEqual(1, rcv2.CallCount, 'Receiver2 called once (before removal).');
|
||||
Assert.AreEqual(1, rcv3.CallCount, 'Receiver3 called once.');
|
||||
|
||||
// Reset counts and check again
|
||||
rcv1.CallCount := 0;
|
||||
@@ -228,17 +229,17 @@ begin
|
||||
itemsProcessedCount := 0;
|
||||
|
||||
FNotifier.Notify(
|
||||
function( Item: IMyTestInterface ): Boolean
|
||||
function(Item: IMyTestInterface): Boolean
|
||||
begin
|
||||
Inc( itemsProcessedCount );
|
||||
TMyTestReceiver( Item ).Foo;
|
||||
Inc(itemsProcessedCount);
|
||||
TMyTestReceiver(Item).Foo;
|
||||
Result := True; // Keep remaining
|
||||
end
|
||||
);
|
||||
Assert.AreEqual( 2, itemsProcessedCount, 'Notify predicate called for 2 remaining items.' );
|
||||
Assert.AreEqual( 1, rcv1.CallCount, 'Receiver1 called again.' );
|
||||
Assert.AreEqual( 0, rcv2.CallCount, 'Receiver2 (removed) not called again.' );
|
||||
Assert.AreEqual( 1, rcv3.CallCount, 'Receiver3 called again.' );
|
||||
Assert.AreEqual(2, itemsProcessedCount, 'Notify predicate called for 2 remaining items.');
|
||||
Assert.AreEqual(1, rcv1.CallCount, 'Receiver1 called again.');
|
||||
Assert.AreEqual(0, rcv2.CallCount, 'Receiver2 (removed) not called again.');
|
||||
Assert.AreEqual(1, rcv3.CallCount, 'Receiver3 called again.');
|
||||
|
||||
finally
|
||||
FNotifier.Release;
|
||||
@@ -257,6 +258,6 @@ end;
|
||||
|
||||
initialization
|
||||
|
||||
TDUnitX.RegisterTestFixture( TMycTestNotifierTests );
|
||||
TDUnitX.RegisterTestFixture(TMycTestNotifierTests);
|
||||
|
||||
end.
|
||||
|
||||
@@ -20,8 +20,7 @@ type
|
||||
function GetInstanceID: Integer;
|
||||
function GetExpectedToBeAdvised: Boolean;
|
||||
procedure SetExpectedToBeAdvised(const Value: Boolean);
|
||||
property ExpectedToBeAdvised: Boolean read GetExpectedToBeAdvised write
|
||||
SetExpectedToBeAdvised;
|
||||
property ExpectedToBeAdvised: Boolean read GetExpectedToBeAdvised write SetExpectedToBeAdvised;
|
||||
end;
|
||||
|
||||
TMyStressReceiver = class(TInterfacedObject, IMyStressTestInterface)
|
||||
@@ -39,8 +38,7 @@ type
|
||||
function GetValue: Integer;
|
||||
function GetNotificationCount: Integer;
|
||||
function GetInstanceID: Integer;
|
||||
property ExpectedToBeAdvised: Boolean read GetExpectedToBeAdvised write
|
||||
SetExpectedToBeAdvised;
|
||||
property ExpectedToBeAdvised: Boolean read GetExpectedToBeAdvised write SetExpectedToBeAdvised;
|
||||
end;
|
||||
|
||||
// Record type to store an advised receiver and its tag
|
||||
@@ -165,7 +163,8 @@ begin
|
||||
try
|
||||
for i := 1 to FIterations do
|
||||
begin
|
||||
if Terminated then Break; // Respond to termination request
|
||||
if Terminated then
|
||||
Break; // Respond to termination request
|
||||
|
||||
op := Random(100);
|
||||
|
||||
@@ -211,21 +210,23 @@ begin
|
||||
begin
|
||||
FOwnerFixture.FNotifier.Lock;
|
||||
try
|
||||
// if not FOwnerFixture.FNotifier.IsFinalized then // Don't notify if finalized
|
||||
// if not FOwnerFixture.FNotifier.IsFinalized then // Don't notify if finalized
|
||||
begin
|
||||
FOwnerFixture.FNotifier.Notify(
|
||||
function(Item: IMyStressTestInterface): Boolean
|
||||
begin
|
||||
Item.Foo(FThreadID); // Pass ThreadID as notification type for context
|
||||
Result := True; // Keep item
|
||||
end);
|
||||
end
|
||||
);
|
||||
end;
|
||||
finally
|
||||
FOwnerFixture.FNotifier.Release;
|
||||
end;
|
||||
end;
|
||||
|
||||
if (i mod 75 = 0) then Sleep(0); // Yield occasionally to encourage context switching
|
||||
if (i mod 75 = 0) then
|
||||
Sleep(0); // Yield occasionally to encourage context switching
|
||||
end;
|
||||
except
|
||||
on E: Exception do
|
||||
@@ -250,7 +251,6 @@ end;
|
||||
// end;
|
||||
// end;
|
||||
|
||||
|
||||
{ TMycNotifierChaosStressTests }
|
||||
procedure TMycNotifierChaosStressTests.Setup;
|
||||
begin
|
||||
@@ -272,7 +272,7 @@ begin
|
||||
try
|
||||
// UnadviseAll should be safe if the IsFinalized guard is present
|
||||
// and the FFirst state is not pathological.
|
||||
// if not FNotifier.IsFinalized then
|
||||
// if not FNotifier.IsFinalized then
|
||||
begin
|
||||
FNotifier.UnadviseAll;
|
||||
end;
|
||||
@@ -337,8 +337,7 @@ begin
|
||||
begin
|
||||
// If an error occurred, Assert.IsNull would fail.
|
||||
// The message can safely use LThreadError.Message here.
|
||||
Assert.IsNull(LThreadError,
|
||||
'Thread ' + threads[i].FThreadID.ToString + ' reported an error: ' + LThreadError.Message);
|
||||
Assert.IsNull(LThreadError, 'Thread ' + threads[i].FThreadID.ToString + ' reported an error: ' + LThreadError.Message);
|
||||
end
|
||||
else
|
||||
begin
|
||||
@@ -357,14 +356,15 @@ begin
|
||||
try
|
||||
FNotifier.Lock;
|
||||
try
|
||||
// if not FNotifier.IsFinalized then
|
||||
// if not FNotifier.IsFinalized then
|
||||
begin
|
||||
FNotifier.Notify(
|
||||
function(Item: IMyStressTestInterface): Boolean
|
||||
begin
|
||||
actualLiveReceiversInNotifier.Add(Item);
|
||||
Result := True;
|
||||
end);
|
||||
end
|
||||
);
|
||||
end;
|
||||
finally
|
||||
FNotifier.Release;
|
||||
@@ -372,8 +372,14 @@ begin
|
||||
actualLiveInNotifierAtEnd := actualLiveReceiversInNotifier.Count;
|
||||
|
||||
// 2. Compare overall counts
|
||||
Assert.AreEqual(totalExpectedLiveByThreadsAtEnd, actualLiveInNotifierAtEnd,
|
||||
Format('Mismatch in live item count at end. Threads expected %d, Notifier has %d.', [totalExpectedLiveByThreadsAtEnd, actualLiveInNotifierAtEnd]));
|
||||
Assert.AreEqual(
|
||||
totalExpectedLiveByThreadsAtEnd,
|
||||
actualLiveInNotifierAtEnd,
|
||||
Format(
|
||||
'Mismatch in live item count at end. Threads expected %d, Notifier has %d.',
|
||||
[totalExpectedLiveByThreadsAtEnd, actualLiveInNotifierAtEnd]
|
||||
)
|
||||
);
|
||||
|
||||
// 3. Detailed check: Iterate all receivers ever created.
|
||||
// Their 'ExpectedToBeAdvised' flag (last known state from its managing thread)
|
||||
@@ -393,14 +399,29 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
Assert.AreEqual(receiver.ExpectedToBeAdvised, found,
|
||||
Format('Receiver ID %d: Thread expected it to be advised=%d, but its presence in Notifier is %d. NotificationCount=%d',
|
||||
[receiver.GetInstanceID, Integer(receiver.ExpectedToBeAdvised), Integer(found), receiver.GetNotificationCount]));
|
||||
Assert.AreEqual(
|
||||
receiver.ExpectedToBeAdvised,
|
||||
found,
|
||||
Format(
|
||||
'Receiver ID %d: Thread expected it to be advised=%d, but its presence in Notifier is %d. NotificationCount=%d',
|
||||
[
|
||||
receiver.GetInstanceID,
|
||||
Integer(receiver.ExpectedToBeAdvised),
|
||||
Integer(found),
|
||||
receiver.GetNotificationCount
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
// Further checks on NotificationCount could be added if specific notification patterns were expected.
|
||||
// For this chaos test, ensuring count is non-negative and consistent with advised state is a good start.
|
||||
Assert.IsTrue(receiver.GetNotificationCount >= 0,
|
||||
Format('Receiver ID %d has non-positive notification count: %d', [receiver.GetInstanceID, receiver.GetNotificationCount]));
|
||||
Assert.IsTrue(
|
||||
receiver.GetNotificationCount >= 0,
|
||||
Format(
|
||||
'Receiver ID %d has non-positive notification count: %d',
|
||||
[receiver.GetInstanceID, receiver.GetNotificationCount]
|
||||
)
|
||||
);
|
||||
if found and (receiver.GetNotificationCount = 0) then
|
||||
begin
|
||||
// This might be okay if Notify calls were very sparse or the item was just added and not yet notified.
|
||||
|
||||
@@ -127,7 +127,8 @@ begin
|
||||
// Acquire lock before calling Advise
|
||||
FOwnerFixture.FNotifier.Lock;
|
||||
try
|
||||
{var tag :=} FOwnerFixture.FNotifier.Advise(FReceiversToAdd[i]);
|
||||
{var tag :=}
|
||||
FOwnerFixture.FNotifier.Advise(FReceiversToAdd[i]);
|
||||
// The 'tag' could be used here if necessary for other test scenarios
|
||||
finally
|
||||
// Definitely release lock in the finally block
|
||||
@@ -168,7 +169,7 @@ begin
|
||||
try
|
||||
// UnadviseAll should be safe if the IsFinalized guard is present
|
||||
// and the FFirst state is not pathological.
|
||||
// if not FNotifier.IsFinalized then
|
||||
// if not FNotifier.IsFinalized then
|
||||
begin
|
||||
FNotifier.UnadviseAll;
|
||||
end;
|
||||
@@ -264,9 +265,15 @@ begin
|
||||
finally
|
||||
FNotifier.Release;
|
||||
end;
|
||||
Assert.AreEqual(totalAdvisedExpected, currentNotifyCount,
|
||||
'The number of items in the Notifier after all Advise operations (' + currentNotifyCount.ToString +
|
||||
') does not match the expected number (' + totalAdvisedExpected.ToString + ').');
|
||||
Assert.AreEqual(
|
||||
totalAdvisedExpected,
|
||||
currentNotifyCount,
|
||||
'The number of items in the Notifier after all Advise operations ('
|
||||
+ currentNotifyCount.ToString
|
||||
+ ') does not match the expected number ('
|
||||
+ totalAdvisedExpected.ToString
|
||||
+ ').'
|
||||
);
|
||||
|
||||
// 2. Execute UnadviseAll
|
||||
FNotifier.Lock;
|
||||
@@ -290,12 +297,15 @@ begin
|
||||
finally
|
||||
FNotifier.Release;
|
||||
end;
|
||||
Assert.AreEqual(0, currentNotifyCount,
|
||||
'The Notifier should be empty after UnadviseAll, but still contains ' + currentNotifyCount.ToString + ' items.');
|
||||
Assert.AreEqual(
|
||||
0,
|
||||
currentNotifyCount,
|
||||
'The Notifier should be empty after UnadviseAll, but still contains ' + currentNotifyCount.ToString + ' items.'
|
||||
);
|
||||
end;
|
||||
|
||||
initialization
|
||||
|
||||
TDUnitX.RegisterTestFixture(TMycNotifierThreadingTests);
|
||||
TDUnitX.RegisterTestFixture(TMycNotifierThreadingTests);
|
||||
|
||||
end.
|
||||
|
||||
@@ -128,7 +128,11 @@ begin
|
||||
Assert.AreEqual(ExpectedIsSet, latch.State.IsSet, 'Latch initial IsSet state mismatch for count ' + IntToStr(InitialCount) + '.');
|
||||
end;
|
||||
|
||||
procedure TTestMycLatch.TestNotify_ReturnValueAndState_AfterOneNotify(InitialCount: Integer; ExpectedReturn: Boolean; ExpectedIsSet: Boolean);
|
||||
procedure TTestMycLatch.TestNotify_ReturnValueAndState_AfterOneNotify(
|
||||
InitialCount: Integer;
|
||||
ExpectedReturn: Boolean;
|
||||
ExpectedIsSet: Boolean
|
||||
);
|
||||
var
|
||||
latch: IMycLatch;
|
||||
returnedValueFromNotify: Boolean;
|
||||
@@ -136,8 +140,16 @@ begin
|
||||
latch := TLatch.Construct(InitialCount);
|
||||
returnedValueFromNotify := latch.Notify; // This is IMycLatch (as IMycSubscriber).Notify
|
||||
|
||||
Assert.AreEqual(ExpectedReturn, returnedValueFromNotify, 'Unexpected return value from Latch.Notify call for initial count ' + IntToStr(InitialCount) + '.');
|
||||
Assert.AreEqual(ExpectedIsSet, latch.State.IsSet, 'Unexpected IsSet state after Latch.Notify call for initial count ' + IntToStr(InitialCount) + '.');
|
||||
Assert.AreEqual(
|
||||
ExpectedReturn,
|
||||
returnedValueFromNotify,
|
||||
'Unexpected return value from Latch.Notify call for initial count ' + IntToStr(InitialCount) + '.'
|
||||
);
|
||||
Assert.AreEqual(
|
||||
ExpectedIsSet,
|
||||
latch.State.IsSet,
|
||||
'Unexpected IsSet state after Latch.Notify call for initial count ' + IntToStr(InitialCount) + '.'
|
||||
);
|
||||
end;
|
||||
|
||||
procedure TTestMycLatch.TestNotify_DecrementsCounter_StateChanges;
|
||||
@@ -430,7 +442,7 @@ procedure TTestMycLatch.TestLatchSet_SubscriptionFinalize_IsSafePostUnadviseAll;
|
||||
var
|
||||
latch: IMycLatch;
|
||||
mockSub: TMockSubscriber;
|
||||
// 'subscription' will be declared in an inner scope to control its finalization
|
||||
// 'subscription' will be declared in an inner scope to control its finalization
|
||||
begin
|
||||
latch := TLatch.Construct(1);
|
||||
mockSub := TMockSubscriber.Create;
|
||||
@@ -492,7 +504,11 @@ begin
|
||||
Assert.AreEqual(1, mockSubInitial.NotifyCount, 'Initial subscriber (unadvised) not notified again.');
|
||||
// mockSubNew should also not be notified again, as it was only notified immediately
|
||||
// and not persistently added to FSubscribers for subsequent Latch.Notify calls.
|
||||
Assert.AreEqual(1, mockSubNew.NotifyCount, 'New subscriber (notified once on subscribe) not notified by subsequent Latch.Notify calls.');
|
||||
Assert.AreEqual(
|
||||
1,
|
||||
mockSubNew.NotifyCount,
|
||||
'New subscriber (notified once on subscribe) not notified by subsequent Latch.Notify calls.'
|
||||
);
|
||||
end;
|
||||
|
||||
initialization
|
||||
|
||||
+19
-21
@@ -8,7 +8,8 @@ uses
|
||||
System.Classes,
|
||||
System.SyncObjs,
|
||||
Myc.Core.Tasks,
|
||||
Myc.Signals, Myc.Core.Signals,
|
||||
Myc.Signals,
|
||||
Myc.Core.Signals,
|
||||
Myc.Core.Atomic;
|
||||
|
||||
type
|
||||
@@ -73,10 +74,7 @@ var
|
||||
procWrapper: TProc;
|
||||
begin
|
||||
executed := False;
|
||||
procWrapper := procedure
|
||||
begin
|
||||
executed := True;
|
||||
end;
|
||||
procWrapper := procedure begin executed := True; end;
|
||||
|
||||
threadState := FFactory.CreateThread(procWrapper); // CreateThread in TaskFactory now uses TMycLatch.CreateLatch
|
||||
Assert.IsNotNull(threadState, 'CreateThread should return a valid state object');
|
||||
@@ -93,12 +91,14 @@ begin
|
||||
jobExecuted := False;
|
||||
jobCompletedLatch := TLatch.Construct(1); // Changed from Signals.CreateLatch
|
||||
|
||||
FFactory.Run(
|
||||
FFactory
|
||||
.Run(
|
||||
procedure
|
||||
begin
|
||||
jobExecuted := True;
|
||||
jobCompletedLatch.Notify;
|
||||
end).Notify;
|
||||
end)
|
||||
.Notify;
|
||||
|
||||
FFactory.WaitFor(jobCompletedLatch.State);
|
||||
Assert.IsTrue(jobExecuted, 'Immediate job should have executed');
|
||||
@@ -113,12 +113,14 @@ begin
|
||||
jobExecuted := False;
|
||||
jobCompletedLatch := TLatch.Construct(1); // Changed from Signals.CreateLatch
|
||||
|
||||
subscriber := FFactory.Run(
|
||||
subscriber :=
|
||||
FFactory.Run(
|
||||
procedure
|
||||
begin
|
||||
jobExecuted := True;
|
||||
jobCompletedLatch.Notify;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
Assert.IsNotNull(subscriber, 'Run should return a subscriber for delayed job');
|
||||
// Use TMycLatch.Null for comparison
|
||||
@@ -130,7 +132,6 @@ begin
|
||||
Assert.IsTrue(jobExecuted, 'Delayed job should execute after all notifications');
|
||||
end;
|
||||
|
||||
|
||||
procedure TMycTaskFactoryTests.TestWaitForAlreadySetState;
|
||||
var
|
||||
alreadySetLatch: IMycLatch;
|
||||
@@ -166,7 +167,8 @@ begin
|
||||
inMainThreadInJob := FFactory.InMainThread;
|
||||
inWorkerThreadInJob := FFactory.InWorkerThread;
|
||||
jobDoneLatch.Notify;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
FFactory.WaitFor(jobDoneLatch.State);
|
||||
|
||||
@@ -188,13 +190,11 @@ begin
|
||||
finally
|
||||
jobDoneLatch.Notify;
|
||||
end;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
Assert.WillRaise(
|
||||
procedure
|
||||
begin
|
||||
FFactory.WaitFor(jobDoneLatch.State);
|
||||
end,
|
||||
procedure begin FFactory.WaitFor(jobDoneLatch.State); end,
|
||||
TMycTaskFactory.ETaskException,
|
||||
'WaitFor should re-raise the exception from the job'
|
||||
);
|
||||
@@ -213,10 +213,7 @@ begin
|
||||
FFactory.Teardown;
|
||||
|
||||
Assert.WillRaise(
|
||||
procedure
|
||||
begin
|
||||
FFactory.EnqueueJob(procedure begin end);
|
||||
end,
|
||||
procedure begin FFactory.EnqueueJob(procedure begin end); end,
|
||||
TMycTaskFactory.ETaskException,
|
||||
'Running a job on a torn-down factory should raise ETaskException'
|
||||
);
|
||||
@@ -244,7 +241,8 @@ begin
|
||||
end;
|
||||
end;
|
||||
jobDoneLatch.Notify;
|
||||
end);
|
||||
end
|
||||
);
|
||||
|
||||
FFactory.WaitFor(jobDoneLatch.State);
|
||||
Assert.IsTrue(exceptionCaughtInJob, 'WaitFor called within a worker thread job should raise ETaskException');
|
||||
|
||||
Reference in New Issue
Block a user