Ejemplo n.º 1
0
        public void CaptureReader_Can_Read_Back_A_Stream_That_Was_Written_By_A_CaptureWriter()
        {
            var writeOperations = 100;
            var ms = new ConcurrentStream(new MemoryStream());

            var sat = new Dictionary<Guid, short>();
            sat.Add(Guid.NewGuid(), 24);
            sat.Add(Guid.NewGuid(), 25);
            sat.Add(Guid.NewGuid(), 26);

            var stateResolver = new Mock<IStateResolver>();
            stateResolver.SetupGet(s => s.AllocationTable).Returns(sat);
            stateResolver.Setup(s => s.IsAllocated(It.IsAny<Guid>())).Returns<Guid>(g => sat.ContainsKey(g));
            stateResolver.Setup(s => s.GetAllocatedId(It.IsAny<Guid>())).Returns<Guid>(g => sat[g]);

            //write operations
            var captureWriter = new CaptureWriter(ms, stateResolver.Object);

            for (int i = 0; i < writeOperations; i++)
            {
                var timestamp = DateTime.UtcNow;
                var randomData = new byte[1024];
                new Random().NextBytes(randomData);

                var randomSat = (short)new Random().Next(24, 26);

                var captureState = new Mock<ICaptureState>();
                captureState.Setup(c => c.Data).Returns(randomData);
                captureState.Setup(c => c.Guid).Returns(sat.Where(f => f.Value == randomSat).First().Key);
                captureState.Setup(c => c.Timestamp).Returns(timestamp);

                captureWriter.Write(captureState.Object);
            }

            //rewind the stream to the beginning
            ms.Position = 0;

            //read operations
            var readStateResolver = new StateResolver();
            readStateResolver.Add(new SimpleStateController { Guid = sat.Where(f => f.Value == 24).First().Key });
            readStateResolver.Add(new SimpleStateController { Guid = sat.Where(f => f.Value == 25).First().Key });
            readStateResolver.Add(new SimpleStateController { Guid = sat.Where(f => f.Value == 26).First().Key });

            var captureReader = new CaptureReader(ms, readStateResolver);

            while (captureReader.Position < captureReader.Count)
            {
                captureReader.Read();
            }
        }
Ejemplo n.º 2
0
        public void CaptureReader_Selects_The_Correct_IStateController_During_Reads()
        {
            var immlStateController = new Mock<IStateController>();
            var immlStateControllerId = (short)100;
            var immlStateControllerGuid = Guid.NewGuid();
            immlStateController.Setup(g => g.Guid).Returns(immlStateControllerGuid);

            var boneNodeStateController = new Mock<IStateController>();
            var boneNodeStateControllerId = (short)123;
            var boneNodeStateControllerGuid = Guid.NewGuid();
            boneNodeStateController.Setup(g => g.Guid).Returns(boneNodeStateControllerGuid);

            //build 2 different state mappings
            var ms = new ConcurrentStream(new MemoryStream());

            //header
            var header = _CreateContinuumHeader(1000, 10000);
            ms.Write(header, 0, header.Length);

            //sat
            ms.Write(BitConverter.GetBytes(2), 0, 4); //count

            //mapping for imml
            ms.Write(BitConverter.GetBytes(immlStateControllerId), 0, 2);
            ms.Write(immlStateControllerGuid.ToByteArray(), 0, 16);

            //mapping for bones
            ms.Write(BitConverter.GetBytes(boneNodeStateControllerId), 0, 2);
            ms.Write(boneNodeStateControllerGuid.ToByteArray(), 0, 16);

            //generate 2 dummy states, one for each controller
            var firstRandomBytes = new byte[1024];
            new Random().NextBytes(firstRandomBytes);

            ms.Write(BitConverter.GetBytes(immlStateControllerId), 0, 2);
            ms.Write(BitConverter.GetBytes(DateTime.UtcNow.ToBinary()), 0, 8);
            ms.Write(BitConverter.GetBytes((int)firstRandomBytes.Length), 0, 4);
            ms.Write(firstRandomBytes, 0, firstRandomBytes.Length);

            var secondRandomBytes = new byte[2048];
            new Random().NextBytes(secondRandomBytes);

            ms.Write(BitConverter.GetBytes(boneNodeStateControllerId), 0, 2);
            ms.Write(BitConverter.GetBytes(DateTime.UtcNow.ToBinary()), 0, 8);
            ms.Write(BitConverter.GetBytes((int)secondRandomBytes.Length), 0, 4);
            ms.Write(secondRandomBytes, 0, secondRandomBytes.Length);

            ms.Position = 0;

            //perform the reads and verify
            var stateResolver = new StateResolver();
            stateResolver.Add(immlStateController.Object);
            stateResolver.Add(boneNodeStateController.Object);

            var captureReader = new CaptureReader(ms, stateResolver);
            captureReader.Read();
            captureReader.Read();

            boneNodeStateController.Verify(c => c.Create(It.IsAny<byte[]>(), It.IsAny<DateTime>(), It.IsAny<double>()), Times.Once());
            immlStateController.Verify(c => c.Create(It.IsAny<byte[]>(), It.IsAny<DateTime>(), It.IsAny<double>()), Times.Once());
        }