Beispiel #1
0
        public async Task BadServerPort_Async()
        {
            bool gotexception = false;

            _whois.WhenExceptionThrown += (o, e) =>
            {
                if (e.Exception is ArgumentOutOfRangeException)
                {
                    gotexception = true;
                }
            };

            await _whois.QueryAsync("example.com", BadPortServer);

            Assert.IsTrue(gotexception);
        }
Beispiel #2
0
        public async Task TimeoutsRace(int serverDelay, int timeout1, int timeout2)
        {
            bool c1_result = false;
            bool c2_result = false;

            var cts = new CancellationTokenSource();
            var ct  = cts.Token;

            var server_t = Task.Run(async() =>
            {
                var server = new TestServer();

                server.WhenRequestReceived += (o, args) =>
                {
                    args.DelayResponse = serverDelay;
                    args.Response      = args.Request;
                };

                var pair = LocalServer.Split(new char[] { ':' }, 2);
                var addr = pair[0];
                var port = int.Parse(pair[1]);
                await server.StartListening(ct, addr, port);
            });

            _whois.WhenRequestReady += (o, e) =>
            {
                var whois = (YaWhoisClient)o;
                whois.ReadWriteTimeout = e.Query.Contains("example1")
                    ? timeout1
                    : timeout2;
            };

            _whois.WhenExceptionThrown += (o, e) =>
            {
                if (e.Query.Contains("example1"))
                {
                    c1_result = true;
                }
                if (e.Query.Contains("example2"))
                {
                    c2_result = true;
                }
            };

            var c1 = Task.Run(async() =>
            {
                await Task.Delay(500);
                await _whois.QueryAsync("example1.com", LocalServer);
            });

            var c2 = Task.Run(async() =>
            {
                await Task.Delay(500);
                await _whois.QueryAsync("example2.com", LocalServer);
            });

            await Task.WhenAll(c1, c2);

            cts.Cancel();
            await server_t;

            Assert.IsTrue(c1_result);
            Assert.IsFalse(c2_result);
        }
Beispiel #3
0
        public async Task ClientReady(string id, Guid queueId, string language)
        {
            var login  = Context.User.GetLogin();
            var client = Clients.Client(id);

            if (queueId == Guid.Empty)
            {
                _logger.LogWarning($"Client {login} | {id} has passed empty queueId.");
                return;
            }

            if (!_queue.Queue.TryRemove(queueId, out IEnumerable <WhoisDto> dtos))
            {
                _logger.LogError($"Failed remove Ping Request {queueId} for client {id}.");
                await client.SendAsync("Exception", "Queue error.");

                return;
            }

            _logger.LogInformation($"User {login} has started Whois Request {queueId} with {dtos.Count()} host(s).");

            var culture = await GetCultureAsync(id, language);

            _tasks.QueueTask(async token =>
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }

                Thread.CurrentThread.CurrentCulture       =
                    Thread.CurrentThread.CurrentUICulture = culture;

                var taskFactory = new TaskFactory(TaskScheduler.Current);
                var cde         = new AsyncCountdownEvent(dtos.Count());
                var tasks       = new List <Task>();

                var whois              = new YaWhoisClient();
                whois.ResponseParsed  += Whois_ResponseParsed;
                whois.ExceptionThrown += Whois_ExceptionThrown;

                foreach (var host in dtos.Select(a => a.Hostname))
                {
                    var data = new YaWhoisData()
                    {
                        Client   = client,
                        ClientId = id,
                        Object   = host
                    };

                    tasks.Add(taskFactory.StartNew((d) =>
                    {
                        whois.QueryAsync(host, token: token, value: d).Wait();
                        cde.Signal();
                    }, data, token));
                }

                try
                {
                    Task.WaitAll(tasks.ToArray());
                }
                catch (Exception e)
                {
                    var obj = e.Data["object"].ToString();
                    _logger.LogError(e, $"Whois {obj} failed (clientId: {id}).");
                }

                await cde.WaitAsync();
            });
        }