public void BroadcastRangeTest() { SimulationRun run = new SimulationRun( new BaseDB.ConfigContainer() { extent = Int3.One, r = 1f, m = 1f }, new ShardID(Int3.Zero, 0)); Entity[] entities = new Entity[16]; entities[0] = new Entity(new EntityID(Guid.NewGuid(), Vec3.Zero), Vec3.Zero, new BroadcastLogic(entities.Length - 1)); for (int i = 1; i < entities.Length; i++) { entities[i] = new Entity(new EntityID(Guid.NewGuid(), new Vec3(1f / (entities.Length - 1) * i, 0, 0)), Vec3.Zero, new BroadcastReceiverLogic(entities.Length - i)); } run.FeedEntities(entities); run.AdvanceTLG(true, true); //send messages here run.AdvanceTLG(true, true); //receive messages here for (int i = 0; i < run.tlgEntry.SDS.FinalEntities.Length; i++) { var e = run.tlgEntry.SDS.FinalEntities[i]; BroadcastReceiverLogic logic = e.MyLogic as BroadcastReceiverLogic; if (logic != null) { Assert.AreEqual(logic.ShouldReceive, logic.numReceived, i.ToString()); } } }
public void ConsistentStateTest() { SimulationRun run = new SimulationRun( new BaseDB.ConfigContainer() { extent = Int3.One, r = 1f / 8, m = 1f / 16 }, new ShardID(Int3.Zero, 0), new Entity[] { new Entity( new EntityID(Guid.NewGuid(), Simulation.MySpace.Center), Vec3.Zero, new RoundState(), null), } ); foreach (var s in run.stack) { foreach (var e in s.SDS.FinalEntities) { RoundState st = e.MyLogic as RoundState; Assert.IsNotNull(st); Assert.AreEqual(s.Generation, st.state, "-1: " + s.Generation); } } const int NumIterations = 10; StringBuilder ks = new StringBuilder(); for (int i = 0; i < NumIterations; i++) { run.AdvanceTLG(true, true, false); if (i > 1) { int k = random.Next(1, i - 1); ks.Append(',').Append(k); Console.WriteLine(ks); run.RecomputeGeneration(k, false); } foreach (var s in run.stack) { foreach (var e in s.SDS.FinalEntities) { RoundState st = e.MyLogic as RoundState; Assert.IsNotNull(st); Assert.AreEqual(s.Generation, st.state, i + ": " + s.Generation); } } } }
public void PingPongTest() { SimulationRun run = new SimulationRun( new BaseDB.ConfigContainer() { extent = Int3.One, r = 1f / 8, m = 1f / 16 }, new ShardID(Int3.Zero, 0), new Entity[] { new Entity( new EntityID(Guid.NewGuid(), Simulation.MySpace.Center), Vec3.Zero, new PingLogic(new PingPacket(0), false), //check that this doesn't actually cause a fault (should get clamped) null), new Entity( new EntityID(Guid.NewGuid(), Simulation.MySpace.Center + new Vec3(Simulation.Ranges.R)), Vec3.Zero, new PingLogic(new PingPacket(0), true), //new EntityTest.FaultLogic.State(), null), } ); //EntityTest.RandomDefaultPool(100); const int NumIterations = 10; for (int i = 0; i < NumIterations; i++) { var rs = run.AdvanceTLG(true, true); Assert.AreEqual(rs.SDS.FinalEntities.Length, 2); } Assert.AreEqual(1, run.stack.Size); int sum = 0; foreach (var e in run.stack.Last().SDS.FinalEntities) { var state = Helper.Deserialize(e.SerialLogicState) as PingLogic; if (state != null) { int cs = state.CounterState; Assert.IsTrue(cs == NumIterations - 2 || cs == NumIterations - 1, cs.ToString()); sum += cs; //one should be at counter 8, one at 9 } } Assert.AreEqual(NumIterations * 2 - 3, sum); }
public void StupidModelTest2D() { int gridRes = 100; //2d resolution //each grid cell can 'see' +- 4 cells in all direction. All 'motion' is done via communication //hence R = 4 / gridRes float r = 4.5f / gridRes; SimulationRun run = new SimulationRun( new BaseDB.ConfigContainer() { extent = Int3.One, r = r, m = r * 0.5f }, new ShardID(Int3.Zero, 0), MakeGrid2D(gridRes)); for (int i = 0; i < 13; i++) { var sds = run.AdvanceTLG(true, true); Assert.AreEqual(sds.SDS.FinalEntities.Length, gridRes * gridRes); int numBugs = 0; int numPredators = 0; int numConflicts = 0; float totalFood = 0; foreach (var e in sds.SDS.FinalEntities) { Habitat h = (Habitat)Helper.Deserialize(e.SerialLogicState); if (h.bug.HasAnimal) { numBugs++; } if (h.predator.HasAnimal) { numPredators++; if (h.bug.HasAnimal) { numConflicts++; } } totalFood += h.food; } Console.WriteLine("Population: b=" + numBugs + ", p=" + numPredators + ", c=" + numConflicts + "; Food=" + totalFood); } }
public void ScriptedRemoteLogicInstantiationTest() { CSLogicProvider providerA = CSLogicProvider.CompileAsync("RemoteA", remoteTestA).Result; CSLogicProvider providerB = CSLogicProvider.CompileAsync("RemoteB", remoteTestB).Result; DB.LogicLoader = CSLogicProvider.AsyncFactory = scriptName => Task.Run(() => scriptName == providerA.AssemblyName ? providerA : providerB); SimulationRun run = new SimulationRun( new BaseDB.ConfigContainer() { extent = Int3.One, r = 1f / 8, m = 1f / 16 }, new ShardID(Int3.Zero, 0), new Entity[] { new Entity( new EntityID(Guid.NewGuid(), Simulation.MySpace.Center), Vec3.Zero, new DynamicCSLogic(providerA, "InstantiatorLogic", null), null), } ); const int NumIterations = 3; for (int i = 0; i < NumIterations; i++) { var rs = run.AdvanceTLG(true, true); int instantiations = rs.IntermediateSDS.localChangeSet.NamedSets.Where(pair => pair.Key == "instantiations").First().Value.Size; Assert.AreEqual(instantiations, 1); Assert.AreEqual(rs.IntermediateSDS.entities.Count, Math.Min(i + 1, 2)); //can never be more than 2 Assert.AreEqual(rs.SDS.FinalEntities.Length, 2); //previous clone self-destructed, so we are back to exactly 2 } Assert.AreEqual(1, run.stack.Size); foreach (var e in run.stack.Last().SDS.FinalEntities) { var st = Helper.Deserialize(e.SerialLogicState); Assert.IsTrue(st is DynamicCSLogic, st.GetType().ToString()); } }
public void IsolatedComputationTest() { SimulationRun run = new SimulationRun( new BaseDB.ConfigContainer() { extent = Int3.One, r = 1f / 8, m = 1f / 16 }, new ShardID(Int3.Zero, 0)); Vec3 outlierCoords = Simulation.MySpace.Min; foreach (var n in Simulation.Neighbors) { n.OnPutRCS = (rcs, gen) => { Assert.Fail("This test generates no simulation neighbors. Should not generate RCSs"); } } ; DB.OnPutSDS = dbSDS => { Assert.AreEqual(Entity.Import(dbSDS.SerialEntities).Length, 2); Assert.AreEqual(dbSDS.Generation, 1); Assert.AreEqual(new InconsistencyCoverage(dbSDS.IC).OneCount, 0); }; run.FeedEntities(new Entity[] { new Entity( new EntityID(Guid.NewGuid(), Simulation.MySpace.Center), Vec3.Zero, new ExceedingMovementLogic(), //check that this doesn't actually cause a fault (should get clamped) null), new Entity( new EntityID(Guid.NewGuid(), outlierCoords), Vec3.Zero, new StationaryLogic(), //new EntityTest.FaultLogic.State(), null), } ); var comp = run.BeginAdvanceTLG(true); foreach (var p in comp.Intermediate.localChangeSet.NamedSets) { int expected = p.Key == "motions" || p.Key == "advertisements" ? 2 : 0; Assert.AreEqual(expected, p.Value.Size); } Assert.AreEqual(comp.Generation, 1); var sds = run.CompleteAdvanceTLG(true); Assert.AreEqual(sds.Generation, 1); Assert.AreEqual(sds.SDS.FinalEntities.Length, 2); }
public void NestedComputationTest() { int numRCS = 0; SimulationRun run = new SimulationRun( new BaseDB.ConfigContainer() { extent = new Int3(3), r = 1f / 8, m = 1f / 16 }, new ShardID(Int3.One, 0)); Vec3 outlierCoords = Simulation.MySpace.Min; var crossingLogic = new MovingLogic(new Vec3(-1, 0, 0)); Entity crosser = new Entity(new EntityID(Guid.NewGuid(), Simulation.MySpace.Min), Vec3.Zero, crossingLogic, null); Vec3 crossingTarget = crosser.ID.Position + crossingLogic.Motion; foreach (var n in Simulation.Neighbors) { n.OnPutRCS = (decoded, gen) => { numRCS++; Assert.AreEqual(gen, 1); //RCS decoded = new RCS(rcs); Assert.IsTrue(decoded.IsFullyConsistent); //RCS.GenID id = new RCS.GenID(rcs.NumericID,0); //Link lnk = Simulation.Neighbors.Find(id.ID.ToShard); //Assert.IsNotNull(lnk); if (n.WorldSpace.Grow(Simulation.Ranges.Transmission).Contains(outlierCoords)) { Assert.IsFalse(decoded.CS.IsEmpty); } else { Assert.IsTrue(decoded.CS.IsEmpty); } if (n.WorldSpace.Contains(crossingTarget)) { Assert.IsNotNull(decoded.CS.FindMotionOf(crosser.ID.Guid)); } } } ; run.FeedEntities( new Entity[] { new Entity( new EntityID(Guid.NewGuid(), Simulation.MySpace.Center), Vec3.Zero, new ExceedingMovementLogic(), //new EntityTest.FaultLogic.State(), null), new Entity( new EntityID(Guid.NewGuid(), outlierCoords), Vec3.Zero, new StationaryLogic(), //new EntityTest.FaultLogic.State(), null), crosser } ); var inter = run.BeginAdvanceTLG(true); foreach (var p in inter.Intermediate.localChangeSet.NamedSets) { int expected = p.Key == "motions" || p.Key == "advertisements" ? 3 : 0; Assert.AreEqual(expected, p.Value.Size); } Assert.AreEqual(inter.Generation, 1); Assert.AreEqual(Simulation.NeighborCount, numRCS); //comp. { Link inbound = Simulation.Neighbors.Find(new Int3(0, 1, 1)); RCS inRCS = new RCS(new EntityChangeSet(), new InconsistencyCoverage(inbound.ICExportRegion.Size)); Simulation.FetchNeighborUpdate(run.tlgEntry, inbound, inRCS.Export()); } var rs = run.CompleteAdvanceTLG(false); //check if most outer cells are 1 (one full-edge incoming RCS): var core = rs.SDS.IC.Sub(Int3.One, rs.SDS.IC.Size - 2); int edgeSize = InconsistencyCoverage.CommonResolution - 2; Assert.AreEqual(rs.SDS.IC.OneCount, rs.SDS.IC.Size.Product - core.Size.Product - edgeSize * edgeSize, edgeSize.ToString()); Assert.IsTrue(rs.SDS.IC.OneCount > 0); Assert.IsTrue(core.OneCount == 0); Assert.AreEqual(rs.Generation, 1); Assert.AreEqual(rs.SDS.FinalEntities.Length, 2); Assert.IsFalse(rs.SDS.HasEntity(crosser.ID.Guid)); var check = Simulation.CheckMissingRCS(rs); Assert.IsFalse(check.AllThere); Assert.IsFalse(check.AnyAvailableFromNeighbors); Assert.AreEqual(check.missingRCS, numRCS - 1); Assert.IsTrue(check.predecessorIsConsistent); Assert.AreEqual(check.rcsAvailableFromNeighbor, 0); Assert.AreEqual(check.rcsRestoredFromDB, 0); }
public MyNotify(SimulationRun simulationRun) { this.simulationRun = simulationRun; }
public void FullInteractionLinkTest() { Random random = new Random(); var timing = new BaseDB.TimingContainer(); timing.msGenerationBudget = 210; timing.msComputation = 20; timing.msApplication = 20; timing.recoverySteps = 2; BaseDB.TimingPoller = new DBType.FunctionPollable <BaseDB.TimingContainer>(() => timing); SimulationRun run = new SimulationRun( new BaseDB.ConfigContainer() { extent = Int3.One, r = 1f / 8, m = 1f / 16 }, ShardID.Zero, new Entity[] { new Entity( new EntityID(Guid.NewGuid(), Simulation.MySpace.Center), Vec3.Zero, new SineLogic()), } ); var defaultPort = random.Next(1024, 32768); run.InstallConsensusCluster(3, defaultPort + 1, true); bool keepRunning = true; //parallel evolution: var task = Task.Run(() => { for (int i = 0; keepRunning; i++) { var rs = run.AdvanceTLG(true, true); Assert.AreEqual(rs.IntermediateSDS.entities.Count, 1); Assert.AreEqual(rs.SDS.FinalEntities.Length, 1); } }); List <TimeSpan> deltas = new List <TimeSpan>(); using (Listener listener = new Listener(null, defaultPort)) { InteractionLink myLink = null; AutoResetEvent onLink = new AutoResetEvent(false), onRegister = new AutoResetEvent(false), onUnregister = new AutoResetEvent(false), onMessage = new AutoResetEvent(false); Guid me = Guid.NewGuid(); Guid lastTargetEntity = Guid.Empty; byte[] lastData = null; listener.OnNewInteractionLink = lnk => { myLink = lnk; lnk.OnRegisterReceiver = receiver => { Assert.AreEqual(receiver, me); onRegister.Set(); }; lnk.OnMessage = (msg, sender) => { Assert.AreEqual(msg.ID.From, me); run.RelayMessageToConsensus(msg, sender); lastTargetEntity = msg.ID.To; lastData = msg.Body; onMessage.Set(); }; lnk.OnUnregisterReceiver = receiver => { Assert.AreEqual(receiver, me); onUnregister.Set(); }; onLink.Set(); }; using (TcpClient client = new TcpClient("localhost", defaultPort)) { var stream = client.GetStream(); Assert.IsTrue(onLink.WaitOne()); Register(stream, me); Assert.IsTrue(onRegister.WaitOne()); Message findEntity = new Message(me, Guid.Empty, 0, null, 50); SendMessage(stream, findEntity); //broadcast, find entity int gen; ReadMessageDelivered(stream, findEntity.MessageID); Message broadCastResponse = ReadMessage(stream, out gen, 0); Assert.AreEqual(broadCastResponse.Data.Length, 16); Assert.AreEqual(broadCastResponse.To, me); Guid entityID = broadCastResponse.From; for (int i = 0; i < 10; i++) { float f = random.NextFloat(0, 100); Console.WriteLine(i + ": " + f); Message job = new Message(me, entityID, 0, BitConverter.GetBytes(f), 50); var t0 = PreciseTime.Now; SendMessage(stream, job); ReadMessageDelivered(stream, job.MessageID); Message response = ReadMessage(stream, out gen, 1); var delta = PreciseTime.Now - t0; deltas.Add(delta.TimeSpan); Assert.AreEqual(response.To, me); Assert.AreEqual(response.From, entityID); Assert.IsNotNull(response.Data); Assert.AreEqual(response.Data.Length, 4); float rs = BitConverter.ToSingle(response.Data, 0); Assert.AreEqual((float)Math.Sin(f), rs, i + ": " + f); } } } keepRunning = false; task.Wait(); foreach (var t in deltas) { Debug.WriteLine(t); } }