Exemplo n.º 1
0
        public void Store(StoreOperation operation)
        {
            // Arrange
            var packet = new StorePacket()
            {
                Expires   = 1000,
                Flags     = 101,
                Key       = new byte[] { 7 },
                Operation = operation,
                Value     = new ArraySegment <byte>(new byte[] { 46, 62 }),
                Version   = 4363431212
            };

            m_mockPacketBuilder.Setup(builder => builder.Reset()).Returns(m_mockPacketBuilder.Object);
            m_mockPacketBuilder.Setup(builder => builder.WriteOperation((RequestOperation)packet.Operation)).Returns(m_mockPacketBuilder.Object);
            m_mockPacketBuilder.Setup(builder => builder.WriteNoReply()).Returns(m_mockPacketBuilder.Object);
            m_mockPacketBuilder.Setup(builder => builder.WriteVersion(packet.Version)).Returns(m_mockPacketBuilder.Object);
            m_mockPacketBuilder.Setup(builder => builder.WriteFlags(packet.Flags)).Returns(m_mockPacketBuilder.Object);
            m_mockPacketBuilder.Setup(builder => builder.WriteExpires(packet.Expires)).Returns(m_mockPacketBuilder.Object);
            m_mockPacketBuilder.Setup(builder => builder.WriteKey(packet.Key)).Returns(m_mockPacketBuilder.Object);
            m_mockPacketBuilder.Setup(builder => builder.WriteValue(packet.Value)).Returns(m_mockPacketBuilder.Object);

            // Act
            m_writer.Store(packet, true);

            // Assert
        }
Exemplo n.º 2
0
        public void Store(StoreOperation operation, string optionsString, bool connectSucceed, bool storeSucceed)
        {
            // Arrange
            var options  = new ProtocolOptions(optionsString);
            var protocol = new DefaultProtocol(m_mockFactory.Object, options);
            var datakey  = new DataKey <int>("Key1")
            {
                Value   = 12345,
                Version = 556
            };
            int         flags  = 10101;
            var         key    = new byte[] { };
            var         value  = new ArraySegment <byte>(new byte[] { });
            StorePacket packet = null;

            m_mockObjectFormatter.Setup(formatter => formatter.Serialize <int>(12345, out flags)).Returns(value);
            m_mockEncoder.Setup(encoder => encoder.GetBytes("Key1")).Returns(key);
            m_mockCommandWriter.Setup(writer => writer.Store(It.IsAny <StorePacket>(), options.NoReply))
            .Callback <StorePacket, bool>((p, nr) => packet = p);
            m_mockFactory.Setup(factory => factory.Context(key))
            .Returns <byte[]>(state => (Action2 <IClientConnection, object> action) =>
            {
                if (connectSucceed)
                {
                    action(m_mockClientConnection.Object, state);
                }

                return(connectSucceed);
            });
            if (connectSucceed)
            {
                m_mockClientConnection.SetupGet(connection => connection.Writer).Returns(m_mockBinaryWriter.Object);
                m_mockPacketBuilder.Setup(builder => builder.WriteTo(m_mockBinaryWriter.Object)).Returns(0);
                if (!options.NoReply)
                {
                    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.ReadStored()).Returns(storeSucceed);
                }
            }

            // Act
            var result = protocol.Store(operation, datakey, 100);

            // Assert
            Assert.Equal(connectSucceed && (options.NoReply || storeSucceed), result);
            Assert.NotNull(packet);
            Assert.Equal(100, packet.Expires);
            Assert.Equal(flags, packet.Flags);
            Assert.Equal(key, packet.Key);
            Assert.Equal(operation, packet.Operation);
            Assert.Equal(value, packet.Value);
            Assert.Equal(556, packet.Version);
        }