public async Task ParallelHashSetDuplicate()
        {
            var hashAlgorithm = new MurmurHashAlgorithmService();
            var ownerId       = Guid.NewGuid().ToString("N");
            var scheduler     = new HashSetDuplicateRemover();
            var requestHasher = new RequestHasher(hashAlgorithm);
            var r1            = new Request("http://www.a.com")
            {
                Owner = ownerId
            };

            r1.Hash = requestHasher.ComputeHash(r1);
            var isDuplicate = await scheduler.IsDuplicateAsync(r1);

            Assert.False(isDuplicate);
            Parallel.For(0, 1000, new ParallelOptions {
                MaxDegreeOfParallelism = 30
            }, async i =>
            {
                var r = new Request("http://www.a.com")
                {
                    Owner = ownerId
                };
                r.Hash      = requestHasher.ComputeHash(r);
                isDuplicate = await scheduler.IsDuplicateAsync(r);
                Assert.True(isDuplicate);
            });
        }
Пример #2
0
        public async Task HashSetDuplicate()
        {
            var hashAlgorithm = new MurmurHashAlgorithmService();
            var scheduler     = new HashSetDuplicateRemover();
            var requestHasher = new RequestHasher(hashAlgorithm);

            var ownerId = Guid.NewGuid().ToString("N");
            await scheduler.InitializeAsync(ownerId);

            var r1 = new Request("http://www.a.com")
            {
                Owner = ownerId
            };

            r1.Headers.Accept = ("asdfasdfasdf");
            requestHasher.ComputeHash(r1);
            var isDuplicate = await scheduler.IsDuplicateAsync(r1);

            Assert.False(isDuplicate);
            var r2 = new Request("http://www.a.com")
            {
                Owner = ownerId
            };

            requestHasher.ComputeHash(r2);
            isDuplicate = await scheduler.IsDuplicateAsync(r2);

            Assert.True(isDuplicate);
            var r3 = new Request("http://www.b.com")
            {
                Owner = ownerId
            };

            requestHasher.ComputeHash(r3);
            isDuplicate = await scheduler.IsDuplicateAsync(r3);

            Assert.False(isDuplicate);
            var r4 = new Request("http://www.b.com")
            {
                Owner = ownerId
            };

            requestHasher.ComputeHash(r4);

            isDuplicate = await scheduler.IsDuplicateAsync(r4);

            Assert.True(isDuplicate);
        }
Пример #3
0
        public void SerializeAndDeserialize3()
        {
            var hashAlgorithm = new MurmurHashAlgorithmService();
            var requestHasher = new RequestHasher(hashAlgorithm);
            var ownerId       = ObjectId.GenerateNewId().ToString();
            var r1            = new Request("http://www.a.com")
            {
                Owner = ownerId
            };
            var h1 = requestHasher.ComputeHash(r1);

            var r2 = new Request("http://www.a.com")
            {
                Owner = ownerId
            };
            var h2 = requestHasher.ComputeHash(r2);

            Assert.Equal(h1, h2);
        }
Пример #4
0
        public void Enqueue()
        {
            var queue   = new RequestedQueue();
            var request = new Request("http://www.baidu.com")
            {
                Timeout = 2000
            };
            var requestHasher = new RequestHasher(new MurmurHashAlgorithmService());

            requestHasher.ComputeHash(request);
            queue.Enqueue(request);
            Assert.Equal(1, queue.Count);
            queue.Enqueue(request);
            Assert.Equal(1, queue.Count);
            var request2 = new Request("http://www.baidu.com/2")
            {
                Timeout = 2000
            };

            requestHasher.ComputeHash(request2);
            queue.Enqueue(request2);
            Assert.Equal(2, queue.Count);
        }
Пример #5
0
        public void ParallelEnqueue()
        {
            var queue         = new RequestedQueue();
            var requestHasher = new RequestHasher(new MurmurHashAlgorithmService());

            Parallel.For(1, 10000, new ParallelOptions(), (i) =>
            {
                var request = new Request($"http://www.baidu.com/{i}")
                {
                    Timeout = 2000
                };
                requestHasher.ComputeHash(request);
                queue.Enqueue(request);
            });
        }
Пример #6
0
        public void Dequeue()
        {
            var queue   = new RequestedQueue();
            var request = new Request("http://www.baidu.com")
            {
                Timeout = 2000
            };
            var requestHasher = new RequestHasher(new MurmurHashAlgorithmService());

            requestHasher.ComputeHash(request);
            queue.Enqueue(request);
            Assert.Equal(1, queue.Count);
            Thread.Sleep(1000);
            var request2 = queue.Dequeue(request.Hash);

            Assert.NotNull(request2);
            Assert.Equal(request, request2);
            Assert.Equal(request.Hash, request2.Hash);
        }
Пример #7
0
        public void DequeueTimeout()
        {
            var queue   = new RequestedQueue();
            var request = new Request("http://www.baidu.com")
            {
                Timeout = 2000
            };
            var requestHasher = new RequestHasher(new MurmurHashAlgorithmService());

            requestHasher.ComputeHash(request);
            queue.Enqueue(request);
            Assert.Equal(1, queue.Count);
            Thread.Sleep(2500);
            Assert.Null(queue.Dequeue(request.Hash));
            var timeoutRequests = queue.GetAllTimeoutList();

            Assert.Single(timeoutRequests);
            Assert.Equal(request.Hash, timeoutRequests[0].Hash);
        }
Пример #8
0
        public void ParallelDequeue()
        {
            var queue         = new RequestedQueue();
            var requestHasher = new RequestHasher(new MurmurHashAlgorithmService());
            var hashes        = new List <string>();

            for (var i = 0; i < 10000; ++i)
            {
                var request = new Request($"http://www.baidu.com/{i}")
                {
                    Timeout = 30000
                };
                requestHasher.ComputeHash(request);
                hashes.Add(request.Hash);
                queue.Enqueue(request);
            }

            Parallel.ForEach(hashes, new ParallelOptions(), (hash) =>
            {
                var request = queue.Dequeue(hash);
                Assert.NotNull(request);
            });
        }