Skip to content

Commit

Permalink
#813 Unit testing MemoryFormat and Delta with result etc..
Browse files Browse the repository at this point in the history
  • Loading branch information
zhabis committed Jan 31, 2023
1 parent 7f70372 commit e912323
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/Azos.Sky.Server/Fabric/Server/FiberMemoryDelta.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public void WriteOneWay(BixWriter writer, int formatVersion)
writer.Write(json);
return;
}
else
{
writer.Write((string)null);
}

writer.Write(NextStep);
writer.Write(NextSliceInterval);
Expand All @@ -105,7 +109,7 @@ public void WriteOneWay(BixWriter writer, int formatVersion)
{
var change = Changes[i];
writer.Write(change.Key);
JsonWriter.Write(change.Value, JsonWritingOptions.CompactRowsAsMap);
writer.Write(JsonWriter.Write(change.Value, JsonWritingOptions.CompactRowsAsMap));
}
}

Expand Down
195 changes: 195 additions & 0 deletions src/testing/Azos.Tests.Unit/Fabric/MemoryDeltaFormatTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*<FILE_LICENSE>
* Azos (A to Z Application Operating System) Framework
* The A to Z Foundation (a.k.a. Azist) licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
</FILE_LICENSE>*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Azos.Data;
using Azos.Scripting;
using Azos.Serialization.Bix;
using Azos.Sky.Fabric;
using Azos.Sky.Fabric.Server;
using Azos.Time;

namespace Azos.Tests.Unit.Fabric
{
[Runnable]
public class MemoryDeltaFormatTests : IRunnableHook
{
public void Prologue(Runner runner, FID id)
{
Bixer.RegisterTypeSerializationCores(System.Reflection.Assembly.GetExecutingAssembly());
}
public bool Epilogue(Runner runner, FID id, Exception error) => false;

[Run]
public void Test01_Roundtrip_NextStep_NoResult()
{
var fid = new FiberId(Atom.Encode("sys"), Atom.Encode("s1"), new GDID(7, 12, 1234567890));
var pars = new TeztParams
{
Bool1 = true,
Int1 = 123456,
String1 = "BoltJolt"
};

var state = new TeztState();
state.FirstName = "Alex";
state.LastName = "Booster";
state.DOB = new DateTime(1980, 1, 2, 14, 15, 00, DateTimeKind.Utc);
state.AccountNumber = 987654321;
state.SetAttachment("donkey_fun.jpeg", new byte[]{1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5});
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), null, pars, state);//this packs buffer

using var wscope = BixWriterBufferScope.DefaultCapacity;
mem.WriteOneWay(wscope.Writer, 1);//Serialize by SHARD

using var rscope = new BixReaderBufferScope(wscope.Buffer);
var got = new FiberMemory(rscope.Reader);//Read by processor

Aver.AreEqual(mem.Id, got.Id);
Aver.IsTrue(mem.Status == got.Status);
Aver.AreEqual(mem.ImageTypeId, got.ImageTypeId);
Aver.AreEqual(mem.ImpersonateAs, got.ImpersonateAs);
Aver.AreArraysEquivalent(mem.Buffer, got.Buffer);
got.See();
var (gotParsBase, gotStateBase) = got.UnpackBuffer(typeof(TeztParams), typeof(TeztState));
var (gotPars, gotState) = (gotParsBase.CastTo<TeztParams>(), gotStateBase.CastTo<TeztState>());
Aver.AreEqual(pars.Int1, gotPars.Int1);
Aver.AreEqual(pars.String1, gotPars.String1);
Aver.AreEqual(state.AccountNumber, gotState.AccountNumber);
Aver.AreEqual(state.AttachmentName, gotState.AttachmentName);
Aver.AreArraysEquivalent(state.AttachmentContent, gotState.AttachmentContent);

//==== We are in processor pretending to use state now

Aver.IsFalse(got.HasDelta(gotState));
gotState.AccountNumber = 223322;//mutate state <====================
Aver.IsTrue(got.HasDelta(gotState));

var delta = got.MakeDeltaSnapshot(FiberStep.ContinueImmediately(Atom.Encode("step2")), gotState);

Aver.AreEqual("step2", delta.NextStep.Value);
Aver.AreEqual(new TimeSpan(0), delta.NextSliceInterval);
Aver.AreEqual(1, delta.Changes.Length);
Aver.IsNull(delta.Result);
Aver.AreEqual(0, delta.ExitCode);

wscope.Reset();
delta.WriteOneWay(wscope.Writer, 1);//written by processor

using var rscope2 = new BixReaderBufferScope(wscope.Buffer);
var gotDelta = new FiberMemoryDelta(rscope2.Reader);//read back by shard

gotDelta.See("Got delta: ");

Aver.AreEqual(mem.Id, gotDelta.Id);
Aver.AreEqual(got.Id, gotDelta.Id);


Aver.AreEqual("step2", gotDelta.NextStep.Value);
Aver.AreEqual(new TimeSpan(0), gotDelta.NextSliceInterval);
Aver.AreEqual(1, gotDelta.Changes.Length);
Aver.IsNull(gotDelta.Result);
Aver.AreEqual(0, gotDelta.ExitCode);

Aver.AreEqual("d", gotDelta.Changes[0].Key.Value);
Aver.AreEqual(223322ul, gotDelta.Changes[0].Value.CastTo<TeztState.DemographicsSlot>().AccountNumber);
Aver.AreEqual("Booster", gotDelta.Changes[0].Value.CastTo<TeztState.DemographicsSlot>().LastName);
}


[Run]
public void Test02_Roundtrip_FinalStep_WithResult()
{
var fid = new FiberId(Atom.Encode("sys"), Atom.Encode("s1"), new GDID(7, 12, 1234567890));
var pars = new TeztParams
{
Bool1 = true,
Int1 = 123456,
String1 = "BoltJolt"
};

var state = new TeztState();
state.FirstName = "Alex";
state.LastName = "Rooster";
state.DOB = new DateTime(1980, 1, 2, 14, 15, 00, DateTimeKind.Utc);
state.AccountNumber = 987654321;
state.SetAttachment("hockey_fun.jpeg", new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 });
var mem = new FiberMemory(1, MemoryStatus.LockedForCaller, fid, Guid.NewGuid(), null, pars, state);//this packs buffer

using var wscope = BixWriterBufferScope.DefaultCapacity;
mem.WriteOneWay(wscope.Writer, 1);//Serialize by SHARD

using var rscope = new BixReaderBufferScope(wscope.Buffer);
var got = new FiberMemory(rscope.Reader);//Read by processor

Aver.AreEqual(mem.Id, got.Id);
Aver.IsTrue(mem.Status == got.Status);
Aver.AreEqual(mem.ImageTypeId, got.ImageTypeId);
Aver.AreEqual(mem.ImpersonateAs, got.ImpersonateAs);
Aver.AreArraysEquivalent(mem.Buffer, got.Buffer);
got.See();
var (gotParsBase, gotStateBase) = got.UnpackBuffer(typeof(TeztParams), typeof(TeztState));
var (gotPars, gotState) = (gotParsBase.CastTo<TeztParams>(), gotStateBase.CastTo<TeztState>());
Aver.AreEqual(pars.Int1, gotPars.Int1);
Aver.AreEqual(pars.String1, gotPars.String1);
Aver.AreEqual(state.AccountNumber, gotState.AccountNumber);
Aver.AreEqual(state.AttachmentName, gotState.AttachmentName);
Aver.AreArraysEquivalent(state.AttachmentContent, gotState.AttachmentContent);

//==== We are in processor pretending to use state now

Aver.IsFalse(got.HasDelta(gotState));
gotState.AccountNumber = 55166;//mutate state <====================
gotState.LastName = "Shuster";
Aver.IsTrue(got.HasDelta(gotState));
var result = new TeztResult
{
Int1 = 900,
String1 = "Dallas"
};

var delta = got.MakeDeltaSnapshot(FiberStep.FinishWithResult(123, result), gotState);

Aver.IsTrue(delta.NextStep.IsZero);
Aver.AreEqual(new TimeSpan(0), delta.NextSliceInterval);
Aver.AreEqual(1, delta.Changes.Length);
Aver.IsNotNull(delta.Result);
Aver.AreEqual(123, delta.ExitCode);

wscope.Reset();
delta.WriteOneWay(wscope.Writer, 1);//written by processor

var wire = wscope.Buffer;
"{0} bytes of delta wired back to shard".SeeArgs(wire.Length);

using var rscope2 = new BixReaderBufferScope(wire);
var gotDelta = new FiberMemoryDelta(rscope2.Reader);//read back by shard

gotDelta.See("Got delta: ");

Aver.AreEqual(mem.Id, gotDelta.Id);
Aver.AreEqual(got.Id, gotDelta.Id);


Aver.IsTrue(gotDelta.NextStep.IsZero);
Aver.AreEqual(new TimeSpan(0), gotDelta.NextSliceInterval);
Aver.AreEqual(1, gotDelta.Changes.Length);
Aver.IsNotNull(gotDelta.Result);
Aver.AreEqual(123, gotDelta.ExitCode);

Aver.AreEqual("d", gotDelta.Changes[0].Key.Value);
Aver.AreEqual(55166ul, gotDelta.Changes[0].Value.CastTo<TeztState.DemographicsSlot>().AccountNumber);
Aver.AreEqual("Shuster", gotDelta.Changes[0].Value.CastTo<TeztState.DemographicsSlot>().LastName);
}


}
}
11 changes: 9 additions & 2 deletions src/testing/Azos.Tests.Unit/Fabric/TestObjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ public sealed class TeztParams : FiberParameters
[Field] public string String1 { get; set; }
}

[Bix("55322a1b-65cf-47eb-b73b-ba41caae4b7f")]
public sealed class TeztResult : FiberResult
{
[Field] public int Int1 { get; set; }
[Field] public string String1 { get; set; }
}


public sealed class TeztState : FiberState
{
public static readonly Atom SLOT_DEMOGRAPHICS = Atom.Encode("d");
public static readonly Atom SLOT_ATTACHMENT = Atom.Encode("a");
[Bix("5ea4d9a4-01e5-4cd0-a38a-0ae9588a5047")]
sealed class DemographicsSlot : Slot
internal sealed class DemographicsSlot : Slot
{
[Field] public string FirstName { get; set; }
[Field] public string LastName { get; set; }
Expand All @@ -40,7 +47,7 @@ sealed class DemographicsSlot : Slot
}

[Bix("91bc6ce6-dcf6-4d16-942a-4acd42588118")]
sealed class AttachmentSlot : Slot
internal sealed class AttachmentSlot : Slot
{
[Field] public string AttachmentName { get; set; }
[Field] public byte[] AttachContent { get; set; }
Expand Down

0 comments on commit e912323

Please sign in to comment.