Streamlining DataServer
This commit is contained in:
@@ -14,19 +14,20 @@ uses
|
||||
Myc.Trade.DataServer;
|
||||
|
||||
type
|
||||
// Define ITickData as an alias for the specific IDataPoint interface.
|
||||
ITickData = IDataPoint<ITick>;
|
||||
|
||||
[TestFixture]
|
||||
[IgnoreMemoryLeaks(true)]
|
||||
TTest_TABFileServer_Equivalence = class(TObject)
|
||||
private
|
||||
FServer: IDataServer<ITickData>;
|
||||
FExpectedData: TArray<TAskBid.TTick>;
|
||||
FServer: IDataServer<TDataPoint<TAskBidItem>>;
|
||||
public
|
||||
[SetupFixture]
|
||||
procedure SetupFixture;
|
||||
[Setup]
|
||||
procedure Setup;
|
||||
[TearDown]
|
||||
procedure TearDown;
|
||||
[TearDownFixture]
|
||||
procedure TearDownFixture;
|
||||
|
||||
[Test]
|
||||
procedure Test_ServerReturnsSameDataAs_LoadDataSeries;
|
||||
@@ -39,79 +40,70 @@ uses
|
||||
|
||||
const
|
||||
// The reference data file for this test.
|
||||
C_TEST_FILENAME = '\\COFFEE\TickData\Pepperstone\AUDNZD_2024_05.tab_zip';
|
||||
C_TEST_FILENAME = '\\COFFEE\TickData\Pepperstone\GER40_2025_03.tab_zip';
|
||||
// The chunk size for fetching data from the server.
|
||||
C_MAX_FETCH = 20;
|
||||
C_MAX_FETCH = 200;
|
||||
|
||||
{ TTest_TABFileServer_Equivalence }
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.SetupFixture;
|
||||
begin
|
||||
// IsMemoryLow := function: Boolean begin exit(false); end;
|
||||
// Load the ground truth data using the original LoadDataSeries function.
|
||||
// SetupTaskManagerMock;
|
||||
TAskBid.ClearCache;
|
||||
end;
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.TearDownFixture;
|
||||
begin
|
||||
IsMemoryLow := nil;
|
||||
end;
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.Setup;
|
||||
begin
|
||||
// SetupTaskManagerMock;
|
||||
|
||||
// IsMemoryLow := function: Boolean begin exit(false); end;
|
||||
|
||||
// 1. Load the ground truth data using the original LoadDataSeries function.
|
||||
FExpectedData := TAskBid.LoadDataSeries(C_TEST_FILENAME).WaitFor;
|
||||
|
||||
// 2. Set up the filename and the TABFileServer instance.
|
||||
FServer := TTABFileServer.Create(C_TEST_FILENAME, C_MAX_FETCH);
|
||||
FServer := TTABFileServer.Create(C_TEST_FILENAME);
|
||||
end;
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.TearDown;
|
||||
begin
|
||||
FServer := nil;
|
||||
SetLength(FExpectedData, 0);
|
||||
IsMemoryLow := nil;
|
||||
TAskBid.ClearCache;
|
||||
end;
|
||||
|
||||
procedure TTest_TABFileServer_Equivalence.Test_ServerReturnsSameDataAs_LoadDataSeries;
|
||||
var
|
||||
actualDataList: TList<ITickData>;
|
||||
chunk: TArray<ITickData>;
|
||||
newChunk: TLazy<TArray<ITickData>>;
|
||||
chunk: array[0..C_MAX_FETCH-1] of TDataPoint<TAskBidItem>;
|
||||
Dst: TArray<TDataPoint<TAskBidItem>>;
|
||||
i: Int64;
|
||||
begin
|
||||
actualDataList := TList<ITickData>.Create;
|
||||
try
|
||||
newChunk := TLazy<TArray<ITickData>>.Construct(FServer.Data);
|
||||
var ExpectedData := TAskBid.LoadDataSeries(C_TEST_FILENAME).WaitFor;
|
||||
SetLength( Dst, Length( ExpectedData ) );
|
||||
|
||||
// Loop until the server stops providing new data chunks.
|
||||
var cnt: Int64 := 0;
|
||||
while not FServer.IsLiveData.Value do
|
||||
begin
|
||||
FServer.Fetch;
|
||||
var cnt: Int64 := 0;
|
||||
var n: Integer;
|
||||
while not FServer.IsLiveData.Value and (cnt < Length(Dst)) do
|
||||
begin
|
||||
n := FServer.GetChunk(chunk);
|
||||
if n > 0 then
|
||||
Move( chunk[0], dst[cnt], n * sizeof( TDataPoint<TAskBidItem> ) );
|
||||
inc( cnt, n );
|
||||
end;
|
||||
|
||||
// Get the data chunk produced by the last Fetch call.
|
||||
if newChunk.Pop(chunk) then
|
||||
begin
|
||||
for var i := 0 to High(chunk) do
|
||||
begin
|
||||
actualDataList.Add(chunk[i]);
|
||||
Assert.AreEqual(chunk[i].Idx, cnt, 'Index mismatch');
|
||||
inc(cnt);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
// --- Assertions ---
|
||||
|
||||
// --- Assertions ---
|
||||
// 1. Verify that the total number of records matches.
|
||||
Assert.AreEqual(Length(ExpectedData), cnt, 'Data record count mismatch');
|
||||
|
||||
// 1. Verify that the total number of records matches.
|
||||
Assert.AreEqual(Length(FExpectedData), actualDataList.Count, 'Data record count mismatch');
|
||||
|
||||
// 2. Verify that the content of each record is identical.
|
||||
for var i := 0 to High(FExpectedData) do
|
||||
begin
|
||||
var expectedTick := FExpectedData[i];
|
||||
var actualTick := actualDataList[i];
|
||||
|
||||
// E2003: Removed assertion for 'Index', as it's not part of the IDataPoint interface.
|
||||
// The index is an implementation detail of the faulty ConvertTicks and cannot be tested here.
|
||||
Assert.AreEqual(expectedTick.OADateTime, actualTick.Time, 'Timestamp mismatch ');
|
||||
Assert.IsTrue(Abs(expectedTick.Data.Ask - actualTick.Data.Ask) < 0.0001, 'Ask price mismatch');
|
||||
Assert.IsTrue(Abs(expectedTick.Data.Bid - actualTick.Data.Bid) < 0.0001, 'Bid price mismatch');
|
||||
end;
|
||||
finally
|
||||
actualDataList.Free;
|
||||
// 2. Verify that the content of each record is identical.
|
||||
for n := 0 to High(ExpectedData) div 1000 do
|
||||
begin
|
||||
i := n * 1000;
|
||||
// E2003: Removed assertion for 'Index', as it's not part of the IDataPoint interface.
|
||||
// The index is an implementation detail of the faulty ConvertTicks and cannot be tested here.
|
||||
Assert.AreEqual(Dst[i].Idx, i, 'Index mismatch ');
|
||||
Assert.AreEqual(ExpectedData[i].TimeStamp, Dst[i].Time, 'Timestamp mismatch ');
|
||||
Assert.AreEqual(ExpectedData[i].Data.Ask, Dst[i].Data.Ask, 'Ask price mismatch');
|
||||
Assert.AreEqual(ExpectedData[i].Data.Bid, Dst[i].Data.Bid, 'Bid price mismatch');
|
||||
end;
|
||||
end;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user