Test Refactoring of signals

This commit is contained in:
Michael Schimmel
2025-06-05 22:11:54 +02:00
parent c9817ca200
commit e06a023742
17 changed files with 528 additions and 774 deletions
+2 -2
View File
@@ -24,7 +24,7 @@ type
TMycGateFuncFuture<T> = class(TMycFuture<T>) TMycGateFuncFuture<T> = class(TMycFuture<T>)
private private
FInit: TState.TSubscription; FInit: TState.TSubscription;
FDone: IMycLatch; FDone: IMycEvent;
FResult: T; FResult: T;
protected protected
function GetResult: T; override; function GetResult: T; override;
@@ -54,7 +54,7 @@ constructor TMycGateFuncFuture<T>.Create(const ATaskManager: IMycTaskManager; co
begin begin
inherited Create; inherited Create;
FDone := TLatch.Construct(1); FDone := TEvent.CreateLatch(1);
// Subscribe the job execution to AGate. // Subscribe the job execution to AGate.
// The job will run when AGate notifies the subscriber returned by Run. // The job will run when AGate notifies the subscriber returned by Run.
+101 -26
View File
@@ -8,38 +8,61 @@ uses
Myc.Lazy; Myc.Lazy;
type type
TMycLazy<T> = class abstract(TInterfacedObject, IMycLazy<T>) TMycNullLazy<T> = class(TInterfacedObject, IMycLazy<T>)
protected protected
function GetChanged: IMycState; virtual; abstract; function GetChanged: TState;
public public
function Pop(out Res: T): Boolean; virtual; abstract; function Pop(out Res: T): Boolean;
end; end;
TMycNullLazy<T> = class(TMycLazy<T>) TMycLazyBase<T> = class(TInterfacedObject, IMycLazy<T>)
protected strict private
function GetChanged: IMycState; override; FChanged: TFlag;
public
function Pop(out Res: T): Boolean; override;
end;
TMycFuncLazy<T> = class(TMycLazy<T>)
private
FChanged: IMycDirty;
FChangeState: TState.TSubscription; FChangeState: TState.TSubscription;
function GetChanged: TState;
protected
function GetValue: T; virtual; abstract;
public
constructor Create(const AChanged: TState);
destructor Destroy; override;
function Pop(out Res: T): Boolean;
end;
TMycFuncLazy<T> = class(TMycLazyBase<T>)
private
FProc: TFunc<T>; FProc: TFunc<T>;
protected protected
function GetChanged: IMycState; override; function GetValue: T; override;
public public
constructor Create(const AChanged: TState; const AProc: TFunc<T>); constructor Create(const AChanged: TState; const AProc: TFunc<T>);
destructor Destroy; override; end;
function Pop(out Res: T): Boolean; override;
TMycChainedLazy<T> = class(TMycLazyBase<T>)
private
FValue: IMycLazy<T>;
protected
function GetValue: T; override;
public
constructor Create(const AValue: IMycLazy<T>);
end;
TMycMutable<T> = class(TInterfacedObject, IMycLazy<T>, IMycMutable<T>)
private
FNotifier: TEvent;
FValue: T;
protected
function GetChanged: TState;
public
constructor Create;
function Pop(out Res: T): Boolean;
procedure SendValue(const Value: T);
end; end;
implementation implementation
{ TMycNullLazy<T> } { TMycNullLazy<T> }
function TMycNullLazy<T>.GetChanged: IMycState; function TMycNullLazy<T>.GetChanged: TState;
begin begin
Result := TState.Null; Result := TState.Null;
end; end;
@@ -50,36 +73,88 @@ begin
Result := true; Result := true;
end; end;
{ TMycFuncLazy<T> } { TMycLazyBase<T> }
constructor TMycFuncLazy<T>.Create(const AChanged: TState; const AProc: TFunc<T>); constructor TMycLazyBase<T>.Create(const AChanged: TState);
begin begin
inherited Create; inherited Create;
FChanged := TDirty.Construct; FChanged := TFlag.CreateFlag;
FProc := AProc;
FChangeState := AChanged.Subscribe(FChanged); FChangeState := AChanged.Subscribe(FChanged);
end; end;
destructor TMycFuncLazy<T>.Destroy; destructor TMycLazyBase<T>.Destroy;
begin begin
FChangeState.Unsubscribe; FChangeState.Unsubscribe;
inherited; inherited;
end; end;
function TMycFuncLazy<T>.GetChanged: IMycState; function TMycLazyBase<T>.GetChanged: TState;
begin begin
Result := FChanged.State; Result := FChanged.State;
end; end;
function TMycFuncLazy<T>.Pop(out Res: T): Boolean; function TMycLazyBase<T>.Pop(out Res: T): Boolean;
begin begin
Result := FChanged.State.IsSet; Result := FChanged.State.IsSet;
if Result then if Result then
begin begin
if Assigned(FProc) then Res := GetValue;
Res := FProc;
FChanged.Reset; FChanged.Reset;
end; end;
end; end;
{ TMycFuncLazy<T> }
constructor TMycFuncLazy<T>.Create(const AChanged: TState; const AProc: TFunc<T>);
begin
inherited Create( AChanged );
FProc := AProc;
Assert( Assigned(FProc) );
end;
function TMycFuncLazy<T>.GetValue: T;
begin
Result := FProc();
end;
{ TMycChainedLazy<T> }
constructor TMycChainedLazy<T>.Create(const AValue: IMycLazy<T>);
begin
inherited Create( AValue.Changed );
FValue := AValue;
end;
function TMycChainedLazy<T>.GetValue: T;
begin
if not FValue.Pop( Result ) then
raise Exception.Create('Lazy chain already popped');
end;
{ TMycMutable<T> }
constructor TMycMutable<T>.Create;
begin
inherited Create;
FNotifier := TEvent.CreateNotifier;
FValue := Default(T);
end;
function TMycMutable<T>.GetChanged: TState;
begin
Result := FNotifier.State;
end;
function TMycMutable<T>.Pop(out Res: T): Boolean;
begin
Res := FValue;
exit(true);
end;
procedure TMycMutable<T>.SendValue(const Value: T);
begin
FValue := Value;
FNotifier.Notify;
end;
end. end.
+116 -48
View File
@@ -8,32 +8,38 @@ uses
Myc.Signals; Myc.Signals;
type type
TMycState = class abstract(TInterfacedObject, IMycState)
protected
function GetIsSet: Boolean; virtual; abstract;
public
function Subscribe(Subscriber: IMycSubscriber): Pointer; virtual; abstract;
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); virtual; abstract;
property IsSet: Boolean read GetIsSet;
end;
// TMycNullState implements a "null object" pattern for IMycState. // TMycNullState implements a "null object" pattern for IMycState.
// This state is always considered "set". // This state is always considered "set".
TMycNullState = class(TInterfacedObject, IMycState) TMycNullState = class(TInterfacedObject, IMycState)
protected protected
// IMycState // IMycState
function GetIsSet: Boolean; function GetIsSet: Boolean;
function Subscribe(Subscriber: IMycSubscriber): Pointer;
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
public public
constructor Create; constructor Create;
function Subscribe(Subscriber: IMycSubscriber): Pointer;
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
end;
// A state that acts as an event. It's state ist always set and it will always notify it's subscribers, if it gets notified by itself.
TMycNotifyEvent = class(TInterfacedObject, IMycState, IMycEvent)
strict private
FSubscribers: TMycNotifyList<IMycSubscriber>;
protected
function GetIsSet: Boolean;
function GetState: TState;
public
constructor Create;
destructor Destroy; override;
function Subscribe(Subscriber: IMycSubscriber): Pointer;
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
function Notify: Boolean;
end; end;
// TMycLatch implements a countdown latch. // TMycLatch implements a countdown latch.
// It is initialized with a count. Calls to its Notify method (as an IMycSubscriber) // It is initialized with a count. Calls to its Notify method (as an IMycSubscriber)
// decrement this count. When the count reaches zero, the latch transitions to the "set" // decrement this count. When the count reaches zero, the latch transitions to the "set"
// state (IsSet becomes true), notifies its current subscribers once, and then remains set. // state (IsSet becomes true), notifies its current subscribers once, and then remains set.
TMycLatch = class(TMycState, IMycLatch) TMycLatch = class(TInterfacedObject, IMycState, IMycEvent)
strict private strict private
FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers waiting for this latch to be set. FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers waiting for this latch to be set.
private private
@@ -41,7 +47,7 @@ type
FCount: Integer; // The internal countdown value for the latch. FCount: Integer; // The internal countdown value for the latch.
function GetState: TState; // Implementation for IMycLatch.GetState and IMycFlag.GetState. function GetState: TState; // Implementation for IMycLatch.GetState and IMycFlag.GetState.
protected protected
function GetIsSet: Boolean; override; final; // Implementation for IMycState.GetIsSet. function GetIsSet: Boolean;
public public
// Creates the latch with an initial count. // Creates the latch with an initial count.
// If ACount is 0 or less, the latch is effectively pre-set (delegates to FNull via CreateLatch factory). // If ACount is 0 or less, the latch is effectively pre-set (delegates to FNull via CreateLatch factory).
@@ -49,8 +55,8 @@ type
destructor Destroy; override; destructor Destroy; override;
// IMycSignal implementation // IMycSignal implementation
function Subscribe(Subscriber: IMycSubscriber): Pointer; override; function Subscribe(Subscriber: IMycSubscriber): Pointer;
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); override; procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
// IMycSubscriber implementation for TMycLatch itself. // IMycSubscriber implementation for TMycLatch itself.
// Decrements the internal count. If the count reaches zero, // Decrements the internal count. If the count reaches zero,
@@ -58,16 +64,16 @@ type
function Notify: Boolean; function Notify: Boolean;
end; end;
TMycNullLatch = class(TInterfacedObject, IMycLatch) TMycNullEvent = class(TInterfacedObject, IMycEvent)
private private
function GetState: TState; function GetState: TState;
function Notify: Boolean; function Notify: Boolean;
end; end;
// TMycDirty implements a resettable "dirty flag". // TMycFlag implements a resettable "dirty flag".
// It can be explicitly set to dirty (via Notify) or reset to clean (via Reset). // It can be explicitly set to dirty (via Notify) or reset to clean (via Reset).
// Subscribers are notified of relevant state changes. // Subscribers are notified of relevant state changes.
TMycDirty = class(TMycState, IMycDirty) TMycFlag = class(TInterfacedObject, IMycState, IMycFlag)
strict private strict private
FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers interested in state changes of this dirty flag. FSubscribers: TMycNotifyList<IMycSubscriber>; // List of subscribers interested in state changes of this dirty flag.
private private
@@ -75,27 +81,27 @@ type
FFlag: Boolean; // Internal state: true if dirty/set, false if clean/reset. FFlag: Boolean; // Internal state: true if dirty/set, false if clean/reset.
function GetState: TState; function GetState: TState;
protected protected
function GetIsSet: Boolean; override; final; // Implementation for IMycState.GetIsSet (true if dirty). function GetIsSet: Boolean;
public public
// Creates a new dirty flag, initially set to dirty (true). // Creates a new dirty flag, initially set to dirty (true).
constructor Create; constructor Create;
destructor Destroy; override; destructor Destroy; override;
// Factory method to create a new TMycDirty instance. // Factory method to create a new TMycFlag instance.
class function CreateDirty: IMycDirty; static; class function CreateDirty: IMycFlag; static;
function Subscribe(Subscriber: IMycSubscriber): Pointer; override; function Subscribe(Subscriber: IMycSubscriber): Pointer;
procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); override; procedure Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
// IMycSubscriber implementation for TMycDirty. // IMycSubscriber implementation for TMycFlag.
// Sets this flag to dirty (true). Notifies subscribers if it transitioned from clean to dirty. // Sets this flag to dirty (true). Notifies subscribers if it transitioned from clean to dirty.
function Notify: Boolean; function Notify: Boolean;
// IMycDirty implementation. Resets the flag to clean (false). // IMycFlag implementation. Resets the flag to clean (false).
function Reset: Boolean; function Reset: Boolean;
end; end;
TMycNullDirty = class(TInterfacedObject, IMycDirty) TMycNullFlag = class(TInterfacedObject, IMycFlag)
private private
function GetState: TState; function GetState: TState;
function Notify: Boolean; function Notify: Boolean;
@@ -135,14 +141,76 @@ begin
// Unsubscribing from a null state has no effect. // Unsubscribing from a null state has no effect.
end; end;
{ TMycNullLatch } { TMycNotifyEvent }
function TMycNullLatch.GetState: TState; constructor TMycNotifyEvent.Create;
begin
inherited Create;
FSubscribers.Create;
end;
destructor TMycNotifyEvent.Destroy;
begin
FSubscribers.Destroy;
inherited;
end;
function TMycNotifyEvent.GetIsSet: Boolean;
begin
Result := true;
end;
function TMycNotifyEvent.GetState: TState;
begin
Result := Self;
end;
function TMycNotifyEvent.Notify: Boolean;
begin
FSubscribers.Lock;
try
FSubscribers.Notify(function(Subscriber: IMycSubscriber): Boolean begin Result := Subscriber.Notify; end);
finally
FSubscribers.Release;
end;
end;
function TMycNotifyEvent.Subscribe(Subscriber: IMycSubscriber): Pointer;
begin
Result := nil;
if not Assigned(Subscriber) then
exit;
FSubscribers.Lock;
try
Subscriber.Notify;
Result := FSubscribers.Advise(Subscriber);
finally
FSubscribers.Release;
end;
end;
procedure TMycNotifyEvent.Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
begin
if Tag = nil then
exit;
FSubscribers.Lock;
try
FSubscribers.Unadvise(Tag);
finally
FSubscribers.Release;
end;
end;
{ TMycNullEvent }
function TMycNullEvent.GetState: TState;
begin begin
Result := TState.Null; Result := TState.Null;
end; end;
function TMycNullLatch.Notify: Boolean; function TMycNullEvent.Notify: Boolean;
begin begin
Result := false; Result := false;
end; end;
@@ -246,63 +314,63 @@ begin
end; end;
end; end;
{ TMycNullDirty } { TMycNullFlag }
function TMycNullDirty.GetState: TState; function TMycNullFlag.GetState: TState;
begin begin
Result := TState.Null; Result := TState.Null;
end; end;
function TMycNullDirty.Notify: Boolean; function TMycNullFlag.Notify: Boolean;
begin begin
Result := false; Result := false;
end; end;
function TMycNullDirty.Reset: Boolean; function TMycNullFlag.Reset: Boolean;
begin begin
Result := true; Result := true;
end; end;
{ TMycDirty } { TMycFlag }
constructor TMycDirty.Create; constructor TMycFlag.Create;
begin begin
inherited Create; inherited Create;
FFlag := true; // A new dirty flag is initially considered dirty. FFlag := true; // A new dirty flag is initially considered dirty.
FSubscribers.Create; FSubscribers.Create;
end; end;
class function TMycDirty.CreateDirty: IMycDirty; class function TMycFlag.CreateDirty: IMycFlag;
begin begin
// Factory method to create a new TMycDirty instance. // Factory method to create a new TMycFlag instance.
Result := TMycDirty.Create; Result := TMycFlag.Create;
end; end;
destructor TMycDirty.Destroy; destructor TMycFlag.Destroy;
begin begin
FSubscribers.Destroy; // Clean up the subscriber list. FSubscribers.Destroy; // Clean up the subscriber list.
inherited; inherited;
end; end;
function TMycDirty.Reset: Boolean; function TMycFlag.Reset: Boolean;
begin begin
// Sets the flag to false (clean) and returns true if it was previously true (dirty). // Sets the flag to false (clean) and returns true if it was previously true (dirty).
Result := TInterlocked.Exchange(FFlag, false); Result := TInterlocked.Exchange(FFlag, false);
end; end;
function TMycDirty.GetIsSet: Boolean; function TMycFlag.GetIsSet: Boolean;
begin begin
// IsSet is true if the flag is dirty (FFlag = true). // IsSet is true if the flag is dirty (FFlag = true).
Result := FFlag; Result := FFlag;
end; end;
function TMycDirty.GetState: TState; function TMycFlag.GetState: TState;
begin begin
// TMycDirty itself implements IMycState. // TMycFlag itself implements IMycState.
Result := Self; Result := Self;
end; end;
function TMycDirty.Notify: Boolean; function TMycFlag.Notify: Boolean;
var var
wasPreviouslyClean: Boolean; wasPreviouslyClean: Boolean;
begin begin
@@ -316,7 +384,7 @@ begin
FSubscribers.Notify(function(Subscriber: IMycSubscriber): Boolean begin Result := Subscriber.Notify; end); FSubscribers.Notify(function(Subscriber: IMycSubscriber): Boolean begin Result := Subscriber.Notify; end);
end; end;
// The return value of this Notify (as an IMycSubscriber) indicates if this // 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. // TMycFlag instance itself would want more notifications if it were subscribed to something.
// Here, it reflects whether a state change to dirty occurred due to this call. // Here, it reflects whether a state change to dirty occurred due to this call.
Result := wasPreviouslyClean; Result := wasPreviouslyClean;
finally finally
@@ -324,12 +392,12 @@ begin
end; end;
end; end;
function TMycDirty.Subscribe(Subscriber: IMycSubscriber): Pointer; function TMycFlag.Subscribe(Subscriber: IMycSubscriber): Pointer;
begin begin
if not Assigned(Subscriber) then if not Assigned(Subscriber) then
exit(nil); exit(nil);
// For TMycDirty, we always add the subscriber and then check if we need to notify immediately. // For TMycFlag, we always add the subscriber and then check if we need to notify immediately.
// Ref counting for the subscriber interface is assumed to be handled by TMycNotifyList.Advise/Unadvise. // Ref counting for the subscriber interface is assumed to be handled by TMycNotifyList.Advise/Unadvise.
// Or, if explicit management is needed like in TMycLatch, it should be added. // Or, if explicit management is needed like in TMycLatch, it should be added.
// For consistency with TMycLatch, let's add AddRef/Release here too. // For consistency with TMycLatch, let's add AddRef/Release here too.
@@ -346,7 +414,7 @@ begin
end; end;
end; end;
procedure TMycDirty.Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag); procedure TMycFlag.Unsubscribe(Tag: TMycNotifyList<IMycSubscriber>.TTag);
begin begin
if Tag = nil then if Tag = nil then
exit; exit;
+3 -3
View File
@@ -203,11 +203,11 @@ end;
function TMycTaskFactory.CreateThread(const Proc: TProc): IMycState; function TMycTaskFactory.CreateThread(const Proc: TProc): IMycState;
var var
res: IMycLatch; res: IMycEvent;
capturedProc: TProc; capturedProc: TProc;
begin begin
// Create a latch that will be signaled when the proc finishes // Create a latch that will be signaled when the proc finishes
res := TLatch.Construct(1); // Changed to use direct static call on TMycLatch res := TEvent.CreateLatch(1);
capturedProc := Proc; // Capture Proc for the anonymous method capturedProc := Proc; // Capture Proc for the anonymous method
CreateAnonymousThread( CreateAnonymousThread(
@@ -309,7 +309,7 @@ begin
raise ETaskException.Create('Task factory terminated'); raise ETaskException.Create('Task factory terminated');
if not Assigned(Job) then if not Assigned(Job) then
exit(TLatch.Null); // Already correct, uses direct static property on TMycLatch exit(TEvent.Null); // Already correct, uses direct static property on TMycLatch
// Create a pending job // Create a pending job
Result := TMycPendingJob.Create(Self, Job); Result := TMycPendingJob.Create(Self, Job);
+16 -3
View File
@@ -7,12 +7,19 @@ uses
Myc.Signals; Myc.Signals;
type type
IMycMutable<T> = interface
function GetChanged: TState;
function GetValue: T;
property Changed: TState read GetChanged;
property Value: T read GetValue;
end;
IMycLazy<T> = interface IMycLazy<T> = interface
{$REGION 'property access'} {$REGION 'property access'}
function GetChanged: IMycState; function GetChanged: TState;
{$ENDREGION} {$ENDREGION}
function Pop(out Res: T): Boolean; function Pop(out Res: T): Boolean;
property Changed: IMycState read GetChanged; property Changed: TState read GetChanged;
end; end;
TLazy<T> = record TLazy<T> = record
@@ -26,7 +33,8 @@ type
public 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>; overload; static;
class function Construct(const Value: IMycLazy<T>): IMycLazy<T>; overload; static;
class operator Implicit(const A: IMycLazy<T>): TLazy<T>; overload; 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: TLazy<T>): IMycLazy<T>; overload;
@@ -58,6 +66,11 @@ begin
FNull := TMycNullLazy<T>.Create; FNull := TMycNullLazy<T>.Create;
end; end;
class function TLazy<T>.Construct(const Value: IMycLazy<T>): IMycLazy<T>;
begin
Result := TMycChainedLazy<T>.Create(Value);
end;
function TLazy<T>.GetChanged: IMycState; function TLazy<T>.GetChanged: IMycState;
begin begin
Result := FLazy.Changed; Result := FLazy.Changed;
+110 -51
View File
@@ -61,44 +61,44 @@ type
property IsSet: Boolean read GetIsSet; property IsSet: Boolean read GetIsSet;
end; end;
// IMycLatch is a specific type of flag that, once set, remains set (non-resettable). IMycEvent = interface(IMycSubscriber)
// It typically becomes set when an internal countdown reaches zero.
IMycLatch = interface(IMycSubscriber)
// Provides access to the IMycState interface of the flag.
function GetState: TState; function GetState: TState;
property State: TState read GetState; property State: TState read GetState;
end; end;
TLatch = record TEvent = record
strict private strict private
class var class var
FNull: IMycLatch; FNull: IMycEvent;
class constructor ClassCreate; class constructor ClassCreate;
private private
FLatch: IMycLatch; FLatch: IMycEvent;
function GetState: TState; inline; function GetState: TState; inline;
public public
constructor Create(const ALatch: IMycLatch); constructor Create(const ALatch: IMycEvent);
class operator Initialize(out Dest: TLatch); class operator Initialize(out Dest: TEvent);
class operator Implicit(const A: IMycLatch): TLatch; overload; class operator Implicit(const A: IMycEvent): TEvent; overload;
class operator Implicit(const A: TLatch): IMycLatch; overload; class operator Implicit(const A: TEvent): IMycEvent; overload;
class function Construct(Count: Integer): TLatch; static; // A notifier is an event that acts as a multi-cast event. Its state is always set and it forwards all notifications the all subscribers.
class function CreateNotifier: TEvent; static;
class function Enqueue(var Gate: TLatch; Count: Integer = 1): TState; static; // A latch is a specific type of event that, once set, remains set (non-resettable).
// It becomes set when the internal countdown reaches zero.
class function CreateLatch(Count: Integer): TEvent; static;
class property Null: IMycLatch read FNull; class function Enqueue(var Gate: TEvent; Count: Integer = 1): TState; static;
class property Null: IMycEvent read FNull;
function Notify: Boolean; inline; function Notify: Boolean; inline;
property State: TState read GetState; property State: TState read GetState;
end; end;
// IMycDirty represents a resettable flag, typically indicating if a State is "dirty" (requiring attention) or "clean". // IMycFlag 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. IMycFlag = interface(IMycSubscriber)
IMycDirty = interface(IMycSubscriber)
// Provides access to the IMycState interface of the flag.
function GetState: TState; function GetState: TState;
// Resets the flag to its "clean" (not set / not dirty) State. // Resets the flag to its "clean" (not set / not dirty) State.
// Returns true if the flag was actually dirty before this reset, false otherwise. // Returns true if the flag was actually dirty before this reset, false otherwise.
@@ -106,14 +106,29 @@ type
property State: TState read GetState; property State: TState read GetState;
end; end;
TDirty = record TFlag = record
strict private strict private
class var class var
FNull: IMycDirty; FNull: IMycFlag;
class constructor ClassCreate; class constructor ClassCreate;
private
FDirty: IMycFlag;
function GetState: TState; inline;
public public
class function Construct: IMycDirty; static; constructor Create(const ADirty: IMycFlag);
class property Null: IMycDirty read FNull; class operator Implicit(const A: IMycFlag): TFlag; overload;
class operator Implicit(const A: TFlag): IMycFlag; overload;
class operator Initialize(out Dest: TFlag);
class function CreateFlag: IMycFlag; static;
class property Null: IMycFlag read FNull;
function Notify: Boolean; inline;
function Reset: Boolean;
property State: TState read GetState;
end; end;
implementation implementation
@@ -161,9 +176,9 @@ end;
class function TState.All(const States: TArray<TState>): TState; class function TState.All(const States: TArray<TState>): TState;
var var
Latch: IMycLatch; Latch: IMycEvent;
begin begin
Latch := TLatch.Construct(Length(States)); Latch := TEvent.CreateLatch(Length(States));
for var i := 0 to High(States) do for var i := 0 to High(States) do
States[i].Subscribe(Latch); States[i].Subscribe(Latch);
Result := Latch.State; Result := Latch.State;
@@ -171,7 +186,7 @@ end;
class function TState.Any(const States: TArray<TState>): TState; class function TState.Any(const States: TArray<TState>): TState;
var var
Latch: IMycLatch; Latch: IMycEvent;
begin begin
if Length(States) = 0 then if Length(States) = 0 then
begin begin
@@ -179,7 +194,7 @@ begin
end end
else else
begin begin
Latch := TLatch.Construct(1); Latch := TEvent.CreateLatch(1);
for var i := 0 to High(States) do for var i := 0 to High(States) do
States[i].Subscribe(Latch); States[i].Subscribe(Latch);
Result := Latch.State; Result := Latch.State;
@@ -211,24 +226,75 @@ begin
Result.Create(A); Result.Create(A);
end; end;
{ TLatch } { TFlag }
class constructor TLatch.ClassCreate; class constructor TFlag.ClassCreate;
begin begin
// Create a singleton null latch instance that is initially (and always) set. FNull := TMycNullFlag.Create;
FNull := TMycNullLatch.Create; // Calls the instance constructor
end; end;
{ TLatch } { TFlag }
constructor TLatch.Create(const ALatch: IMycLatch); constructor TFlag.Create(const ADirty: IMycFlag);
begin
FDirty := ADirty;
if not Assigned(FDirty) then
FDirty := FNull;
end;
class function TFlag.CreateFlag: IMycFlag;
begin
Result := TMycFlag.Create;
end;
function TFlag.GetState: TState;
begin
Result := FDirty.State;
end;
function TFlag.Notify: Boolean;
begin
Result := FDirty.Notify;
end;
function TFlag.Reset: Boolean;
begin
Result := FDirty.Reset;
end;
class operator TFlag.Implicit(const A: IMycFlag): TFlag;
begin
Result.Create(A);
end;
class operator TFlag.Implicit(const A: TFlag): IMycFlag;
begin
Result := A.FDirty;
end;
class operator TFlag.Initialize(out Dest: TFlag);
begin
Dest.FDirty := FNull;
end;
{ TEvent }
class constructor TEvent.ClassCreate;
begin
// Create a singleton null latch instance that is initially (and always) set.
FNull := TMycNullEvent.Create;
end;
{ TEvent }
constructor TEvent.Create(const ALatch: IMycEvent);
begin begin
FLatch := ALatch; FLatch := ALatch;
if not Assigned(FLatch) then if not Assigned(FLatch) then
FLatch := FNull; FLatch := FNull;
end; end;
class function TLatch.Construct(Count: Integer): TLatch; class function TEvent.CreateLatch(Count: Integer): TEvent;
begin begin
if Count > 0 then if Count > 0 then
Result := TMycLatch.Create(Count) Result := TMycLatch.Create(Count)
@@ -236,7 +302,12 @@ begin
Result := FNull; Result := FNull;
end; end;
class function TLatch.Enqueue(var Gate: TLatch; Count: Integer = 1): TState; class function TEvent.CreateNotifier: TEvent;
begin
Result := TMycNotifyEvent.Create;
end;
class function TEvent.Enqueue(var Gate: TEvent; Count: Integer = 1): TState;
begin begin
var gateState := Gate.State; var gateState := Gate.State;
Gate := TMycLatch.Create(Count); Gate := TMycLatch.Create(Count);
@@ -244,41 +315,29 @@ begin
exit(Gate.State); exit(Gate.State);
end; end;
function TLatch.GetState: TState; function TEvent.GetState: TState;
begin begin
Result := FLatch.State; Result := FLatch.State;
end; end;
function TLatch.Notify: Boolean; function TEvent.Notify: Boolean;
begin begin
Result := FLatch.Notify; Result := FLatch.Notify;
end; end;
class operator TLatch.Implicit(const A: IMycLatch): TLatch; class operator TEvent.Implicit(const A: IMycEvent): TEvent;
begin begin
Result.Create(A); Result.Create(A);
end; end;
class operator TLatch.Implicit(const A: TLatch): IMycLatch; class operator TEvent.Implicit(const A: TEvent): IMycEvent;
begin begin
Result := A.FLatch; Result := A.FLatch;
end; end;
class operator TLatch.Initialize(out Dest: TLatch); class operator TEvent.Initialize(out Dest: TEvent);
begin begin
Dest.FLatch := FNull; Dest.FLatch := FNull;
end; end;
{ TDirty }
class constructor TDirty.ClassCreate;
begin
FNull := TMycNullDirty.Create;
end;
class function TDirty.Construct: IMycDirty;
begin
Result := TMycDirty.Create;
end;
end. end.
+22 -22
View File
@@ -162,9 +162,9 @@ procedure TTestMycCoreLazy.TestFuncLazy_GetChanged_IsAlwaysTrueAfterCreation;
var var
funcLazy: IMycLazy<Integer>; funcLazy: IMycLazy<Integer>;
changedState: IMycState; changedState: IMycState;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; sourceDirty.Reset;
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State, function: Integer begin Result := 10; end); funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State, function: Integer begin Result := 10; end);
Assert.IsNotNull(funcLazy, 'TMycFuncLazy<Integer> instance should not be nil'); Assert.IsNotNull(funcLazy, 'TMycFuncLazy<Integer> instance should not be nil');
@@ -178,11 +178,11 @@ var
funcLazy: IMycLazy<Integer>; funcLazy: IMycLazy<Integer>;
value: Integer; value: Integer;
result: Boolean; result: Boolean;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
procExecuted: Boolean; procExecuted: Boolean;
expectedValue: Integer; expectedValue: Integer;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; sourceDirty.Reset;
procExecuted := False; procExecuted := False;
expectedValue := 50; expectedValue := 50;
@@ -211,9 +211,9 @@ var
value: Integer; value: Integer;
preCallValue: Integer; preCallValue: Integer;
result: Boolean; result: Boolean;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; sourceDirty.Reset;
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State, nil); funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State, nil);
Assert.IsNotNull(funcLazy, 'TMycFuncLazy<Integer> instance should not be nil'); Assert.IsNotNull(funcLazy, 'TMycFuncLazy<Integer> instance should not be nil');
@@ -235,10 +235,10 @@ end;
procedure TTestMycCoreLazy.TestFuncLazy_AfterFirstPop_IfNoSourceChange_GetChangedIsFalse; procedure TTestMycCoreLazy.TestFuncLazy_AfterFirstPop_IfNoSourceChange_GetChangedIsFalse;
var var
funcLazy: IMycLazy<Integer>; funcLazy: IMycLazy<Integer>;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
initialProcValue: Integer; initialProcValue: Integer;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; sourceDirty.Reset;
initialProcValue := 33; initialProcValue := 33;
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State, function: Integer begin Result := initialProcValue; end); funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State, function: Integer begin Result := initialProcValue; end);
@@ -251,11 +251,11 @@ var
funcLazy: IMycLazy<Integer>; funcLazy: IMycLazy<Integer>;
value: Integer; value: Integer;
result: Boolean; result: Boolean;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
initialProcValue: Integer; initialProcValue: Integer;
procExecutedCount: Integer; procExecutedCount: Integer;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; sourceDirty.Reset;
initialProcValue := 44; initialProcValue := 44;
procExecutedCount := 0; procExecutedCount := 0;
@@ -279,10 +279,10 @@ procedure TTestMycCoreLazy.TestFuncLazy_AfterSourceChange_GetChanged_IsSet;
var var
funcLazy: IMycLazy<Integer>; funcLazy: IMycLazy<Integer>;
changedState: IMycState; changedState: IMycState;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
initialProcValue: Integer; initialProcValue: Integer;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; sourceDirty.Reset;
initialProcValue := 20; initialProcValue := 20;
funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State, function: Integer begin Result := initialProcValue; end); funcLazy := TMycFuncLazy<Integer>.Create(sourceDirty.State, function: Integer begin Result := initialProcValue; end);
@@ -297,11 +297,11 @@ var
funcLazy: IMycLazy<Integer>; funcLazy: IMycLazy<Integer>;
value: Integer; value: Integer;
result: Boolean; result: Boolean;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
procCallCount: Integer; procCallCount: Integer;
currentExpectedValue: Integer; currentExpectedValue: Integer;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; sourceDirty.Reset;
procCallCount := 0; procCallCount := 0;
funcLazy := funcLazy :=
@@ -335,10 +335,10 @@ var
funcLazy: IMycLazy<Integer>; funcLazy: IMycLazy<Integer>;
value: Integer; value: Integer;
resultPop1, resultPop2: Boolean; resultPop1, resultPop2: Boolean;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
procCallCount: Integer; procCallCount: Integer;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; sourceDirty.Reset;
procCallCount := 0; procCallCount := 0;
funcLazy := funcLazy :=
@@ -367,11 +367,11 @@ var
funcLazy: IMycLazy<Integer>; funcLazy: IMycLazy<Integer>;
value: Integer; value: Integer;
result: Boolean; result: Boolean;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
procCallCount: Integer; procCallCount: Integer;
initialValue, firstSourceChangeValue: Integer; initialValue, firstSourceChangeValue: Integer;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; // sourceDirty.IsSet is FALSE sourceDirty.Reset; // sourceDirty.IsSet is FALSE
procCallCount := 0; procCallCount := 0;
initialValue := 51; initialValue := 51;
@@ -427,10 +427,10 @@ end;
procedure TTestMycCoreLazy.TestFuncLazy_Destroy_UnsubscribesFromSource; procedure TTestMycCoreLazy.TestFuncLazy_Destroy_UnsubscribesFromSource;
var var
funcLazyObj: TMycFuncLazy<Integer>; funcLazyObj: TMycFuncLazy<Integer>;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
tempVal: Integer; tempVal: Integer;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
sourceDirty.Reset; sourceDirty.Reset;
funcLazyObj := TMycFuncLazy<Integer>.Create(sourceDirty.State, function: Integer begin Result := 1; end); funcLazyObj := TMycFuncLazy<Integer>.Create(sourceDirty.State, function: Integer begin Result := 1; end);
Assert.IsTrue(funcLazyObj.Pop(tempVal), 'Initial Pop should succeed'); Assert.IsTrue(funcLazyObj.Pop(tempVal), 'Initial Pop should succeed');
@@ -448,11 +448,11 @@ var
funcLazy: IMycLazy<Integer>; funcLazy: IMycLazy<Integer>;
value: Integer; value: Integer;
result: Boolean; result: Boolean;
sourceDirty: IMycDirty; sourceDirty: IMycFlag;
expectedValue: Integer; expectedValue: Integer;
procExecuted: Boolean; procExecuted: Boolean;
begin begin
sourceDirty := TDirty.Construct; sourceDirty := TFlag.CreateFlag;
Assert.IsTrue(sourceDirty.State.IsSet, 'SourceDirty should be initially set for this test scenario'); Assert.IsTrue(sourceDirty.State.IsSet, 'SourceDirty should be initially set for this test scenario');
expectedValue := 70; expectedValue := 70;
procExecuted := False; procExecuted := False;
+4 -4
View File
@@ -12,7 +12,7 @@ type
[TestFixture] [TestFixture]
TTestMyLazy = class(TObject) TTestMyLazy = class(TObject)
private private
FChangingSignal: IMycDirty; // Used as the 'Changing' state for functional lazy objects FChangingSignal: IMycFlag; // Used as the 'Changing' state for functional lazy objects
// Helper to consume the initial pop, which is always expected to succeed // Helper to consume the initial pop, which is always expected to succeed
// for a TLazy wrapping a functional lazy object due to "Changed.IsSet initially true" design. // for a TLazy wrapping a functional lazy object due to "Changed.IsSet initially true" design.
@@ -76,7 +76,7 @@ begin
// Create a common signal source for tests that need it. // Create a common signal source for tests that need it.
// TState.CreateDirty is from Myc.Signals.pas (interface part) // TState.CreateDirty is from Myc.Signals.pas (interface part)
// Its implementation might rely on Myc.Core.Signals, but that's an indirect usage. // Its implementation might rely on Myc.Core.Signals, but that's an indirect usage.
FChangingSignal := TDirty.Construct; FChangingSignal := TFlag.CreateFlag;
FChangingSignal.Reset; // Start with a clean (not set) signal for predictable test starts FChangingSignal.Reset; // Start with a clean (not set) signal for predictable test starts
end; end;
@@ -306,9 +306,9 @@ end;
procedure TTestMyLazy.TestConstruct_Destruction_UnsubscribesAndNoCrashOnSourceNotify; procedure TTestMyLazy.TestConstruct_Destruction_UnsubscribesAndNoCrashOnSourceNotify;
var var
lazyIntf: IMycLazy<Integer>; lazyIntf: IMycLazy<Integer>;
localChangingSignal: IMycDirty; // Use a local signal for this test to control its lifetime localChangingSignal: IMycFlag; // Use a local signal for this test to control its lifetime
begin begin
localChangingSignal := TDirty.Construct; localChangingSignal := TFlag.CreateFlag;
localChangingSignal.Reset; localChangingSignal.Reset;
lazyIntf := TLazy<Integer>.Construct(localChangingSignal.State, function: Integer begin Result := 1; end); lazyIntf := TLazy<Integer>.Construct(localChangingSignal.State, function: Integer begin Result := 1; end);
@@ -1,5 +1,4 @@
// Suggested filename: TestSignals_Latch.pas unit Myc.Test.Signals.Latch;
unit TestSignals_Latch;
interface interface
@@ -121,10 +120,10 @@ end;
procedure TTestMycLatch.TestCreate_InitialState(InitialCount: Integer; ExpectedIsSet: Boolean); procedure TTestMycLatch.TestCreate_InitialState(InitialCount: Integer; ExpectedIsSet: Boolean);
var var
latch: IMycLatch; latch: IMycEvent;
begin begin
// TMycLatch.CreateLatch returns TMycLatch.Null if InitialCount <= 0 // TMycLatch.CreateLatch returns TMycLatch.Null if InitialCount <= 0
latch := TLatch.Construct(InitialCount); latch := TEvent.CreateLatch(InitialCount);
Assert.AreEqual(ExpectedIsSet, latch.State.IsSet, 'Latch initial IsSet state mismatch for count ' + IntToStr(InitialCount) + '.'); Assert.AreEqual(ExpectedIsSet, latch.State.IsSet, 'Latch initial IsSet state mismatch for count ' + IntToStr(InitialCount) + '.');
end; end;
@@ -134,11 +133,11 @@ procedure TTestMycLatch.TestNotify_ReturnValueAndState_AfterOneNotify(
ExpectedIsSet: Boolean ExpectedIsSet: Boolean
); );
var var
latch: IMycLatch; latch: IMycEvent;
returnedValueFromNotify: Boolean; returnedValueFromNotify: Boolean;
begin begin
latch := TLatch.Construct(InitialCount); latch := TEvent.CreateLatch(InitialCount);
returnedValueFromNotify := latch.Notify; // This is IMycLatch (as IMycSubscriber).Notify returnedValueFromNotify := latch.Notify; // This is IMycEvent (as IMycSubscriber).Notify
Assert.AreEqual( Assert.AreEqual(
ExpectedReturn, ExpectedReturn,
@@ -154,9 +153,9 @@ end;
procedure TTestMycLatch.TestNotify_DecrementsCounter_StateChanges; procedure TTestMycLatch.TestNotify_DecrementsCounter_StateChanges;
var var
latch: IMycLatch; latch: IMycEvent;
begin begin
latch := TLatch.Construct(1); latch := TEvent.CreateLatch(1);
Assert.IsFalse(latch.State.IsSet, 'Latch should initially not be set (Count=1).'); Assert.IsFalse(latch.State.IsSet, 'Latch should initially not be set (Count=1).');
latch.Notify; // Count becomes 0 latch.Notify; // Count becomes 0
Assert.IsTrue(latch.State.IsSet, 'Latch should be set after Notify (Count became 0).'); Assert.IsTrue(latch.State.IsSet, 'Latch should be set after Notify (Count became 0).');
@@ -164,9 +163,9 @@ end;
procedure TTestMycLatch.TestNotify_MultipleNotifies_CounterBecomesNegativeAndStaysSet; procedure TTestMycLatch.TestNotify_MultipleNotifies_CounterBecomesNegativeAndStaysSet;
var var
latch: IMycLatch; latch: IMycEvent;
begin begin
latch := TLatch.Construct(1); latch := TEvent.CreateLatch(1);
latch.Notify; // Count becomes 0, IsSet = True latch.Notify; // Count becomes 0, IsSet = True
Assert.IsTrue(latch.State.IsSet, 'Latch should be set after first Notify.'); Assert.IsTrue(latch.State.IsSet, 'Latch should be set after first Notify.');
latch.Notify; // Count becomes -1, IsSet should remain True latch.Notify; // Count becomes -1, IsSet should remain True
@@ -177,11 +176,11 @@ end;
procedure TTestMycLatch.TestSubscriber_Single_IsNotifiedWhenLatchSet; procedure TTestMycLatch.TestSubscriber_Single_IsNotifiedWhenLatchSet;
var var
latch: IMycLatch; latch: IMycEvent;
mockSub: TMockSubscriber; mockSub: TMockSubscriber;
subscription: TState.TSubscription; subscription: TState.TSubscription;
begin begin
latch := TLatch.Construct(1); latch := TEvent.CreateLatch(1);
mockSub := TMockSubscriber.Create; mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub); // Subscribing to the Latch's state subscription := latch.State.Subscribe(mockSub); // Subscribing to the Latch's state
@@ -196,11 +195,11 @@ end;
procedure TTestMycLatch.TestSubscriber_Multiple_AreNotifiedWhenLatchSet; procedure TTestMycLatch.TestSubscriber_Multiple_AreNotifiedWhenLatchSet;
var var
latch: IMycLatch; latch: IMycEvent;
mockSub1, mockSub2, mockSub3: TMockSubscriber; mockSub1, mockSub2, mockSub3: TMockSubscriber;
sub1, sub2, sub3: TState.TSubscription; // Keep subscriptions in scope sub1, sub2, sub3: TState.TSubscription; // Keep subscriptions in scope
begin begin
latch := TLatch.Construct(1); latch := TEvent.CreateLatch(1);
mockSub1 := TMockSubscriber.Create; mockSub1 := TMockSubscriber.Create;
mockSub2 := TMockSubscriber.Create; mockSub2 := TMockSubscriber.Create;
mockSub3 := TMockSubscriber.Create; mockSub3 := TMockSubscriber.Create;
@@ -218,11 +217,11 @@ end;
procedure TTestMycLatch.TestSubscriber_NotNotified_BeforeLatchSet; procedure TTestMycLatch.TestSubscriber_NotNotified_BeforeLatchSet;
var var
latch: IMycLatch; latch: IMycEvent;
mockSub: TMockSubscriber; mockSub: TMockSubscriber;
subscription: TState.TSubscription; subscription: TState.TSubscription;
begin begin
latch := TLatch.Construct(2); // Count = 2 latch := TEvent.CreateLatch(2); // Count = 2
mockSub := TMockSubscriber.Create; mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub); subscription := latch.State.Subscribe(mockSub);
@@ -233,11 +232,11 @@ end;
procedure TTestMycLatch.TestSubscriber_NotifiedOnlyOnce_AfterLatchSetAndUnadvised; procedure TTestMycLatch.TestSubscriber_NotifiedOnlyOnce_AfterLatchSetAndUnadvised;
var var
latch: IMycLatch; latch: IMycEvent;
mockSub: TMockSubscriber; mockSub: TMockSubscriber;
subscription: TState.TSubscription; subscription: TState.TSubscription;
begin begin
latch := TLatch.Construct(1); latch := TEvent.CreateLatch(1);
mockSub := TMockSubscriber.Create; mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub); subscription := latch.State.Subscribe(mockSub);
@@ -250,11 +249,11 @@ end;
procedure TTestMycLatch.TestSubscribe_ToAlreadySetLatch_NotifiesImmediately; procedure TTestMycLatch.TestSubscribe_ToAlreadySetLatch_NotifiesImmediately;
var var
latch: IMycLatch; latch: IMycEvent;
mockSub1, mockSub2: TMockSubscriber; mockSub1, mockSub2: TMockSubscriber;
subscription1, subscription2: TState.TSubscription; subscription1, subscription2: TState.TSubscription;
begin begin
latch := TLatch.Construct(1); // Create with count 1 latch := TEvent.CreateLatch(1); // Create with count 1
mockSub1 := TMockSubscriber.Create; mockSub1 := TMockSubscriber.Create;
subscription1 := latch.State.Subscribe(mockSub1); // Subscribe before set subscription1 := latch.State.Subscribe(mockSub1); // Subscribe before set
@@ -278,12 +277,12 @@ end;
procedure TTestMycLatch.TestChaining_A_Notifies_B; procedure TTestMycLatch.TestChaining_A_Notifies_B;
var var
latchA, latchB: IMycLatch; latchA, latchB: IMycEvent;
mockSubForB: TMockSubscriber; mockSubForB: TMockSubscriber;
subHandle_B_listens_A, subHandle_Mock_listens_B: TState.TSubscription; subHandle_B_listens_A, subHandle_Mock_listens_B: TState.TSubscription;
begin begin
latchA := TLatch.Construct(1); latchA := TEvent.CreateLatch(1);
latchB := TLatch.Construct(1); // latchB is an IMycSubscriber latchB := TEvent.CreateLatch(1); // latchB is an IMycSubscriber
mockSubForB := TMockSubscriber.Create; mockSubForB := TMockSubscriber.Create;
subHandle_Mock_listens_B := latchB.State.Subscribe(mockSubForB); subHandle_Mock_listens_B := latchB.State.Subscribe(mockSubForB);
@@ -303,13 +302,13 @@ end;
procedure TTestMycLatch.TestCascade_A_Notifies_B_Notifies_C; procedure TTestMycLatch.TestCascade_A_Notifies_B_Notifies_C;
var var
latchA, latchB, latchC: IMycLatch; latchA, latchB, latchC: IMycEvent;
mockSubForC: TMockSubscriber; mockSubForC: TMockSubscriber;
sub_Mock_C, sub_C_B, sub_B_A: TState.TSubscription; sub_Mock_C, sub_C_B, sub_B_A: TState.TSubscription;
begin begin
latchA := TLatch.Construct(1); latchA := TEvent.CreateLatch(1);
latchB := TLatch.Construct(1); latchB := TEvent.CreateLatch(1);
latchC := TLatch.Construct(1); latchC := TEvent.CreateLatch(1);
mockSubForC := TMockSubscriber.Create; mockSubForC := TMockSubscriber.Create;
sub_Mock_C := latchC.State.Subscribe(mockSubForC); sub_Mock_C := latchC.State.Subscribe(mockSubForC);
@@ -326,14 +325,14 @@ end;
procedure TTestMycLatch.TestChaining_B_NeedsMultipleNotifiesFromA_WithSubscriberOnB; procedure TTestMycLatch.TestChaining_B_NeedsMultipleNotifiesFromA_WithSubscriberOnB;
var var
latchA, latchB: IMycLatch; latchA, latchB: IMycEvent;
mockSubForB: TMockSubscriber; mockSubForB: TMockSubscriber;
sub_Mock_B, sub_B_A: TState.TSubscription; sub_Mock_B, sub_B_A: TState.TSubscription;
begin begin
// LatchA needs 2 notifies to become set. LatchB needs 1 notify to become set. // LatchA needs 2 notifies to become set. LatchB needs 1 notify to become set.
// LatchB subscribes to LatchA. So LatchB will be notified once LatchA becomes set. // LatchB subscribes to LatchA. So LatchB will be notified once LatchA becomes set.
latchA := TLatch.Construct(2); latchA := TEvent.CreateLatch(2);
latchB := TLatch.Construct(1); latchB := TEvent.CreateLatch(1);
mockSubForB := TMockSubscriber.Create; mockSubForB := TMockSubscriber.Create;
sub_Mock_B := latchB.State.Subscribe(mockSubForB); sub_Mock_B := latchB.State.Subscribe(mockSubForB);
@@ -355,11 +354,11 @@ end;
procedure TTestMycLatch.TestInitiallySetLatch_NotifiesNewSubscriberImmediately; procedure TTestMycLatch.TestInitiallySetLatch_NotifiesNewSubscriberImmediately;
var var
latch: IMycLatch; // Will be TMycLatch.Null latch: IMycEvent; // Will be TMycLatch.Null
mockSub: TMockSubscriber; mockSub: TMockSubscriber;
subscription: TState.TSubscription; subscription: TState.TSubscription;
begin begin
latch := TLatch.Construct(0); // Returns TMycLatch.Null which is set. latch := TEvent.CreateLatch(0); // Returns TMycLatch.Null which is set.
mockSub := TMockSubscriber.Create; mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub); // TMycLatch.Subscribe notifies immediately if already set. subscription := latch.State.Subscribe(mockSub); // TMycLatch.Subscribe notifies immediately if already set.
@@ -370,11 +369,11 @@ end;
procedure TTestMycLatch.TestUnsubscribe_SubscriberNotNotified; procedure TTestMycLatch.TestUnsubscribe_SubscriberNotNotified;
var var
latch: IMycLatch; latch: IMycEvent;
mockSub: TMockSubscriber; mockSub: TMockSubscriber;
subscription: TState.TSubscription; subscription: TState.TSubscription;
begin begin
latch := TLatch.Construct(1); latch := TEvent.CreateLatch(1);
mockSub := TMockSubscriber.Create; mockSub := TMockSubscriber.Create;
subscription := latch.State.Subscribe(mockSub); subscription := latch.State.Subscribe(mockSub);
@@ -389,11 +388,11 @@ end;
procedure TTestMycLatch.TestMultipleTriggersNoSubscriber_StateCorrectForLaterSubscriber; procedure TTestMycLatch.TestMultipleTriggersNoSubscriber_StateCorrectForLaterSubscriber;
var var
latch: IMycLatch; latch: IMycEvent;
mockSub: TMockSubscriber; mockSub: TMockSubscriber;
subscription: TState.TSubscription; subscription: TState.TSubscription;
begin begin
latch := TLatch.Construct(3); latch := TEvent.CreateLatch(3);
latch.Notify; // Count -> 2 latch.Notify; // Count -> 2
latch.Notify; // Count -> 1 latch.Notify; // Count -> 1
@@ -412,11 +411,11 @@ end;
procedure TTestMycLatch.TestLatchSet_ClearsSubscribers_NoFurtherNotification; procedure TTestMycLatch.TestLatchSet_ClearsSubscribers_NoFurtherNotification;
var var
latch: IMycLatch; latch: IMycEvent;
mockSub1, mockSub2: TMockSubscriber; mockSub1, mockSub2: TMockSubscriber;
subscription1, subscription2: TState.TSubscription; subscription1, subscription2: TState.TSubscription;
begin begin
latch := TLatch.Construct(1); latch := TEvent.CreateLatch(1);
mockSub1 := TMockSubscriber.Create; mockSub1 := TMockSubscriber.Create;
mockSub2 := TMockSubscriber.Create; mockSub2 := TMockSubscriber.Create;
@@ -440,11 +439,11 @@ end;
procedure TTestMycLatch.TestLatchSet_SubscriptionFinalize_IsSafePostUnadviseAll; procedure TTestMycLatch.TestLatchSet_SubscriptionFinalize_IsSafePostUnadviseAll;
var var
latch: IMycLatch; latch: IMycEvent;
mockSub: TMockSubscriber; 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 begin
latch := TLatch.Construct(1); latch := TEvent.CreateLatch(1);
mockSub := TMockSubscriber.Create; mockSub := TMockSubscriber.Create;
var noException: Boolean := True; var noException: Boolean := True;
@@ -476,11 +475,11 @@ end;
procedure TTestMycLatch.TestLatchSet_NewSubscriberToSetLatch_NotifiedImmediatelyButNotPersistedForLatchNotify; procedure TTestMycLatch.TestLatchSet_NewSubscriberToSetLatch_NotifiedImmediatelyButNotPersistedForLatchNotify;
var var
latch: IMycLatch; latch: IMycEvent;
mockSubInitial, mockSubNew: TMockSubscriber; mockSubInitial, mockSubNew: TMockSubscriber;
subscriptionInitial, subscriptionNew: TState.TSubscription; subscriptionInitial, subscriptionNew: TState.TSubscription;
begin begin
latch := TLatch.Construct(1); latch := TEvent.CreateLatch(1);
mockSubInitial := TMockSubscriber.Create; mockSubInitial := TMockSubscriber.Create;
subscriptionInitial := latch.State.Subscribe(mockSubInitial); subscriptionInitial := latch.State.Subscribe(mockSubInitial);
-5
View File
@@ -257,11 +257,6 @@ begin
end; end;
end; end;
function FindNextFile(const Filename: String): String;
begin
end;
class destructor TDataSeries<T>.CreateClass; class destructor TDataSeries<T>.CreateClass;
begin begin
FCachedFiles := TDictionary<String, TCachedFile>.Create; FCachedFiles := TDictionary<String, TCachedFile>.Create;
+72
View File
@@ -0,0 +1,72 @@
unit Myc.Trade.Ticker;
interface
uses
Myc.Lazy;
type
TMycLogger = reference to procedure(const Msg: String);
IMycTradeObject = interface
end;
TMycTradeObject = class(TInterfacedObject, IMycTradeObject)
private
FCaption: string;
FLog: TMycLogger;
function GetCaption: string;
public
constructor Create(const ACaption: string; ALog: TMycLogger);
property Caption: string read GetCaption;
property Log: TMycLogger read FLog;
end;
IMycTime = interface
{$REGION 'property access'}
function GetTimeStamp: TDateTime;
{$ENDREGION}
property TimeStamp: TDateTime read GetTimeStamp;
end;
IMycTick = interface
{$REGION 'property access'}
function GetAsk: Double;
function GetBid: Double;
function GetTime: IMycTime;
function GetVolume: Double;
{$ENDREGION}
property Ask: Double read GetAsk;
property Bid: Double read GetBid;
property Time: IMycTime read GetTime;
property Volume: Double read GetVolume;
end;
IMycTicker = interface(IMycTradeObject)
{$REGION 'property access'}
function GetLastTick: TLazy<IMycTick>;
function GetCurrentTime: IMycTime;
{$ENDREGION}
property LastTick: TLazy<IMycTick> read GetLastTick;
property CurrentTime: IMycTime read GetCurrentTime;
end;
IMycHistoryTicker = interface(IMycTicker)
end;
implementation
constructor TMycTradeObject.Create(const ACaption: string; ALog: TMycLogger);
begin
inherited Create;
FCaption := ACaption;
FLog := procedure(const Msg: String) begin ALog('[' + FCaption + '] ' + Msg); end;
end;
function TMycTradeObject.GetCaption: string;
begin
Result := FCaption;
end;
end.
+4 -3
View File
@@ -17,10 +17,8 @@ uses
TestNotifier in 'TestNotifier.pas', TestNotifier in 'TestNotifier.pas',
TestNotifier_Threading in 'TestNotifier_Threading.pas' {/TestNotifier_ChaosStress in 'TestNotifier_ChaosStress.pas',}, TestNotifier_Threading in 'TestNotifier_Threading.pas' {/TestNotifier_ChaosStress in 'TestNotifier_ChaosStress.pas',},
TestNotifier_ChaosStress in 'TestNotifier_ChaosStress.pas', TestNotifier_ChaosStress in 'TestNotifier_ChaosStress.pas',
TestSignals_Latch in 'TestSignals_Latch.pas',
Myc.Core.Tasks in '..\Src\Myc.Core.Tasks.pas', Myc.Core.Tasks in '..\Src\Myc.Core.Tasks.pas',
TestTasks in 'TestTasks.pas', TestTasks in 'TestTasks.pas',
TestSignals_Dirty in 'TestSignals_Dirty.pas',
Myc.Futures in '..\Src\Myc.Futures.pas', Myc.Futures in '..\Src\Myc.Futures.pas',
TestCoreFutures in 'TestCoreFutures.pas', TestCoreFutures in 'TestCoreFutures.pas',
Myc.TaskManager in '..\Src\Myc.TaskManager.pas', Myc.TaskManager in '..\Src\Myc.TaskManager.pas',
@@ -31,7 +29,10 @@ uses
Myc.Core.Lazy in '..\Src\Myc.Core.Lazy.pas', Myc.Core.Lazy in '..\Src\Myc.Core.Lazy.pas',
Myc.Test.Core.Lazy in '..\Src\Myc.Test.Core.Lazy.pas', Myc.Test.Core.Lazy in '..\Src\Myc.Test.Core.Lazy.pas',
Myc.Test.Lazy in '..\Src\Myc.Test.Lazy.pas', Myc.Test.Lazy in '..\Src\Myc.Test.Lazy.pas',
Myc.Test.Core.Atomic in '..\Src\Myc.Test.Core.Atomic.pas'; Myc.Test.Core.Atomic in '..\Src\Myc.Test.Core.Atomic.pas',
Myc.Trade.Ticker in '..\Src\Myc.Trade.Ticker.pas',
Myc.Test.Signals.Latch in '..\Src\Myc.Test.Signals.Latch.pas',
Myc.Test.Signals.Dirty in '..\Src\Myc.Test.Signals.Dirty.pas';
{ keep comment here to protect the following conditional from being removed by the IDE when adding a unit } { keep comment here to protect the following conditional from being removed by the IDE when adding a unit }
{$IFNDEF TESTINSIGHT} {$IFNDEF TESTINSIGHT}
+3 -2
View File
@@ -114,10 +114,8 @@ $(PreBuildEvent)]]></PreBuildEvent>
<Form>/TestNotifier_ChaosStress in &apos;TestNotifier_ChaosStress.pas&apos;,</Form> <Form>/TestNotifier_ChaosStress in &apos;TestNotifier_ChaosStress.pas&apos;,</Form>
</DCCReference> </DCCReference>
<DCCReference Include="TestNotifier_ChaosStress.pas"/> <DCCReference Include="TestNotifier_ChaosStress.pas"/>
<DCCReference Include="TestSignals_Latch.pas"/>
<DCCReference Include="..\Src\Myc.Core.Tasks.pas"/> <DCCReference Include="..\Src\Myc.Core.Tasks.pas"/>
<DCCReference Include="TestTasks.pas"/> <DCCReference Include="TestTasks.pas"/>
<DCCReference Include="TestSignals_Dirty.pas"/>
<DCCReference Include="..\Src\Myc.Futures.pas"/> <DCCReference Include="..\Src\Myc.Futures.pas"/>
<DCCReference Include="TestCoreFutures.pas"/> <DCCReference Include="TestCoreFutures.pas"/>
<DCCReference Include="..\Src\Myc.TaskManager.pas"/> <DCCReference Include="..\Src\Myc.TaskManager.pas"/>
@@ -129,6 +127,9 @@ $(PreBuildEvent)]]></PreBuildEvent>
<DCCReference Include="..\Src\Myc.Test.Core.Lazy.pas"/> <DCCReference Include="..\Src\Myc.Test.Core.Lazy.pas"/>
<DCCReference Include="..\Src\Myc.Test.Lazy.pas"/> <DCCReference Include="..\Src\Myc.Test.Lazy.pas"/>
<DCCReference Include="..\Src\Myc.Test.Core.Atomic.pas"/> <DCCReference Include="..\Src\Myc.Test.Core.Atomic.pas"/>
<DCCReference Include="..\Src\Myc.Trade.Ticker.pas"/>
<DCCReference Include="..\Src\Myc.Test.Signals.Latch.pas"/>
<DCCReference Include="..\Src\Myc.Test.Signals.Dirty.pas"/>
<BuildConfiguration Include="Base"> <BuildConfiguration Include="Base">
<Key>Base</Key> <Key>Base</Key>
</BuildConfiguration> </BuildConfiguration>
+14 -14
View File
@@ -16,9 +16,9 @@ type
// Helper class to notify a latch when its Notify method is called. // Helper class to notify a latch when its Notify method is called.
TLatchNotifierSubscriber = class(TInterfacedObject, IMycSubscriber) TLatchNotifierSubscriber = class(TInterfacedObject, IMycSubscriber)
private private
FGateLatch: IMycLatch; FGateLatch: IMycEvent;
public public
constructor Create(AGateLatch: IMycLatch); constructor Create(AGateLatch: IMycEvent);
// IMycSubscriber // IMycSubscriber
function Notify: Boolean; // Implements IMycSubscriber.Notify [cite: 46, 181, 299] function Notify: Boolean; // Implements IMycSubscriber.Notify [cite: 46, 181, 299]
end; end;
@@ -54,7 +54,7 @@ implementation
{ TLatchNotifierSubscriber } { TLatchNotifierSubscriber }
constructor TLatchNotifierSubscriber.Create(AGateLatch: IMycLatch); constructor TLatchNotifierSubscriber.Create(AGateLatch: IMycEvent);
begin begin
inherited Create; inherited Create;
FGateLatch := AGateLatch; FGateLatch := AGateLatch;
@@ -121,12 +121,12 @@ end;
procedure TTestMycGateFuncFuture.Test_ChainedExecution_WithDelayedInitState; procedure TTestMycGateFuncFuture.Test_ChainedExecution_WithDelayedInitState;
var var
LFuture: IMycFuture<string>; LFuture: IMycFuture<string>;
LInitLatch: IMycLatch; LInitLatch: IMycEvent;
LResultValue: string; LResultValue: string;
const const
CExpectedResult = 'ChainCompleted'; CExpectedResult = 'ChainCompleted';
begin begin
LInitLatch := TLatch.Construct(1); // Create an init state that is not yet set [cite: 77, 212, 330] LInitLatch := TEvent.CreateLatch(1); // Create an init state that is not yet set [cite: 77, 212, 330]
LFuture := LFuture :=
TMycGateFuncFuture<string>.Create( TMycGateFuncFuture<string>.Create(
@@ -220,9 +220,9 @@ end;
procedure TTestMycGateFuncFuture.Test_GetResult_BeforeDone_RaisesException; procedure TTestMycGateFuncFuture.Test_GetResult_BeforeDone_RaisesException;
var var
LFuture: IMycFuture<Integer>; LFuture: IMycFuture<Integer>;
LInitLatch: IMycLatch; LInitLatch: IMycEvent;
begin begin
LInitLatch := TLatch.Construct(1); LInitLatch := TEvent.CreateLatch(1);
LFuture := LFuture :=
TMycGateFuncFuture<Integer>.Create( TMycGateFuncFuture<Integer>.Create(
@@ -250,9 +250,9 @@ procedure TTestMycGateFuncFuture.Test_FanIn_OneFutureWaitsForMultipleOthers;
var var
LPrerequisiteFuture1, LPrerequisiteFuture2: IMycFuture<Integer>; LPrerequisiteFuture1, LPrerequisiteFuture2: IMycFuture<Integer>;
LMainFuture: IMycFuture<string>; LMainFuture: IMycFuture<string>;
LGateLatch: IMycLatch; LGateLatch: IMycEvent;
LSub1, LSub2: IMycSubscriber; // To hold the TLatchNotifierSubscriber instances LSub1, LSub2: IMycSubscriber; // To hold the TLatchNotifierSubscriber instances
LInitStateP1, LInitStateP2: IMycLatch; LInitStateP1, LInitStateP2: IMycEvent;
Subscriptions: array[1..2] of TState.TSubscription; // To manage subscriptions Subscriptions: array[1..2] of TState.TSubscription; // To manage subscriptions
const const
CMainFutureResult = 'AllPrerequisitesCompleted'; CMainFutureResult = 'AllPrerequisitesCompleted';
@@ -260,7 +260,7 @@ begin
FProcExecutionCount := 0; // Reset for this test FProcExecutionCount := 0; // Reset for this test
// Gate Latch: MainFuture waits for this latch, which needs 2 notifications. // Gate Latch: MainFuture waits for this latch, which needs 2 notifications.
LGateLatch := TLatch.Construct(2); // [cite: 77, 212, 330] LGateLatch := TEvent.CreateLatch(2); // [cite: 77, 212, 330]
// Create MainFuture, AInitState is the GateLatch's state. // Create MainFuture, AInitState is the GateLatch's state.
LMainFuture := LMainFuture :=
@@ -276,7 +276,7 @@ begin
// Setup Prerequisite Futures // Setup Prerequisite Futures
// PrerequisiteFuture1 // PrerequisiteFuture1
LInitStateP1 := TLatch.Construct(1); // Controllable init state for PF1 LInitStateP1 := TEvent.CreateLatch(1); // Controllable init state for PF1
LPrerequisiteFuture1 := LPrerequisiteFuture1 :=
TMycGateFuncFuture<Integer>.Create( TMycGateFuncFuture<Integer>.Create(
FTaskFactory, FTaskFactory,
@@ -291,7 +291,7 @@ begin
Subscriptions[1] := LPrerequisiteFuture1.Done.Subscribe(LSub1); // Subscribe to PF1's completion [cite: 56, 188, 306] Subscriptions[1] := LPrerequisiteFuture1.Done.Subscribe(LSub1); // Subscribe to PF1's completion [cite: 56, 188, 306]
// PrerequisiteFuture2 // PrerequisiteFuture2
LInitStateP2 := TLatch.Construct(1); // Controllable init state for PF2 LInitStateP2 := TEvent.CreateLatch(1); // Controllable init state for PF2
LPrerequisiteFuture2 := LPrerequisiteFuture2 :=
TMycGateFuncFuture<Integer>.Create( TMycGateFuncFuture<Integer>.Create(
FTaskFactory, FTaskFactory,
@@ -335,7 +335,7 @@ end;
procedure TTestMycGateFuncFuture.Test_FanOut_MultipleFuturesWaitForOneTrigger; procedure TTestMycGateFuncFuture.Test_FanOut_MultipleFuturesWaitForOneTrigger;
var var
LTriggerLatch: IMycLatch; LTriggerLatch: IMycEvent;
LFutureA, LFutureB: IMycFuture<Integer>; LFutureA, LFutureB: IMycFuture<Integer>;
LFlagFutureARan, LFlagFutureBRan: Boolean; LFlagFutureARan, LFlagFutureBRan: Boolean;
begin begin
@@ -343,7 +343,7 @@ begin
LFlagFutureBRan := False; LFlagFutureBRan := False;
Self.FSharedCounter := 0; // Reset shared counter for this test Self.FSharedCounter := 0; // Reset shared counter for this test
LTriggerLatch := TLatch.Construct(1); // Single trigger [cite: 77, 212, 330] LTriggerLatch := TEvent.CreateLatch(1); // Single trigger [cite: 77, 212, 330]
// Future A // Future A
LFutureA := LFutureA :=
+5 -5
View File
@@ -148,11 +148,11 @@ end;
procedure TTestFuture.TestConstructWithDelayedGate; procedure TTestFuture.TestConstructWithDelayedGate;
var var
fut: TFuture<Integer>; fut: TFuture<Integer>;
delayedGate: IMycLatch; // IMycLatch implements IMycState [cite: 58] delayedGate: IMycEvent; // IMycEvent implements IMycState [cite: 58]
resultValue: Integer; resultValue: Integer;
begin begin
delayedGate := TLatch.Construct(1); // Create a latch that requires one notification to be set [cite: 66] delayedGate := TEvent.CreateLatch(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.IsNotNull(delayedGate, 'The delayed gate (IMycEvent) should not be nil.'); // Static string
Assert.IsFalse(delayedGate.State.IsSet, 'The delayed gate should not be initially set.'); // Static string [cite: 59, 55] 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 // Construct a future with a gate that is not yet set
@@ -220,10 +220,10 @@ procedure TTestFuture.TestChainWithGate;
var var
fut1: TFuture<Integer>; fut1: TFuture<Integer>;
fut2: TFuture<string>; fut2: TFuture<string>;
delayedGate: IMycLatch; delayedGate: IMycEvent;
resultValue: string; resultValue: string;
begin begin
delayedGate := TLatch.Construct(1); // [cite: 66] delayedGate := TEvent.CreateLatch(1); // [cite: 66]
Assert.IsFalse(delayedGate.State.IsSet, 'The delayed gate for chain test should not be initially set.'); Assert.IsFalse(delayedGate.State.IsSet, 'The delayed gate for chain test should not be initially set.');
// Static string [cite: 59, 55] // Static string [cite: 59, 55]
-529
View File
@@ -1,529 +0,0 @@
unit TestSignals_Dirty;
interface
uses
DUnitX.TestFramework,
System.SysUtils,
Myc.Signals; // Unit under test
type
// Helper class to mock a subscriber (reuse or redefine if not in common unit)
TMockSubscriber = class(TInterfacedObject, IMycSubscriber)
public
NotifyCount: Integer;
NotifyShouldReturn: Boolean;
WasCalled: Boolean;
LastReturnedValueFromSource: Boolean; // To store what the source's Notify returned
constructor Create(AShouldReturn: Boolean = True);
function Notify: Boolean; // IMycSubscriber implementation.
end;
[TestFixture]
TTestMycDirtyFlag = class(TObject)
private
// Helper to create a dirty flag and optionally reset it for a clean initial state
function CreateAndPrepareDirtyFlag(StartDirty: Boolean = True): IMycDirty;
public
[Setup]
procedure Setup;
[TearDown]
procedure TearDown;
// Category 1: Basic State, Notify (as Subscriber method), and Reset
[Test]
procedure TestCreate_InitialStateIsDirty;
[Test]
procedure TestReset_FromDirtyState_BecomesCleanAndReturnsTrue;
[Test]
procedure TestReset_FromCleanState_StaysCleanAndReturnsFalse;
[Test]
procedure TestNotify_OnCleanFlag_SetsDirtyAndReturnsTrue;
[Test]
procedure TestNotify_OnDirtyFlag_StaysDirtyAndReturnsFalse;
// Category 2: Subscriber Notifications
[Test]
procedure TestSubscribe_ToAlreadyDirtyFlag_NotifiesSubscriberImmediately;
[Test]
procedure TestSubscribe_ToCleanFlag_DoesNotNotifySubscriberImmediately;
[Test]
procedure TestSubscriber_Notified_WhenFlagTransitionsToDirty;
[Test]
procedure TestSubscriber_NotNotified_WhenSettingAlreadyDirtyFlagViaNotify;
[Test]
procedure TestSubscriber_NotNotified_ByResetAction;
[Test]
procedure TestMultipleSubscribers_Notified_WhenFlagTransitionsToDirty;
[Test]
procedure TestUnsubscribe_SubscriberNotNotified;
// Category 3: Chaining Dirty Flags
[Test]
procedure TestChain_A_Notifies_B_SimplePropagation;
[Test]
procedure TestChain_A_Notifies_B_Notifies_C_SimplePropagation;
// Category 4: Chaining with Resets and Re-triggering (Consistency)
[Test]
procedure TestChain_A_B_ResetB_RetriggerA_BStaysCleanIfANotChanged;
[Test]
procedure TestChain_A_B_ResetA_ThenTriggerA_FullPropagationToB;
[Test]
procedure TestChain_A_B_C_IntermediateBReset_RetriggerA_PropagatesToC;
[Test]
procedure TestChain_A_B_C_AllReset_RetriggerA_FullPropagation;
[Test]
procedure TestChain_A_B_C_TriggerA_ResetA_TriggerA_EnsureCNotifiedCorrectly;
end;
implementation
{ TMockSubscriber }
constructor TMockSubscriber.Create(AShouldReturn: Boolean = True);
begin
inherited Create;
NotifyCount := 0;
NotifyShouldReturn := AShouldReturn;
WasCalled := False;
LastReturnedValueFromSource := False; // Default
end;
function TMockSubscriber.Notify: Boolean;
begin
Inc(NotifyCount);
WasCalled := True;
// This return value is what this subscriber tells the source signal.
// For testing, we usually want it to return false to signify it doesn't need more from this one event.
Result := NotifyShouldReturn;
end;
{ TTestMycDirtyFlag }
function TTestMycDirtyFlag.CreateAndPrepareDirtyFlag(StartDirty: Boolean = True): IMycDirty;
begin
Result := TDirty.Construct; // Initially dirty
if not StartDirty then
Result.Reset; // Reset to make it clean
Assert.AreEqual(StartDirty, Result.State.IsSet, 'CreateAndPrepareDirtyFlag initial state incorrect.');
end;
procedure TTestMycDirtyFlag.Setup;
begin
// Per-test setup
end;
procedure TTestMycDirtyFlag.TearDown;
begin
// Per-test teardown
end;
// Category 1: Basic State, Notify (as Subscriber method), and Reset
procedure TTestMycDirtyFlag.TestCreate_InitialStateIsDirty;
var
dirtyFlag: IMycDirty;
begin
dirtyFlag := TDirty.Construct;
Assert.IsTrue(dirtyFlag.State.IsSet, 'Newly created dirty flag should be IsSet (dirty).');
end;
procedure TTestMycDirtyFlag.TestReset_FromDirtyState_BecomesCleanAndReturnsTrue;
var
dirtyFlag: IMycDirty;
resetResult: Boolean;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(True); // Start dirty
resetResult := dirtyFlag.Reset;
Assert.IsFalse(dirtyFlag.State.IsSet, 'Flag should be clean (not IsSet) after Reset.');
Assert.IsTrue(resetResult, 'Reset() should return True when resetting a dirty flag.');
end;
procedure TTestMycDirtyFlag.TestReset_FromCleanState_StaysCleanAndReturnsFalse;
var
dirtyFlag: IMycDirty;
resetResult: Boolean;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(False); // Start clean
resetResult := dirtyFlag.Reset;
Assert.IsFalse(dirtyFlag.State.IsSet, 'Flag should remain clean (not IsSet) after Reset on clean flag.');
Assert.IsFalse(resetResult, 'Reset() should return False when resetting an already clean flag.');
end;
procedure TTestMycDirtyFlag.TestNotify_OnCleanFlag_SetsDirtyAndReturnsTrue;
var
dirtyFlag: IMycDirty;
notifyResult: Boolean;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(False); // Start clean
notifyResult := dirtyFlag.Notify; // Call IMycSubscriber.Notify to make it dirty
Assert.IsTrue(dirtyFlag.State.IsSet, 'Flag should be dirty (IsSet) after Notify on clean flag.');
Assert.IsTrue(notifyResult, 'Notify() should return True indicating a state change from clean to dirty.');
end;
procedure TTestMycDirtyFlag.TestNotify_OnDirtyFlag_StaysDirtyAndReturnsFalse;
var
dirtyFlag: IMycDirty;
notifyResult: Boolean;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(True); // Start dirty
notifyResult := dirtyFlag.Notify; // Call IMycSubscriber.Notify again
Assert.IsTrue(dirtyFlag.State.IsSet, 'Flag should remain dirty (IsSet).');
Assert.IsFalse(notifyResult, 'Notify() should return False as flag was already dirty (no state change to dirty).');
end;
// Category 2: Subscriber Notifications
procedure TTestMycDirtyFlag.TestSubscribe_ToAlreadyDirtyFlag_NotifiesSubscriberImmediately;
var
dirtyFlag: IMycDirty;
subscriber: TMockSubscriber;
subscription: TState.TSubscription;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(True); // Start dirty
subscriber := TMockSubscriber.Create;
subscription := dirtyFlag.State.Subscribe(subscriber);
Assert.AreEqual(1, subscriber.NotifyCount, 'Subscriber should be notified immediately when subscribing to an already dirty flag.');
Assert.IsTrue(subscriber.WasCalled, 'Subscriber WasCalled should be true.');
end;
procedure TTestMycDirtyFlag.TestSubscribe_ToCleanFlag_DoesNotNotifySubscriberImmediately;
var
dirtyFlag: IMycDirty;
subscriber: TMockSubscriber;
subscription: TState.TSubscription;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(False); // Start clean
subscriber := TMockSubscriber.Create;
subscription := dirtyFlag.State.Subscribe(subscriber);
Assert.AreEqual(0, subscriber.NotifyCount, 'Subscriber should NOT be notified immediately when subscribing to a clean flag.');
Assert.IsFalse(subscriber.WasCalled, 'Subscriber WasCalled should be false.');
end;
procedure TTestMycDirtyFlag.TestSubscriber_Notified_WhenFlagTransitionsToDirty;
var
dirtyFlag: IMycDirty;
subscriber: TMockSubscriber;
subscription: TState.TSubscription;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(False); // Start clean
subscriber := TMockSubscriber.Create;
subscription := dirtyFlag.State.Subscribe(subscriber);
Assert.AreEqual(0, subscriber.NotifyCount, 'Subscriber initially not notified.');
dirtyFlag.Notify; // Make the flag dirty
Assert.AreEqual(1, subscriber.NotifyCount, 'Subscriber should be notified once flag transitions to dirty.');
end;
procedure TTestMycDirtyFlag.TestSubscriber_NotNotified_WhenSettingAlreadyDirtyFlagViaNotify;
var
dirtyFlag: IMycDirty;
subscriber: TMockSubscriber;
subscription: TState.TSubscription;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(True); // Start dirty
subscriber := TMockSubscriber.Create;
subscription := dirtyFlag.State.Subscribe(subscriber); // Gets initial notification
Assert.AreEqual(1, subscriber.NotifyCount, 'Subscriber notified on subscribe to dirty flag.');
dirtyFlag.Notify; // Notify again, flag was already dirty
Assert.AreEqual(1, subscriber.NotifyCount, 'Subscriber should NOT be notified again if flag was already dirty.');
end;
procedure TTestMycDirtyFlag.TestSubscriber_NotNotified_ByResetAction;
var
dirtyFlag: IMycDirty;
subscriber: TMockSubscriber;
subscription: TState.TSubscription;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(True); // Start dirty
subscriber := TMockSubscriber.Create;
subscription := dirtyFlag.State.Subscribe(subscriber); // Gets initial notification
Assert.AreEqual(1, subscriber.NotifyCount, 'Subscriber notified on subscribe.');
dirtyFlag.Reset; // Reset the flag
Assert.IsFalse(dirtyFlag.State.IsSet, 'Flag is now clean.');
// Current TMycDirty.Reset does not notify subscribers.
Assert.AreEqual(1, subscriber.NotifyCount, 'Subscriber should NOT be notified by Reset action.');
end;
procedure TTestMycDirtyFlag.TestMultipleSubscribers_Notified_WhenFlagTransitionsToDirty;
var
dirtyFlag: IMycDirty;
sub1, sub2: TMockSubscriber;
subscription1, subscription2: TState.TSubscription;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(False); // Start clean
sub1 := TMockSubscriber.Create;
sub2 := TMockSubscriber.Create;
subscription1 := dirtyFlag.State.Subscribe(sub1);
subscription2 := dirtyFlag.State.Subscribe(sub2);
dirtyFlag.Notify; // Make flag dirty
Assert.AreEqual(1, sub1.NotifyCount, 'Subscriber 1 should be notified.');
Assert.AreEqual(1, sub2.NotifyCount, 'Subscriber 2 should be notified.');
end;
procedure TTestMycDirtyFlag.TestUnsubscribe_SubscriberNotNotified;
var
dirtyFlag: IMycDirty;
subscriber: TMockSubscriber;
subscription: TState.TSubscription;
begin
dirtyFlag := CreateAndPrepareDirtyFlag(False); // Start clean
subscriber := TMockSubscriber.Create;
subscription := dirtyFlag.State.Subscribe(subscriber);
subscription.Unsubscribe; // Explicitly unsubscribe
dirtyFlag.Notify; // Make flag dirty
Assert.AreEqual(0, subscriber.NotifyCount, 'Unsubscribed subscriber should not be notified.');
end;
// Category 3: Chaining Dirty Flags
// In these tests, FlagX.Notify means calling the IMycSubscriber.Notify method on FlagX.
// This makes FlagX dirty and potentially notifies its subscribers (like FlagY).
procedure TTestMycDirtyFlag.TestChain_A_Notifies_B_SimplePropagation;
var
flagA, flagB: IMycDirty;
subToB: TMockSubscriber;
subscriptionA_B, subscriptionMock_B: TState.TSubscription;
begin
flagA := CreateAndPrepareDirtyFlag(False); // A starts clean
flagB := CreateAndPrepareDirtyFlag(False); // B starts clean
subToB := TMockSubscriber.Create;
subscriptionMock_B := flagB.State.Subscribe(subToB);
subscriptionA_B := flagA.State.Subscribe(flagB); // B subscribes to A's state changes (B's Notify will be called)
flagA.Notify; // Make A dirty. This should trigger B.Notify
Assert.IsTrue(flagA.State.IsSet, 'Flag A should be dirty.');
Assert.IsTrue(flagB.State.IsSet, 'Flag B should become dirty due to A.');
Assert.AreEqual(1, subToB.NotifyCount, 'Subscriber to B should be notified once.');
end;
procedure TTestMycDirtyFlag.TestChain_A_Notifies_B_Notifies_C_SimplePropagation;
var
flagA, flagB, flagC: IMycDirty;
subToC: TMockSubscriber;
subscriptionB_A, subscriptionC_B, subscriptionMock_C: TState.TSubscription;
begin
flagA := CreateAndPrepareDirtyFlag(False);
flagB := CreateAndPrepareDirtyFlag(False);
flagC := CreateAndPrepareDirtyFlag(False);
subToC := TMockSubscriber.Create;
subscriptionMock_C := flagC.State.Subscribe(subToC);
subscriptionC_B := flagB.State.Subscribe(flagC); // C subscribes to B
subscriptionB_A := flagA.State.Subscribe(flagB); // B subscribes to A
flagA.Notify; // Make A dirty
Assert.IsTrue(flagA.State.IsSet, 'Flag A should be dirty in cascade.');
Assert.IsTrue(flagB.State.IsSet, 'Flag B should be dirty in cascade.');
Assert.IsTrue(flagC.State.IsSet, 'Flag C should be dirty in cascade.');
Assert.AreEqual(1, subToC.NotifyCount, 'Subscriber to C should be notified once in cascade.');
end;
// Category 4: Chaining with Resets and Re-triggering (Consistency)
procedure TTestMycDirtyFlag.TestChain_A_B_ResetB_RetriggerA_BStaysCleanIfANotChanged;
var
flagA, flagB: IMycDirty;
subToB: TMockSubscriber;
subscriptionA_B, subscriptionMock_B: TState.TSubscription;
begin
flagA := CreateAndPrepareDirtyFlag(False);
flagB := CreateAndPrepareDirtyFlag(False);
subToB := TMockSubscriber.Create;
subscriptionMock_B := flagB.State.Subscribe(subToB);
subscriptionA_B := flagA.State.Subscribe(flagB);
flagA.Notify; // A dirty -> B dirty. subToB notified.
Assert.IsTrue(flagB.State.IsSet, 'Flag B should be dirty initially.');
Assert.AreEqual(1, subToB.NotifyCount, 'Subscriber to B initially notified.');
flagB.Reset; // B becomes clean. A is still dirty.
Assert.IsFalse(flagB.State.IsSet, 'Flag B should be clean after reset.');
Assert.AreEqual(1, subToB.NotifyCount, 'Subscriber to B not notified by B.Reset.');
// Notify A again. A was already dirty. So A.Notify() returns false, A does not call ProtectedNotifyOwnSubscribers.
// Thus, B.Notify() is NOT called. B should remain clean.
flagA.Notify;
Assert.IsTrue(flagA.State.IsSet, 'Flag A remains dirty.');
Assert.IsFalse(flagB.State.IsSet, 'Flag B should remain clean if A did not transition to dirty.');
Assert.AreEqual(1, subToB.NotifyCount, 'Subscriber to B should not be notified again.');
end;
procedure TTestMycDirtyFlag.TestChain_A_B_ResetA_ThenTriggerA_FullPropagationToB;
var
flagA, flagB: IMycDirty;
subToB: TMockSubscriber;
subscriptionA_B, subscriptionMock_B: TState.TSubscription;
begin
flagA := CreateAndPrepareDirtyFlag(False);
flagB := CreateAndPrepareDirtyFlag(False);
subToB := TMockSubscriber.Create;
subscriptionMock_B := flagB.State.Subscribe(subToB);
subscriptionA_B := flagA.State.Subscribe(flagB);
flagA.Notify; // A dirty -> B dirty. subToB notified.
Assert.IsTrue(flagB.State.IsSet, 'Flag B should be dirty after A triggers.');
Assert.AreEqual(1, subToB.NotifyCount, 'subToB notified once.');
flagA.Reset; // A becomes clean. B is still dirty (state change of A to clean doesn't propagate as a "dirty" signal to B).
Assert.IsFalse(flagA.State.IsSet, 'Flag A is clean after reset.');
Assert.IsTrue(flagB.State.IsSet, 'Flag B remains dirty.'); // B's state doesn't change because A becoming clean doesn't call B.Notify
flagA.Notify; // A (was clean) becomes dirty again. A.Notify() returns true. A notifies B.
// B.Notify() is called. B was dirty. B.Notify() sets FFlag to true (no change), returns false.
// So B does NOT notify subToB again.
Assert.IsTrue(flagA.State.IsSet, 'Flag A is dirty again.');
Assert.IsTrue(flagB.State.IsSet, 'Flag B is dirty (was already, and A.Notify called B.Notify which set it dirty again).');
Assert.AreEqual(1, subToB.NotifyCount, 'subToB should NOT be notified again as B did not transition from clean to dirty.');
end;
procedure TTestMycDirtyFlag.TestChain_A_B_C_IntermediateBReset_RetriggerA_PropagatesToC;
var
flagA, flagB, flagC: IMycDirty;
subToC: TMockSubscriber;
subscriptionB_A, subscriptionC_B, subscriptionMock_C: TState.TSubscription;
begin
flagA := CreateAndPrepareDirtyFlag(False);
flagB := CreateAndPrepareDirtyFlag(False);
flagC := CreateAndPrepareDirtyFlag(False);
subToC := TMockSubscriber.Create;
subscriptionMock_C := flagC.State.Subscribe(subToC);
subscriptionC_B := flagB.State.Subscribe(flagC);
subscriptionB_A := flagA.State.Subscribe(flagB);
// Initial full propagation
flagA.Notify; // A dirty -> B dirty -> C dirty. subToC notified.
Assert.IsTrue(flagC.State.IsSet, 'Flag C should be dirty initially.');
Assert.AreEqual(1, subToC.NotifyCount, 'subToC initial count.');
// Intermediate reset
flagB.Reset; // B becomes clean. A is dirty, C is dirty.
Assert.IsFalse(flagB.State.IsSet, 'Flag B is clean.');
// Reset and re-trigger A
flagA.Reset; // A becomes clean.
Assert.IsFalse(flagA.State.IsSet, 'Flag A is clean.');
flagA.Notify; // A (was clean) -> A dirty. A notifies B.
// B.Notify() called. B was clean -> B dirty. B notifies C.
// C.Notify() called. C was dirty -> C set dirty (no change), C.Notify() returns false.
// So subToC is NOT notified again.
Assert.IsTrue(flagA.State.IsSet, 'Flag A is dirty again.');
Assert.IsTrue(flagB.State.IsSet, 'Flag B becomes dirty again.');
Assert.IsTrue(flagC.State.IsSet, 'Flag C is dirty (was already).');
Assert.AreEqual(1, subToC.NotifyCount, 'subToC should NOT be notified again.');
end;
procedure TTestMycDirtyFlag.TestChain_A_B_C_AllReset_RetriggerA_FullPropagation;
var
flagA, flagB, flagC: IMycDirty;
subToC: TMockSubscriber;
subscriptionB_A, subscriptionC_B, subscriptionMock_C: TState.TSubscription;
begin
flagA := CreateAndPrepareDirtyFlag(False);
flagB := CreateAndPrepareDirtyFlag(False);
flagC := CreateAndPrepareDirtyFlag(False);
subToC := TMockSubscriber.Create;
subscriptionMock_C := flagC.State.Subscribe(subToC);
subscriptionC_B := flagB.State.Subscribe(flagC);
subscriptionB_A := flagA.State.Subscribe(flagB);
// All are clean. Trigger A.
flagA.Notify; // A dirty -> B dirty -> C dirty. subToC notified.
Assert.IsTrue(flagC.State.IsSet, 'Flag C dirty after first full propagation.');
Assert.AreEqual(1, subToC.NotifyCount, 'subToC notified once.');
// Reset all
flagA.Reset;
flagB.Reset;
flagC.Reset;
Assert.IsFalse(flagA.State.IsSet, 'Flag A clean.');
Assert.IsFalse(flagB.State.IsSet, 'Flag B clean.');
Assert.IsFalse(flagC.State.IsSet, 'Flag C clean.');
// subToC count is still 1, it's not notified by resets.
// Re-trigger A
flagA.Notify; // A (was clean) -> A dirty. A notifies B.
// B.Notify() called. B was clean -> B dirty. B notifies C.
// C.Notify() called. C was clean -> C dirty. C notifies subToC.
Assert.IsTrue(flagA.State.IsSet, 'Flag A dirty on re-trigger.');
Assert.IsTrue(flagB.State.IsSet, 'Flag B dirty on re-trigger.');
Assert.IsTrue(flagC.State.IsSet, 'Flag C dirty on re-trigger.');
Assert.AreEqual(2, subToC.NotifyCount, 'subToC should be notified a second time.');
end;
procedure TTestMycDirtyFlag.TestChain_A_B_C_TriggerA_ResetA_TriggerA_EnsureCNotifiedCorrectly;
var
flagA, flagB, flagC: IMycDirty;
subToC: TMockSubscriber;
subscriptionB_A, subscriptionC_B, subscriptionMock_C: TState.TSubscription;
begin
flagA := CreateAndPrepareDirtyFlag(False);
flagB := CreateAndPrepareDirtyFlag(False);
flagC := CreateAndPrepareDirtyFlag(False);
subToC := TMockSubscriber.Create;
subscriptionMock_C := flagC.State.Subscribe(subToC);
subscriptionC_B := flagB.State.Subscribe(flagC);
subscriptionB_A := flagA.State.Subscribe(flagB);
// 1. Trigger A
flagA.Notify; // A, B, C become dirty. subToC notified (count = 1).
Assert.IsTrue(flagC.State.IsSet, 'C is dirty after 1st trigger.');
Assert.AreEqual(1, subToC.NotifyCount, 'subToC count after 1st trigger.');
// 2. Reset A (B and C remain dirty)
flagA.Reset;
Assert.IsFalse(flagA.State.IsSet, 'A is clean.');
Assert.IsTrue(flagB.State.IsSet, 'B remains dirty.');
Assert.IsTrue(flagC.State.IsSet, 'C remains dirty.');
// 3. Trigger A again
flagA.Notify; // A (was clean) becomes dirty. A notifies B.
// B.Notify() is called. B was dirty. B.Notify sets FFlag to true (no change), returns false.
// B does NOT notify C. C remains dirty. subToC is NOT notified again.
Assert.IsTrue(flagA.State.IsSet, 'A is dirty after 2nd trigger.');
Assert.IsTrue(flagB.State.IsSet, 'B is still dirty (state did not flip to clean then dirty).');
Assert.IsTrue(flagC.State.IsSet, 'C is still dirty.');
Assert.AreEqual(1, subToC.NotifyCount, 'subToC count should remain 1.');
end;
initialization
TDUnitX.RegisterTestFixture(TTestMycDirtyFlag);
end.
+14 -14
View File
@@ -86,10 +86,10 @@ end;
procedure TMycTaskFactoryTests.TestRunImmediateJob; procedure TMycTaskFactoryTests.TestRunImmediateJob;
var var
jobExecuted: Boolean; jobExecuted: Boolean;
jobCompletedLatch: IMycLatch; jobCompletedLatch: IMycEvent;
begin begin
jobExecuted := False; jobExecuted := False;
jobCompletedLatch := TLatch.Construct(1); // Changed from Signals.CreateLatch jobCompletedLatch := TEvent.CreateLatch(1); // Changed from Signals.CreateLatch
FFactory FFactory
.Run( .Run(
@@ -107,11 +107,11 @@ end;
procedure TMycTaskFactoryTests.TestRunDelayedJobExecutesAfterAllNotifies; procedure TMycTaskFactoryTests.TestRunDelayedJobExecutesAfterAllNotifies;
var var
jobExecuted: Boolean; jobExecuted: Boolean;
jobCompletedLatch: IMycLatch; jobCompletedLatch: IMycEvent;
subscriber: IMycSubscriber; subscriber: IMycSubscriber;
begin begin
jobExecuted := False; jobExecuted := False;
jobCompletedLatch := TLatch.Construct(1); // Changed from Signals.CreateLatch jobCompletedLatch := TEvent.CreateLatch(1); // Changed from Signals.CreateLatch
subscriber := subscriber :=
FFactory.Run( FFactory.Run(
@@ -124,7 +124,7 @@ begin
Assert.IsNotNull(subscriber, 'Run should return a subscriber for delayed job'); Assert.IsNotNull(subscriber, 'Run should return a subscriber for delayed job');
// Use TMycLatch.Null for comparison // Use TMycLatch.Null for comparison
Assert.AreNotEqual<IMycSubscriber>(TLatch.Null, subscriber, 'Subscriber should not be TMycLatch.Null for StartCount > 0'); Assert.AreNotEqual<IMycSubscriber>(TEvent.Null, subscriber, 'Subscriber should not be TMycLatch.Null for StartCount > 0');
subscriber.Notify; subscriber.Notify;
@@ -134,11 +134,11 @@ end;
procedure TMycTaskFactoryTests.TestWaitForAlreadySetState; procedure TMycTaskFactoryTests.TestWaitForAlreadySetState;
var var
alreadySetLatch: IMycLatch; alreadySetLatch: IMycEvent;
startTime, endTime: Cardinal; startTime, endTime: Cardinal;
begin begin
// TMycLatch.CreateLatch(0) will return TMycLatch.Null // TMycLatch.CreateLatch(0) will return TMycLatch.Null
alreadySetLatch := TLatch.Construct(0); // Changed from Signals.CreateLatch alreadySetLatch := TEvent.CreateLatch(0); // Changed from Signals.CreateLatch
Assert.IsTrue(alreadySetLatch.State.IsSet, 'Latch should be initially set'); Assert.IsTrue(alreadySetLatch.State.IsSet, 'Latch should be initially set');
startTime := TThread.GetTickCount; startTime := TThread.GetTickCount;
@@ -152,11 +152,11 @@ procedure TMycTaskFactoryTests.TestThreadInfoMethods;
var var
inMainThreadInJob: Boolean; inMainThreadInJob: Boolean;
inWorkerThreadInJob: Boolean; inWorkerThreadInJob: Boolean;
jobDoneLatch: IMycLatch; jobDoneLatch: IMycEvent;
begin begin
inMainThreadInJob := True; inMainThreadInJob := True;
inWorkerThreadInJob := False; inWorkerThreadInJob := False;
jobDoneLatch := TLatch.Construct(1); // Changed from Signals.CreateLatch jobDoneLatch := TEvent.CreateLatch(1); // Changed from Signals.CreateLatch
Assert.IsTrue(FFactory.InMainThread, 'Test method itself should be in main thread'); Assert.IsTrue(FFactory.InMainThread, 'Test method itself should be in main thread');
Assert.IsFalse(FFactory.InWorkerThread, 'Test method itself should not be in a factory worker thread'); Assert.IsFalse(FFactory.InWorkerThread, 'Test method itself should not be in a factory worker thread');
@@ -178,9 +178,9 @@ end;
procedure TMycTaskFactoryTests.TestExceptionPropagationFromJob; procedure TMycTaskFactoryTests.TestExceptionPropagationFromJob;
var var
jobDoneLatch: IMycLatch; jobDoneLatch: IMycEvent;
begin begin
jobDoneLatch := TLatch.Construct(1); jobDoneLatch := TEvent.CreateLatch(1);
FFactory.EnqueueJob( FFactory.EnqueueJob(
procedure procedure
@@ -222,12 +222,12 @@ end;
procedure TMycTaskFactoryTests.TestWaitForInWorkerThreadRaisesException; procedure TMycTaskFactoryTests.TestWaitForInWorkerThreadRaisesException;
var var
exceptionCaughtInJob: Boolean; exceptionCaughtInJob: Boolean;
jobDoneLatch: IMycLatch; jobDoneLatch: IMycEvent;
dummyStateToWaitFor: IMycState; dummyStateToWaitFor: IMycState;
begin begin
exceptionCaughtInJob := False; exceptionCaughtInJob := False;
jobDoneLatch := TLatch.Construct(1); jobDoneLatch := TEvent.CreateLatch(1);
dummyStateToWaitFor := TLatch.Construct(1).State; dummyStateToWaitFor := TEvent.CreateLatch(1).State;
FFactory.EnqueueJob( FFactory.EnqueueJob(
procedure procedure