Ejemplo n.º 1
0
        public void GetMany(GetOperation operation)
        {
            // Arrange
            var options  = new ProtocolOptions();
            var protocol = new DefaultProtocol(m_mockFactory.Object, options);
            var datakey1 = new DataKey <int>("Key1");
            var datakey2 = new DataKey <bool>("Key2");
            var key1     = new byte[] { 1 };
            var key2     = new byte[] { 2 };

            byte[][] keys   = null;
            var      packet = new ValuePacket()
            {
                Flags   = 101,
                Key     = key1,
                Value   = new ArraySegment <byte>(new byte[] { }),
                Version = 245
            };

            m_mockEncoder.Setup(encoder => encoder.GetBytes("Key1")).Returns(key1);
            m_mockEncoder.Setup(encoder => encoder.GetBytes("Key2")).Returns(key2);
            m_mockFactory.Setup(factory => factory.Context(It.IsAny <byte[][]>()))
            .Returns <byte[][]>(state => (Action2 <IClientConnection, object> action) =>
            {
                keys = (byte[][])state;
                action(m_mockClientConnection.Object, state);
                return(true);
            });
            m_mockCommandWriter.Setup(writer => writer.GetMany(operation, It.IsAny <byte[][]>()));
            m_mockClientConnection.SetupGet(connection => connection.Writer).Returns(m_mockBinaryWriter.Object);
            m_mockPacketBuilder.Setup(builder => builder.WriteTo(m_mockBinaryWriter.Object)).Returns(0);
            m_mockClientConnection.SetupGet(connection => connection.Reader).Returns(m_mockBinaryReader.Object);
            m_mockFactory.Setup(factory => factory.CreatePacketParser(m_mockBinaryReader.Object, It.IsAny <Buffer <byte> >())).Returns(m_mockPacketParser.Object);
            m_mockFactory.Setup(factory => factory.CreateCommandReader(m_mockPacketParser.Object)).Returns(m_mockCommandReader.Object);
            m_mockCommandReader.Setup(reader => reader.ReadValue(operation == GetOperation.Gets)).Returns(() =>
            {
                var p = packet;
                if (packet != null)
                {
                    packet = null;
                }

                return(p);
            });
            m_mockObjectFormatter.Setup(formatter => formatter.Deserialize <int>(packet.Value, packet.Flags)).Returns(507);

            // Act
            var result = protocol.GetMany(operation, new DataKey[] { datakey1, datakey2 });

            // Assert
            Assert.True(result);
            Assert.True(datakey1.HasValue);
            Assert.Equal(245, datakey1.Version);
            Assert.Equal(507, datakey1.Value);
            Assert.False(datakey2.HasValue);
        }
Ejemplo n.º 2
0
        public void GetMany_NotFound(GetOperation operation)
        {
            // Arrange
            var options  = new ProtocolOptions();
            var protocol = new DefaultProtocol(m_mockFactory.Object, options);
            var datakey1 = new DataKey <int>("Key1");
            var datakey2 = new DataKey <bool>("Key2");
            var key1     = new byte[] { 1 };
            var key2     = new byte[] { 2 };

            byte[][]    keys   = null;
            ValuePacket packet = null;

            m_mockEncoder.Setup(encoder => encoder.GetBytes("Key1")).Returns(key1);
            m_mockEncoder.Setup(encoder => encoder.GetBytes("Key2")).Returns(key2);
            m_mockFactory.Setup(factory => factory.Context(It.IsAny <byte[][]>()))
            .Returns <byte[][]>(state => (Action2 <IClientConnection, object> action) =>
            {
                keys = (byte[][])state;
                action(m_mockClientConnection.Object, state);
                return(true);
            });
            m_mockCommandWriter.Setup(writer => writer.GetMany(operation, It.IsAny <byte[][]>()));
            m_mockClientConnection.SetupGet(connection => connection.Writer).Returns(m_mockBinaryWriter.Object);
            m_mockPacketBuilder.Setup(builder => builder.WriteTo(m_mockBinaryWriter.Object)).Returns(0);
            m_mockClientConnection.SetupGet(connection => connection.Reader).Returns(m_mockBinaryReader.Object);
            m_mockFactory.Setup(factory => factory.CreatePacketParser(m_mockBinaryReader.Object, It.IsAny <Buffer <byte> >())).Returns(m_mockPacketParser.Object);
            m_mockFactory.Setup(factory => factory.CreateCommandReader(m_mockPacketParser.Object)).Returns(m_mockCommandReader.Object);
            m_mockCommandReader.Setup(reader => reader.ReadValue(operation == GetOperation.Gets)).Returns(packet);

            // Act
            var result = protocol.GetMany(operation, new DataKey[] { datakey1, datakey2 });

            // Assert
            Assert.True(result);
        }