91 lines
2.1 KiB
ObjectPascal
91 lines
2.1 KiB
ObjectPascal
unit Myc.TaskManager;
|
|
|
|
interface
|
|
|
|
uses
|
|
System.SysUtils,
|
|
Myc.Signals;
|
|
|
|
type
|
|
IMycTaskManager = interface
|
|
// TOD Dokumentation
|
|
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.
|
|
// After waiting, or if the state is already set, any first stored exception
|
|
// from any worker thread of this factory will be re-raised in the calling (main) thread.
|
|
// Raises ETaskException if called from within a worker thread.
|
|
procedure WaitFor(State: IMycState);
|
|
end;
|
|
|
|
var
|
|
TaskManager: IMycTaskManager;
|
|
|
|
procedure SetupTaskManagerMock;
|
|
|
|
implementation
|
|
|
|
uses
|
|
System.Generics.Collections;
|
|
|
|
type
|
|
TMycExecMock = class(TInterfacedObject, IMycSubscriber)
|
|
private
|
|
FProc: TProc;
|
|
public
|
|
constructor Create(const AProc: TProc);
|
|
function Notify: Boolean;
|
|
end;
|
|
|
|
TMycTaskManagerMock = class(TInterfacedObject, IMycTaskManager)
|
|
public
|
|
// IMycTaskManager
|
|
function CreateTask(const Gate: TState; const Proc: TProc): TState.TSubscription;
|
|
procedure WaitFor(State: IMycState);
|
|
|
|
constructor Create;
|
|
end;
|
|
|
|
{ TMycExecMock }
|
|
|
|
constructor TMycExecMock.Create(const AProc: TProc);
|
|
begin
|
|
inherited Create;
|
|
FProc := AProc;
|
|
end;
|
|
|
|
function TMycExecMock.Notify: Boolean;
|
|
begin
|
|
Result := Assigned(FProc);
|
|
if Result then
|
|
begin
|
|
FProc();
|
|
FProc := nil;
|
|
end;
|
|
end;
|
|
|
|
{ TMycTaskManagerMock }
|
|
|
|
constructor TMycTaskManagerMock.Create;
|
|
begin
|
|
inherited Create;
|
|
end;
|
|
|
|
function TMycTaskManagerMock.CreateTask(const Gate: TState; const Proc: TProc): TState.TSubscription;
|
|
begin
|
|
Result := Gate.Subscribe( TMycExecMock.Create( Proc ) );
|
|
end;
|
|
|
|
procedure TMycTaskManagerMock.WaitFor(State: IMycState);
|
|
begin
|
|
Assert( State.IsSet );
|
|
end;
|
|
|
|
procedure SetupTaskManagerMock;
|
|
begin
|
|
TaskManager := TMycTaskManagerMock.Create;
|
|
end;
|
|
|
|
end.
|