예제 #1
0
        public void RunTest()
        {
            const string name1 = "Henry has a first name!";
            const string name2 = "Larry doesn't have a last name!";
            var          key1  = new PersonKey(name1);
            var          key2  = new PersonKey(name2);

            var personEntry1 = new PersonEntry(key1, 10);
            var personEntry2 = new PersonEntry(key2, 5);

            var thresholdsByKey = new Dictionary <PersonKey, int>();

            thresholdsByKey.Add(key1, 30);
            thresholdsByKey.Add(key2, 2);

            var levelRemovalProcessor   = new RemovalByLevelThresholdProcessor(thresholdsByKey);
            var friendClearingProcessor = new FriendClearingProcessor();

            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, levelRemovalProcessor);
                    serializer.Serialize(writer, friendClearingProcessor);
                }
                ms.Position = 0;
                Console.WriteLine(ms.ToArray().ToHex());
                using (var reader = new BinaryReader(ms)) {
                    levelRemovalProcessor   = serializer.Deserialize <RemovalByLevelThresholdProcessor>(reader);
                    friendClearingProcessor = serializer.Deserialize <FriendClearingProcessor>(reader);
                }
            }

            var entry1 = new Entry <PersonKey, PersonEntry>(key1, personEntry1);
            var entry2 = new Entry <PersonKey, PersonEntry>(key2, personEntry2);

            friendClearingProcessor.Process(entry1);
            friendClearingProcessor.Process(entry2);

            levelRemovalProcessor.Process(entry1);
            levelRemovalProcessor.Process(entry2);

            assertTrue(entry1.IsPresent());
            assertFalse(entry2.IsPresent());
        }
예제 #2
0
        public void RunTest()
        {
            const string name1 = "Henry has a first name!";
            const string name2 = "Larry doesn't have a last name!";
            var          key1  = new PersonKey(Guid.NewGuid(), name1);
            var          key2  = new PersonKey(Guid.NewGuid(), name2);

            var personEntry1 = new PersonEntry(key1, 10);
            var personEntry2 = new PersonEntry(key2, 5);

            personEntry1.Friends.Add(new PersonFriend(0xAEF8329dF, "Mark"));
            personEntry2.Friends.Add(new PersonFriend(0xF8372D33F, "Henry"));
            personEntry2.Friends.Add(new PersonFriend(0x47928C3ED, "Jane"));

            var thresholdsByKey = new Dictionary <PersonKey, int>();

            thresholdsByKey.Add(key1, 30);
            thresholdsByKey.Add(key2, 2);

            var levelRemovalProcessor   = new RemovalByLevelThresholdProcessor(thresholdsByKey);
            var friendClearingProcessor = new FriendClearingProcessor();

            var serializer = new PofSerializer(context);

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, levelRemovalProcessor);
                    serializer.Serialize(writer, friendClearingProcessor);
                }
                ms.Position = 0;
                Console.WriteLine(ms.ToArray().ToHex());
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    levelRemovalProcessor   = serializer.Deserialize <RemovalByLevelThresholdProcessor>(reader);
                    friendClearingProcessor = serializer.Deserialize <FriendClearingProcessor>(reader);
                }
            }

            var entry1         = new Entry <PersonKey, PersonEntry>(key1, personEntry1);
            var entry2         = new Entry <PersonKey, PersonEntry>(key2, personEntry2);
            var originalEntry1 = entry1;
            var originalEntry2 = entry2;

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, entry1);
                    serializer.Serialize(writer, entry2);
                }
                ms.Position = 0;
                Console.WriteLine(ms.ToArray().ToHex());
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    entry1 = serializer.Deserialize <Entry <PersonKey, PersonEntry> >(reader);

                    int frameLength = reader.ReadInt32();
                    var frameBody   = reader.ReadBytes(frameLength);
                    using (var innerMs = new MemoryStream(frameBody))
                        using (var innerReader = new BinaryReader(innerMs, Encoding.UTF8, true)) {
                            entry2 = (Entry <PersonKey, PersonEntry>)serializer.Deserialize(innerReader, SerializationFlags.Lengthless, null);
                        }
                }
            }

            friendClearingProcessor.Process(entry1);
            friendClearingProcessor.Process(entry2);

            levelRemovalProcessor.Process(entry1);
            levelRemovalProcessor.Process(entry2);

            AssertTrue(entry1.IsPresent());
            AssertFalse(entry2.IsPresent());

            using (var ms = new MemoryStream()) {
                using (var writer = new BinaryWriter(ms, Encoding.UTF8, true)) {
                    serializer.Serialize(writer, entry1);
                    serializer.Serialize(writer, entry2);
                }
                ms.Position = 0;
                Console.WriteLine(ms.ToArray().ToHex());
                using (var reader = new BinaryReader(ms, Encoding.UTF8, true)) {
                    entry1 = serializer.Deserialize <Entry <PersonKey, PersonEntry> >(reader);
                    entry2 = serializer.Deserialize <Entry <PersonKey, PersonEntry> >(reader);
                }
            }

            AssertTrue(entry1.IsPresent());
            AssertFalse(entry2.IsPresent());
        }